0

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
...
JeffC
  • 22,180
  • 5
  • 32
  • 55
Daria
  • 29
  • 6
  • If you are using maven check this -- https://stackoverflow.com/questions/9902084/how-to-pass-input-from-command-line-to-junit-maven-test-program – Grasshopper May 07 '18 at 10:54

1 Answers1

2

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.

Kovacic
  • 1,473
  • 6
  • 21
  • 1
    You didn't initialise variable. public void getScreen(WebDriver name) throws Exception { File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileHandler.copy(srcFile, new File("/Users/admin/Desktop/" + name + ".jpg")); } name == WebDriver and Your trying to take screenshot wirh driver which is not initialised. – Kovacic May 07 '18 at 13:43
  • yes, it works, but I have one problem with function in my superclass: `public class main { private WebDriver driver; public static String domain = "http://testdomain.com"; public void getScreen(String name) throws Exception { File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileHandler.copy(srcFile, new File("/Users/admin/Desktop/" + name + ".jpg")); }` I used getScreen function in my tests, but after I specified getScreen in superclass "main" it starts return "NullPointerException". You don't know what the problem is? – Daria May 07 '18 at 13:44