Parallel Cross Browser Testing In Selenium Using TestNG

Cross Browser Testing in selenium is used to check whether your web application works correctly or not in various web browsers (Chrome, Firefox, Internet Explorer etc). You can perform this functional test through various method, such as Run Selenium Testing Script In Cloud, setup selenium grid and each node test one web browser. But they are so complex. In this article, i will show you how to use TestNG to run cross browser testing in parallel.

cross browser testing selenium

Example Java And Html Files

cross browser testing testng java and html files

    This is a POM (Page Object Model ) class that package all web elements and actions take place in login.html page.

    /* This pom class encapsulate all web element and actions for login.html page. */
    public class LoginPagePOM {
    	/* Webdriver object is used to operate web element in login page. */
    	private WebDriver webDriver = null;
    	/* Map to username input text field. */
    	private WebElement userName;
    	/* Map to password input text field. */
    	private WebElement password;
    	/* Map to submit button. */
    	private WebElement submitBtn;
    	/* Login page class constructor. The parameter is transfered from out side where call this class.*/
    	public LoginPagePOM(WebDriver webDriver) {
    		this.webDriver = webDriver;
    		/* This page factory method will initialize all the web elements.*/
    		PageFactory.initElements(webDriver, this);
    	/* Set usernameValue in username input box.*/
    	private void setUserName(String userNameValue) {
    	/* Set passwordValue in password input box.*/
    	private void setPassword(String passwordValue) {
    	/* Click login button. */
    	public void login(String uerName, String password){
    This is the test class which will run testLogin() mehtod. Please note setup(String browserType) method. It has two TestNG annotations. @BeforeTest and @Parameters. The browserType parameter’s value is defined in testng-cross-browser.xml.

    	private WebDriver webDriver = null;
    	private String browserType = "";
    	public void testLogin() throws InterruptedException {
    		LoginPagePOM loginPagePom = new LoginPagePOM(this.webDriver);
    		loginPagePom.login("Jerry", "");
    		System.out.println("Test " + browserType + " complete.");
    	public void tearDown()
    			this.webDriver = null;
    	@BeforeTest /* @BeforeTest annotated method will run before the first test case defined in testng.xml. */
    	@Parameters("browserType") /* browserType parameter is test case parameter defined in testng.xml. */
    	public void setup(String browserType) 
    		this.browserType = browserType;
    			this.webDriver = new FirefoxDriver();
    		}else if("Chrome".equalsIgnoreCase(browserType))
    			 //Set string variable value to Chrome Driver executable file path.
    			 String chromeDriverPath = "C:\\Workspace\\\\Lib\\chromedriver_win32\\chromedriver.exe";
    			 //Assign chromeDriverPath to system property ""
    			 System.setProperty("", chromeDriverPath);
    			 //Initiate a new instance
    			 this.webDriver = new ChromeDriver();
    		}else if("InternetExplorer".equalsIgnoreCase(browserType))
    			 String ieDriverFilePath = "C:\\Workspace\\\\Lib\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe";
    			 //Specify the executable file path to sysem property.
    			 System.setProperty("", ieDriverFilePath);
    			 //Initiate web browser
    			 this.webDriver = new InternetExplorerDriver();
  3. login.html and index.html
    These are the html page for this example, you can download the attached file to see the detail source code.

    <form action="index.html" method="post">
    UserName : <input type="text" value="" id="UserName"/><br/>
    Password : <input type="password" value="" id="Password"/><br/>
    <input type="submit" value="Login" id="submitBtn"/><input type="reset" value="Clear" id="resetBtn"/>
  4. testng-cross-browser.xml
    This is the testng suite xml configuration file. It includes three test cases, each one will use a different web browser. Please note below xml element and their attribute’s value.
    1) Thread-count and parallel attribute of suite xml element.
    2) Xml node parameter’s name and value attribute. They will be used in

    <suite guice-stage="DEVELOPMENT" name="Cross Browser Test Suite" thread-count="3" parallel="tests">
      <test name="Test Firefox">
      	<parameter name="browserType" value="Firefox"></parameter>
          <class name="com.dev2qa.webdriver.crossbrowser.CrossBrowserTestingWithTestNG"/>
      <test name="Test Chrome">
      	<parameter name="browserType" value="Chrome"></parameter>
          <class name="com.dev2qa.webdriver.crossbrowser.CrossBrowserTestingWithTestNG"/>
      <test verbose="2" name="Test InternetExplorer">
      	<parameter name="browserType" value="InternetExplorer"></parameter>
          <class name="com.dev2qa.webdriver.crossbrowser.CrossBrowserTestingWithTestNG"/>

How To Create testng-cross-browser.xml In Eclipse

  1. Right click the TestNG java file, click ” Run As —> TestNG Test “.
    run testng class test methods
  2. Refresh the project and right click index.html in test-output folder. Click ” Open With —> Web Browser “.
    output folder for testng test report
  3. Click below .xml link in left panel, copy the content in right panel.
    testng.xml content
  4. Click ” File —> New —> Others “, choose xml file in the popup dialog.
    create a new xml file
  5. Save the copied content in the file. Name the file testng-cross-browser.xml. Then edit the xml content as above example.
    content of testng-cross-browser.xml
  6. Right click testng-cross-browser.xml, click ” Run As —> TestNG Suite”.
    run testng test suite by xml
  7. Execution result reports.
    test report for cross browser testing selenium

[download id=”1763″]

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest Most Voted
Inline Feedbacks
View all comments
Ranjit Ray
Ranjit Ray

very good explanation. I have browser setup in one file and test in another. How can i do that. Like browser setup() in Baseclass and in another file. Could you please help me ?

Would love your thoughts, please comment.x