Cross Browser Testing with TestNG

Hola Testors!

Cross browser testing become important when you are executing automation script on regression. Because your customer may request to work the particular web application on different browsers. So you need to automate the application to run in different browsers. As with our current script it will support only one browser at a time. So you have to change your browser parameter and have to run it again and again. That is why cross browser concept came to picture. By using TestNG we can easily do cross browser testing. Let’s do it!

There are two ways to do cross browser testing;

  1. Sequential order
  2. Parallel order


First we look at how to do it on sequentially.

First Create a sample project (I’m creating sample maven project called crossbrowserTest )and apply all the dependancies.

Since this is TestNG, we have to include TestNG dependancy to our pom.xml.


You can download TestNG repositories or JAR from maven repository by clicking here.

Then I’m creating a class to test google home page in different browsers. So, I’m creating a java class called VerifyTitle under src/test/java

package com.daz.crossbrowserTest.crossbrowserTest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class VerifyTitle {
    WebDriver driver;
    static String CHROME_PATH="drivers/chromedriver";
    static String URL="";
    public void VerifyPageTitle(String BrowserName)
			driver=new FirefoxDriver();
		else if(BrowserName.equalsIgnoreCase("Safari"))
			driver=new SafariDriver();
		else if(BrowserName.equalsIgnoreCase("Chrome"))
			System.setProperty("", CHROME_PATH);
			driver=new ChromeDriver();
		System.out.println(BrowserName+" : "+driver.getTitle());


Make sure to create @Test and @Parameter annotations with TestNG

Screen Shot 2015-09-16 at 10.13.58 PM Screen Shot 2015-09-16 at 10.17.54 PM

Our class is ready now. What Next?

We have to pass the browser to our test class. That is the place we do cross browsing.

Right Click on Class > TestNG > Convert to TestNG.

It will display “Refactoring” dialog box. There you can give a any name for the xml (I’m giving crossbrowsing.xml) and click Finish

Screen Shot 2015-09-16 at 10.50.14 PM    Screen Shot 2015-09-16 at 10.50.39 PM

Open the crossbrowsing.xml and add tests to the suite as below

<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE suite SYSTEM "">
<suite name="Suite">
    <test name="FirefoxTest">
  	<parameter name="browser" value="Firefox"/>
  		<class name="com.daz.crossbrowserTest.crossbrowserTest.VerifyTitle">
  <test name="SafariTest">
  <parameter name="browser" value="Safari"/>
  	<class name="com.daz.crossbrowserTest.crossbrowserTest.VerifyTitle"/>
  <test name="ChromeTest">
  	<parameter name="browser" value="Chrome"/>
  		<class name="com.daz.crossbrowserTest.crossbrowserTest.VerifyTitle"/>

Here I explain what the <class name> in above xml. That is your test class name. You can give it as <package_name>.<class_name>.

Now you can run the test by right click on crossbrowsing.xml > Run As > TestNG Suite

Screen Shot 2015-09-16 at 11.09.44 PM

Since this is sequential, it will run according to the order of browsers giving on crossbrowsing.xml.

In our example it will Firefox, Opera and Chrome. Here I add console output and TestNG output

Screen Shot 2015-09-16 at 11.12.59 PM  Screen Shot 2015-09-16 at 11.13.09 PM


Now we can look how to do parallel test. Now it is easy 😉

Open crossbrowsing.xml file and do below change

<suite name="Suite" parallel="tests">

Save the xml

Right click on crossbrowsing.xml > Run As > TestNG Suite.

In this scenario all the browsers will open parallel. But you can see test results will differ from sequential order. Because fastest browser will finish the test first.  Here I attach my parallel test results as well.

Screen Shot 2015-09-16 at 11.18.29 PM    Screen Shot 2015-09-16 at 11.18.42 PM

You can find sample code for this project from my GitHub by clicking here

See you soon!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s