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.

Parallel Cross Browser Testing In Selenium Using TestNG

Example Java And Html Files

Parallel Cross Browser Testing In Selenium Using TestNG

    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. */	@FindBy(id="UserName")	private WebElement userName;	/* Map to password input text field. */	@FindBy(id="Password")	private WebElement password;	/* Map to submit button. */	@FindBy(id="submitBtn")	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) {		this.userName.sendKeys(userNameValue);	}	/* Set passwordValue in password input box.*/	private void setPassword(String passwordValue) {		this.password.sendKeys(passwordValue);	}	/* Click login button. */	public void login(String uerName, String password){		this.setUserName(uerName);		this.setPassword(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 = "";	@Test	public void testLogin() throws InterruptedException {		this.webDriver.get("file://C:/WorkSpace/");		Thread.sleep(3000);		LoginPagePOM loginPagePom = new LoginPagePOM(this.webDriver);		loginPagePom.login("Jerry", "");		Thread.sleep(3000);		System.out.println("Test " + browserType + " complete.");	}	@AfterTest	public void tearDown()	{		if(this.webDriver!=null)		{			this.webDriver.close();			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;		if("Firefox".equalsIgnoreCase(browserType))		{			this.webDriver = new FirefoxDriver();			this.webDriver.manage().window().maximize();		}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"/></form>
  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> <classes> <class name="com.dev2qa.webdriver.crossbrowser.CrossBrowserTestingWithTestNG"/> </classes> </test>  <test name="Test Chrome"> 	<parameter name="browserType" value="Chrome"></parameter> <classes> <class name="com.dev2qa.webdriver.crossbrowser.CrossBrowserTestingWithTestNG"/> </classes> </test>  <test verbose="2" name="Test InternetExplorer"> 	<parameter name="browserType" value="InternetExplorer"></parameter> <classes> <class name="com.dev2qa.webdriver.crossbrowser.CrossBrowserTestingWithTestNG"/> </classes> </test> </suite>

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

  1. Right click the TestNG java file, click ” Run As —> TestNG Test “.
    Parallel Cross Browser Testing In Selenium Using TestNG
  2. Refresh the project and right click index.html in test-output folder. Click ” Open With —> Web Browser “.
    Parallel Cross Browser Testing In Selenium Using TestNG
  3. Click below .xml link in left panel, copy the content in right panel.
    Parallel Cross Browser Testing In Selenium Using TestNG
  4. Click ” File —> New —> Others “, choose xml file in the popup dialog.
    Parallel Cross Browser Testing In Selenium Using TestNG
  5. Save the copied content in the file. Name the file testng-cross-browser.xml. Then edit the xml content as above example.
    Parallel Cross Browser Testing In Selenium Using TestNG
  6. Right click testng-cross-browser.xml, click ” Run As —> TestNG Suite”.
    Parallel Cross Browser Testing In Selenium Using TestNG
  7. Execution result reports.
    Parallel Cross Browser Testing In Selenium Using TestNG
READ :   WebDriver Event Listener : Take Screenshot On Exception

Download “” – Downloaded 199 times – 3 KB

  • 178