user1492081
user1492081

Reputation: 21

Automating switching browsers when running Selenium-Java tests

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");

Upvotes: 2

Views: 2104

Answers (1)

user1492086
user1492086

Reputation: 21

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.

Upvotes: 1

Related Questions