SPONSORS:






User Tag List

Results 1 to 2 of 2
  1. #1
    Apprentice
    Join Date
    Jul 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    'Getter' methods question using Page Object Model framework with TestNG

    Hi,

    I was wondering if someone could answer a question I have regarding 'best practice' when defining 'getter' methods in a page object class and using them within TestNG.

    As an example, I want to validate that a radio box is selected, so for this I'll use isSelected() on the web element

    Should I...

    1. Create a getter method in the Page Object that returns the WebElement

    public WebElement getPayFrequencyRadioBox() {
    return this.monthlyPeriodOfPay;
    }

    Then use testNG to test whether it is selected (which puts the emphasis on the tester to know when to use isSelected() within the test):

    Assert.assertTrue(TaxCalcPage.getMonthlyPayFrequen cy().isSelected());

    OR

    2. Create a getter method in the Page Object that returns a Boolean

    public boolean getPayFrequencyRadioBoxIsSelected() {
    return this.monthlyPeriodOfPay.isSelected();
    }

    Then use testng to test whether it is selected (all the tester has to do is to call the method)

    Assert.assertTrue(TaxCalcPage.getMonthlyPayFrequen cyIsSelected()

  2. #2
    SQA Knight
    Join Date
    May 2006
    Location
    Playa Del Rey, California, United States
    Posts
    2,616
    Post Thanks / Like
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Total Downloaded
    0
    #2 is preferred.

    However, for boolean values, you want to name it starting with 'is...', isPayFrequencySelected()

    Also, leave out the control type. As that radio box may in the future be a select list, or some other control. Keep it to business level descriptions, and avoid UI details.

    If there are multiple choices. You may consider returning an Enum instead.
    Last edited by dlai; 10-14-2016 at 01:07 PM.
    David Lai
    SDET / Consultant
    LinkedIn profile

  3. #3
    Apprentice
    Join Date
    Jul 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    Regarding enum...something like this:

    public enum payFrequency {
    monthly,
    weekly,
    fourweekly,
    twoweekly
    }


    public String payFrequencySelected(){
    if (this.weeklyPayFrequency.isSelected()){
    return payFrequency.weekly.toString();

    } else if (this.monthlyPayFrequency.isSelected()){
    return payFrequency.monthly.toString();

    } else if (this.fourWeeklyPayFrequency.isSelected()){

    return payFrequency.fourweekly.toString();

    } else if (this.twoWeeklyPayFrequency.isSelected()){
    return payFrequency.twoweekly.toString();
    }
    return null;
    }

  4. #4
    Apprentice
    Join Date
    Sep 2011
    Posts
    35
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    Hi,

    The better approach is always define your own class like MyRadioButton under which you can define all the required methods. Then

    public MyRadioButton getPayFrequencyRadioBox() {
    return new MyRadioButton (this.monthlyPeriodOfPay);
    }

    so you use "getPayFrequencyRadioBox(). " it display all methods you define on MyRadioButton.

    Thanks,
    sgh

  5. #5
    Apprentice
    Join Date
    Jul 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    Interesting approach but how does this fit it with page objects. The approach with POM here is to create a class per page (or per header, footer etc) which then defines the elements followed by the methods to allow the tester to interact with a web page and validate returned values. In you example would I create a MyRadioButton class within the page class where the element resides, if that's possible?

  6. #6
    SQA Knight
    Join Date
    May 2006
    Location
    Playa Del Rey, California, United States
    Posts
    2,616
    Post Thanks / Like
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Total Downloaded
    0
    Quote Originally Posted by awhawh View Post
    Interesting approach but how does this fit it with page objects. The approach with POM here is to create a class per page (or per header, footer etc) which then defines the elements followed by the methods to allow the tester to interact with a web page and validate returned values. In you example would I create a MyRadioButton class within the page class where the element resides, if that's possible?
    PageObject model is a subset of the Facade model (much like a data mapping layer abstracts the data abstraction from the database calls in the application stack). The whole idea is to separate high level from low level logic. Your tests rests at the business level, you page object rests at the implementation level. So you should not expose elements from the pageobject. You only expose Actions and Properties.
    David Lai
    SDET / Consultant
    LinkedIn profile

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.71%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 01:21 PM.

Copyright BetaSoft Inc.