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

  1. LoginPagePOM.java
    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.submitBtn.click();
    	}
    }
  2. CrossBrowserTestingWithTestNG.java
    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/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();
    		}
    	}
  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.
    login.html

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

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
READ :   How To Select The Effective XPath For Web Element In Webdriver

Download “Cross-Browser-Testing-Example-Code.zip” Cross-Browser-Testing-Example-Code.zip – Downloaded 94 times – 3 KB

(Visited 1,633 times, 1 visits today)

Leave a Reply

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

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