Thursday 30 August 2018

Multi Browser or Cross Browser Testing with TestNG

It is very important to test your web application across multiple browser. Each browser as different way of processing the code and hence user experience might differ from browser to browser as a tester it is our responsibility to make sure our application works fine on most commonly used browsers such as firefox, chrome and IE. 
TestNG allows us to automate the multi-browser testing using TestNG parameters. In this post we will see in detail how we can achieve this withe simple example.

Example.

1. Create a simple test script.

2. Configure it to pass Browser Type as a parameter.

package test;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.firefox.FirefoxDriver;
 
import org.openqa.selenium.ie.InternetExplorerDriver;
 
import org.testng.annotations.AfterClass;
 
import org.testng.annotations.BeforeClass;
 
import org.testng.annotations.Parameters;
 
import org.testng.annotations.Test;
 
public class MultiBrowserTest {
 
 public WebDriver driver;
 
  @Parameters("browser")
 
  @BeforeClass
 
  // Passing Browser parameter from TestNG xml
 
  public void beforeTest(String browser) {
 
  // If the browser is Chrome, then do this
 
  if(browser.equalsIgnoreCase("chrome")) {
 
   System.setProperty("webdriver.ie.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
   
   driver = new InternetExplorerDriver();;
 
  // If browser is IE, then do this   
 
  }else if (browser.equalsIgnoreCase("ie")) { 
 
   // Here I am setting up the path for my IEDriver
 
   System.setProperty("webdriver.ie.driver", "D:\\QA\\drivers\\IEDriverServer.exe");
 
   driver = new InternetExplorerDriver();
 
  } 
 
  // Doesn't the browser type, lauch the Website
 
  driver.get("http://www.qaautomated.com"); 
 
  }
 
  // Once Before method is completed, Test method will start
 
  @Test 
  public void search() throws Exception {
    
   Thread.sleep(5000);
      driver.findElement(By.xpath("*//input[@class='search-field']")).sendKeys("appium");
      
      driver.findElement(By.xpath("*//input[@class='search-submit']")).click();
 
      driver.quit();
 
 
 }  
 
  @AfterClass public void afterTest() {
 
  driver.quit();
 
 }
 
}

3. Mention browser type details in testng.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="ChromeTest">
 
 <parameter name="browser" value="chrome" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test>
 
 <test name="IETest">
 
 <parameter name="browser" value="ie" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test> <!-- Test -->
</suite> <!-- Suite -->

4. Run it as TestNG Suit.

Parallel Execution - Consider you want to execute your tests in parallel on multiple browser then with a small change to your testng.xml you achieve this


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
  <test name="ChromeTest">
 
 <parameter name="browser" value="chrome" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test>
 
 <test name="IETest">
 
 <parameter name="browser" value="ie" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test> <!-- Test -->
</suite> <!-- Suite -->

.

6 comments: