Alex Toma
Alex Toma

Reputation: 101

Java/Selenium: Class containing methods for each Browser

Class containing all browsers in methods:

public class OpenBrowser {

    public static void firefox() {
        WebDriver driver = new FirefoxDriver();
    }

    public static void chrome() {
        WebDriver driver = new ChromeDriver();
    }
}

Class containing all environments in methods:

public class OpenSite {

    public static void stage(WebDriver driver) {

        driver.get("stagelink.com");
    }

    public static void dev(WebDriver driver) {

        driver.get("devlink.com");
    }
}

Class containing page objects:

public class HomePage {
    private static WebElement element = null;

    public static WebElement object1(WebDriver driver) {
        element = driver.findElement(By.linkText("object1"));
        return element;
    }
}

Class containing actual test

public class Test {

    public static void TC1(WebDriver driver) {

        HomePage.object1(driver).click();

    }
}

MAIN CLASS

public class AllTests {
    public static void main(String[] args){

        OpenBrowser.chrome();

        OpenSite.dev(driver);

        Test.TC1(driver);
    }
}

I am fairly new to all things JAVA and selenium and I've been trying to set up some basic tests and organize them as much as I can. All went well until I tried to create a class that contains methods for all Browsers and then call them in Main based on preference. The issue is that I'm not sure how to import the Driver in main from OpenBrowser.java because "driver" cannot be resolved anymore, since it is created somewhere else.

*I know people online are suggesting testing frameworks like Junit or Testng but I feel like they're a bit advanced for me at the moment and I prefer doing this, this way if it is possible. **Also I know that performing the same tests in various browsers is not really possible since things can go very wrong, but the environments that I'm testing at the moment are fairly basic and I want to do this at least as an exercise.

Thank you!

Upvotes: 0

Views: 988

Answers (1)

Alejandro C.
Alejandro C.

Reputation: 3801

Change openbrowser to:

public class OpenBrowser {

    public static WebDriver firefox() {
        return new FirefoxDriver();
    }

    public static WebDriver chrome() {
        return new ChromeDriver();
    }
}

Then in alltests

public class AllTests {
    public static void main(String[] args){

        WebDriver driver = OpenBrowser.chrome();

        OpenSite.dev(driver);

        Test.TC1(driver);
    }
}

Upvotes: 1

Related Questions