Multi Browser or Cross Browser Testing in Selenium
In this tutorial, we will be studying about multi-browser or cross-browser testing in Selenium along with its implementation. 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, note that multi-browser testing can be carried out using Selenium Grid as well. But here, we will be using TestNG and Selenium WebDriver only for implementing multi-browser testing.
Content
What is Cross Browser Testing?
Multi browser testing or cross-browser testing is a type of testing in which the application under test is tested with multiple supported browsers. The need for multi-browser testing arises from the fact that different browsers have different UI implementations. So, one cannot ensure that applications running on Chrome will also run on IE without any issues.
Cross Browser Testing in Selenium and TestNG
Cross browser testing in Selenium can be carried out 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 browser to the test scripts from the testng.xml file.
The value of the browser parameter can then be used to instantiate the corresponding driver class of Selenium WebDriver. As the browser value is used across all the test methods so, it is better to use the browser variable in @BeforeTest method.
The beforeTest method with @Parameter of TestNG will look like-
@Parameters("browser")
@BeforeTest
public void setBrowser(String browser)
{
if (browser.equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver",
+ 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 stated earlier, the value of the browser variable will be passed to the test scripts through testng.xml file. This testng.xml file will have a parameter tag containing the browser variable and its value.
Here, we will create two tests, one each for Firefox and Chrome browser with different browser values. Refer to the below snippet of testng.xml file to understand the test parameterization concept.
Also, note that having parallel=”tests” in the Suite tag makes the two tests run in parallel.
On executing the test using testng.xml, the test will run in parallel for all the specified browsers(Firefox and Chrome in case of our example).