Cross browser testing using TestNG framework?

Let us understand first what is cross browser testing.
Whenever the application is developed, it is intended to use on most of the web browser. So, while testing we need to make sure that the application works all and all for major browsers like IE, firefox, Chrome, Safari etc. To test the application over multiple browsers is known as cross browser testing.
In an automation framework, there are two ways to pass browser name which will decide on which browser our test should run.
            1.      To pass the browser name / value from external file like properties file or excel file
            2.      To pass the browser name / value from testing.xml file.
Let us understand how can we pass the browser value from testing.xml here.
Below piece of code accepts the parameter form testing.xml
In Testng.xml, browser values are given.

package crossBrowserTests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

public class CrossBrowserTestFromTestNG {

WebDriver driver;

public void lanuchBrowser(String browser){

if (browser.equalsIgnoreCase("firefox")){

driver = new FirefoxDriver();
System.out.println("Browser selected for testing is : Firefox");

else if (browser.equalsIgnoreCase("chrome")){
System.setProperty("", "C:chromedriver.exe");

driver = new ChromeDriver();
System.out.println("Browser selected for testing is : Google Chrome");

else if (browser.equalsIgnoreCase("ie")){
System.setProperty("", "C:IEDriverServer.exe");
driver = new InternetExplorerDriver();
System.out.println("Browser selected for testing is : IE");

public void Test(){

System.out.println("Title of the webpage is: "+driver.getTitle());

public void closeBrowser(){


Sample Testng.xml (where browser value is supplied)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="Default suite">
<test verbose="2" name="FireFox Test">
<parameter name="browser" value="firefox"></parameter>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</test> <!-- Default test -->
<test verbose="2" name="Chrome Test">
<parameter name="browser" value="chrome"></parameter>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</test> <!-- Default test -->
<test verbose="2" name="Internet Explorer Test">
<parameter name="browser" value="ie"></parameter>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
When you run testing.xml, it will run 3 tests. A similar test in 3 different browsers (firefox, ie, chrome).
Hope this helps !!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

Get the Framework code at Github Repo: