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

.

14 comments:

  1. Click Here Best RPA Online Training to go to RPA Online Training Institute Details .

    ReplyDelete
  2. myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

    ReplyDelete
  3. Your Blog is awsem. I also find a blog Gk Questions and Answers which is also awsem

    ReplyDelete
  4. Attend The Analytics Course in Bangalore From ExcelR. Practical Analytics Course in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Analytics Course in Bangalore.
    ExcelR Analytics Course in Bangalore

    ReplyDelete
  5. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    ExcelR Business Analytics Course

    ReplyDelete
  6. Attend The PMP Certification in Abu Dhabi From ExcelR. Practical PMP Certification in Abu Dhabi Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP Certification in Abu Dhabi.
    ExcelR PMP Certification in Abu Dhabi

    ReplyDelete
  7. I am sure that this is going to help a lot of individuals. Keep up the good work. It is highly convincing and I enjoyed going through the entire blog.
    Data Science Training

    ReplyDelete
  8. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job ExcelR Business Analytics Courses

    ReplyDelete