Daria
Daria

Reputation: 29

How to use one parameter for all pages and test using Page Object Model pattern (Webdriver + Java + JUnit)?

In all pages and tests I specify the variable String domain =" https:// ... " and sometimes I need the same tests to run on different domains. Is it possible to specify a variable with a domain once for all tests and pages (so that when I change the value of the variable, I have all the tests run on another domain)?

public class test {
    public static WebDriver driver;
    public static pageLogin login;
    public static pageMain main;
    
    String domain = "http://testdomain.com";
    
    @BeforeClass
...

Upvotes: 0

Views: 610

Answers (1)

Kovacic
Kovacic

Reputation: 1481

I always make an let's say 'super-class' screen page object where I keep my driver instance and global variables like in example bellow:

public class Screen {
    private String currentScreen = "Page";
    private boolean isLoaded = false;
    private MobileDriver driver;

    public Screen(MobileDriver mobileDriver) {
        this.driver = mobileDriver;
    }

public String getCurrentScreen() {
    return name;
}

public void setCurrentScreen(String name) {
    this.name = name;
}


public boolean isLoaded() {
    return isLoaded;
}

public void setLoaded(boolean loaded) {
    isLoaded = loaded;
}

so I extend all of my other page object classes with this where I provide driver and all needed global variables with getter/setter

so this is how child page-object class would look like

public class OnBoardingScreen extends Screen{
    @AndroidFindBy(id = "onboarding_content")
    @WithTimeout(time = 1, unit = TimeUnit.SECONDS)
    private MobileElement labelContent;

    @AndroidFindBy(id = "onboarding_skip_intro")
    @WithTimeout(time = 1, unit = TimeUnit.SECONDS)
    private MobileElement buttonSkipIntro;


    public OnBoardingScreen(MobileDriver driver) {
        super(driver);
        PageFactory.initElements(new AppiumFieldDecorator(driver, 2, TimeUnit.SECONDS), this);

        WaitUtils.isElementPresent(driver, buttonSkipIntro, 1);

        if (!Util.areElementsLoaded(labelTitle, labelContent, buttonSkipIntro)) {
            super.setLoaded(false);
        } else {
            super.setLoaded(true);
        }

so You could certainly do this kind of design to Your tests, by adding domain variable in this super-class.

Upvotes: 2

Related Questions