Cross Browser Testing(Selenium and TestNG)

Murat Feyzioğlu
Beyn Technology
Published in
5 min readSep 29, 2022

Multi-browser testing or cross-browser testing is an essential requirement for your test process. We need Selenium to test websites that check browser drivers. For using Selenium efficiently, we need TestNG. We will do the Cross Browser Testing process using TestNG with Selenium.

What is Cross-Browser Testing?

Cross-browser testing is the process of testing our website on different browsers and operating systems. With cross-browser testing, we ensure that the site is the same in every browser. We can perform cross-browser testing automatically. When cross-browser testing, we care not only about browsers but also their different versions and operating systems.

Why is Cross Browser Testing important?

Browser vendors follow Open Web Standards but have their interpretations. Because each renders HTML, CSS, and JavaScript in unique ways, extensive debugging of a website’s source code isn’t enough to make the site look and behave as intended in different browsers. So it’s up to web developers to abstract away browser differences. Cross-browser compatibility testing helps locate browser-specific compatibility errors so they can be quickly debugged. It helps to ensure that a site does not alienate a significant portion of its target audience because the website does not work on browser operating systems.

By this point, you should have understood that the idea of ​​cross-browser testing is due to the different engines we install the browsers on to render web languages ​​.

Cross Browser Testing using Selenium

In the code below, we will demonstrate how to perform cross-browser testing in TestNG using the Selenium web driver.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.chrome.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MultiBrowserTesting {

public WebDriver driver;

@Parameters("browser")

@BeforeClass
public void beforeTest(String browser) {

// If the browser is Opera
if(browser.equalsIgnoreCase("opera")) {

//Initializing the opera driver
driver = new OperaDriver();
// If the browser is Chrome
}else if (browser.equalsIgnoreCase("chrome")) {

//Initialize the chrome driver
driver = new ChromeDriver();
}

// Website address in the browser
driver.get("https://www.example.com");

}

// Once Before method is completed, the Test method will start
@Test public void tests() throws InterruptedException {

driver.findElement(By.xpath("example[div]")).click();
}
@AfterClass public void afterTest() {
driver.quit();
}
}

Since we are using the TestNG parameters, we need to specify the values ​​in the TestNG XML file that will pass to the test case file.

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="none">
<test name="OperaTest">
<parameter name="browser" value="opera" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
<test name="ChromeTest">
<parameter name="browser" value= "chrome" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
</suite>

The above piece of code does the following;

  • Initialize a parameter with the browser name.
  • Start the scanner driver depending on the parameter value. For a browser value equal to Chrome, start a chrome driver, etc.
  • Open the “example.com” website in the browser and click on an item with the help of Selenium.

In this XML file, the code specifies different classes for drivers to instantiate browsers to run test cases on the website.

After understanding the above code, run the code in the XML file as mentioned above with parameter values ​​like Opera and Chrome.

What are the benefits of cross-browser testing?

  • Different browsers offer the same user experience.
  • Browser-specific functionality does not affect the end-user experience.
  • The website maintains its performance even as new device and operating system combinations are introduced.
  • Each browser responds acceptably to changes in features or functionality.

What are the advantages of cross-browser testing?

Concurrent Test Execution Results in Faster Testing

The biggest challenge posed by automation testing is scalability. The need to run each test on all possible combinations of different browsers, different versions, operating systems, different versions, devices, and platforms makes it a tedious and time-consuming task. What makes it worse is that every time there is a new build or fix that requires testing, your testing team will have to rerun all those tests on all these browser combinations.

You can easily reduce this difficulty by leveraging automation. Automatic cross-browser tests help you run parallel tests quickly and simultaneously across combinations, resulting in much faster delivery.

Save Time and Money

Cross-browser testing often requires mundane and repetitive testing, which can be a tedious and time-consuming task if done manually. By identifying and automating all common tests that need to be repeated across all browser combinations, you can not only save time but also improve return on investment (ROI).

Higher Test Coverage

One of the biggest challenges of cross-browser testing is the need to run and re-run tests across many different browsers, platforms, and device combinations. Oftentimes, test teams are short on time and have to skip some of these combinations for lack of time.

More Accurate Test Results

With automation, human errors can be completely avoided. This can result in improved test accuracy as your test automation tool is free of human emotions such as boredom, fatigue, or stress that result in frequent surveillance.

Get More Testing Done With Fewer Resources

This can be a general benefit in any type of test automation, but it also applies to cross-browser testing. By using test automation to run your cross-compatibility tests, you will need far fewer testers than you would if run manually. This helps in faster test execution that not only scales but also saves time and money.

--

--