Multi Browser Testing in Selenium and TestNG

Posted By :Nidhi Ojha |29th November 2019

In this blog, we will be learning about multi-browser testing and its implementation in Selenium. For this, we will integrate Selenium with TestNG and use @Parameter annotation of TestNG to parameterize the test script with different values of the browser. Also, keep in mind that multi-browser testing can be carried out using Selenium Grid as well. But here, we will use TestNG and Selenium WebDriver only for implementing multi-browser testing.

What is Multi-Browser Testing?

Multi-browser testing, often known as cross-browser testing, involves testing the application under test using multiple supporting browsers. The demand for multi-browser testing stems from the reality that different browsers have distinct UI implementations. As a result, there is no guarantee that apps running in Chrome will run well in Internet Explorer.

Multi-Browser Testing in Selenium and TestNG:

Multi-browser testing can be implemented in Selenium by parameterizing the browser variable. For parameterizing the browser variable we can use the @Parameter annotation of TestNG. Using the @Parameter annotation, we can pass different values of the browser to the test scripts from the testng.xml file. The browser parameter value can then be used to instantiate Selenium WebDriver's associated driver class. Because the browser value is used throughout all test methods, it is preferable to use the browser variable in the @BeforeTest method.

The BeforeTest method with @Parameter of TestNG will look like this:

@Parameters("browser") @BeforeTest public void setBrowser(String browser) {   if (browser.equalsIgnoreCase("Firefox")) {      driver = new FirefoxDriver();         }   else if (browser.equalsIgnoreCase("Chrome")) {      System.setProperty("",         + pathToChromeDriverBinary + "chromedriver.exe");      driver = new ChromeDriver();   }   else {      throw new IllegalArgumentException("Invalid browser value!!");   }   driver.get(URL);   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   driver.manage().window().maximize(); }

As previously indicated, the value of the browser variable will be provided to the test scripts via the testng.xml file. This testng.xml file will include a parameter tag with the browser variable and its value. We are developing two tests, one for Firefox and one for Chrome, each with distinct browser values. To understand the concept of test parameterization, see the following extract from the testng.xml file. Also, note that putting parallel="tests" in the Suite tag causes the two tests to execute in parallel.

<suite name="MultiBrowserSuite" parallel="tests" thread-count="2">   <test name="TestFirefox">      <parameter name="browser" value="Firefox"/>      <classes>         <class name="sampleTestPackage.MultiBrowserTest"/>      </classes>   </test>   <test name="TestChrome">      <parameter name="browser" value="Chrome"/>      <classes>         <class name="sampleTestPackage.MultiBrowserTest"/>      </classes>   </test> </suite>

When you run the test using testng.xml, it will run in parallel for all of the specified browsers (in our case, Firefox and Chrome).

About Author

Nidhi Ojha

Nidhi is certified in Manual Testing and SQL. She has done in Electronics and Communication branch, apart from this she loves to travel to different places.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us