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 methods, such as Run Selenium Testing Script In Cloud, set up 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.

1. Example Java And Html Files.

  1. Below are the source files of this example.
    C:\WORKSPACE\WORK\DEV2QA.COM-EXAMPLE-CODE\JAVACOREEXAMPLEPROJECT\SRC\COM\DEV2QA\WEBDRIVER\CROSSBROWSER
        CrossBrowserTestingWithTestNG.java
        index.html
        login.html
        LoginPagePOM.java
        testng-cross-browser.xml
  2. LoginPagePOM.java
    This is a POM (Page Object Model ) class that contains all web elements and actions that take place on the 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.submitBtn.click();
    	}
    }
  3. CrossBrowserTestingWithTestNG.java: This is the test class that will run testLogin() method. Please note the setup(String browserType) method. It has two TestNG annotations @BeforeTest and @Parameters. The browserType parameter’s value is defined in testng-cross-browser.xml file.
    private WebDriver webDriver = null;
      
    private String browserType = "";
    @Test
    public void testLogin() throws InterruptedException {
    		
    	this.webDriver.get("file://C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/crossbrowser/login.html");
    		
    	Thread.sleep(3000);
    		
    	LoginPagePOM loginPagePom = new LoginPagePOM(this.webDriver);
    		
    	loginPagePom.login("Jerry", "dev2qa.com");
    		
    	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\\dev2qa.com\\Lib\\chromedriver_win32\\chromedriver.exe";
    		 //Assign chromeDriverPath to system property "webdriver.chrome.driver"
    		 System.setProperty("webdriver.chrome.driver", chromeDriverPath);
    		 //Initiate a new instance
    		 this.webDriver = new ChromeDriver();
    	}else if("InternetExplorer".equalsIgnoreCase(browserType))
    	{
    		 String ieDriverFilePath = "C:\\Workspace\\dev2qa.com\\Lib\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe";
    		 //Specify the executable file path to sysem property.
    		 System.setProperty("webdriver.ie.driver", ieDriverFilePath);
    		 //Initiate web browser
    		 this.webDriver = new InternetExplorerDriver();
    	}
    }
  4. login.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Login to dev2qa.com demo system</title>
    </head>
    <body>
    <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>
    </body>
    </html>
  5. index.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Index page of dev2qa.com demo system</title>
    </head>
    <body>
    You have login to dev2qa.com demo system successfully.
    </body>
    </html>
  6. testng-cross-browser.xml: This is the TestNG test 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 the suite XML root element.
    2) The name and value attribute of the parameter XML element. They will be used in CrossBrowserTestingWithTestNG.java.

    <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>

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

  1. Right-click the TestNG java file, click ” Run As —> TestNG Test “.
  2. Refresh the project and right-click index.html in the test-output folder. Click ” Open With —> Web Browser “.
  3. Click ” File —> New —> Others “, choose XML / XML File in the popup dialog to create a new XML file.
  4. Name the XML file testng-cross-browser.xml. Then copy the below XML content to the file.
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <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>
  5. Right-click testng-cross-browser.xml, click ” Run As —> TestNG Suite” to execute the test suite.
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

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

Hi,
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 loginTest.java in another file. Could you please help me ?

1
0
Would love your thoughts, please comment.x
()
x