2

I am currently working on a project using Java, Selenium and Testng. My overall goal is to test the functionality of a webpage over different web browsers. I have my Selenium code working and am able to run the test on Chrome and Firefox. However, I have to manually change the code to switch the browser. I do this by commenting out driver = new ChromeDriver(); I would like to edit my code so the test runs in Firefox and when that test is complete start the test in Chrome. Can someone please guide me in the right direction?

Here is a sample of what my code looks like:

WebDriver driver = null;
Selenium selenium = null;


@BeforeSuite
public void setup() throws Exception {

    ///    Chrome Driver  ///
    System.setProperty("webdriver.chrome.driver", "mac/chromedriver.exe");
    //driver = new ChromeDriver();


    ///    Firefox Driver  ///
    driver = new FirefoxDriver();


}


@Test
public void testGoogle() throws Exception {

selenium = new WebDriverBackedSelenium(driver,"URL");

1 Answer 1

1

There could be quite a few ways of achieving this.

In the setup you can read a property and based on that you can instantiate the right driver.

String driverType = System.getProperty("driverType");
if ("firefox".equals(driverType))
   driver = new FirefoxDriver().....

You can run the test twice, once with firefox property and then with chrome property.

Other option is to keep all the test in one class. Then extend this class by two classes, one for firefox setup and another for chrome setup. Then you can run both the subclass tests in a suite. They would run one after the another.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for getting back to me so fast. I used your first suggestion and added two if statements (one for firefox, the other for Chrome) and I recieve this error: Testing... FAILED: testGoogle java.lang.IllegalStateException: Driver instance must support JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.