Friday 15 August 2014

Selenium WebDriver Parallel Tests Execution Using TestNG - @Parameters




-> Browser compatibility testing Is most Important thing for any web application and generally you have to perform browser compatibility testing before 1 or 2 days of final release of application. In such a sort time period, you have to verify each Important functionality In every browsers suggested by client.


-> If you will go for running your all webdriver tests In each browsers one by one then It will take too much time to complete your test and you may not complete It before release.

-> In such situation, Running your tests In all required browsers at same time will helps you to save your time efforts.

-> The above situation solved in selenium by using @Parameters annotation in TestNG framework.



Parallelism In TestNG


We can configure our TestNG.xml file in such a way to run our test suite or tests or methods in seperate browsers Is known as parallelism In TestNG.


ParellelTestng.java


package TestNG_Package;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNG_Test1 {

 private  WebDriver driver1=null;

  @BeforeClass
  @Parameters ({"browser"})

  public void setup(String browser)
 {
   if (browser.equals("FFX"))
  {
     System.out.println("Test Starts Running In Firefox Browser.");
     driver1 = new FirefoxDriver(); 
    }
  else if (browser.equals("CRM"))
  {
     System.out.println("Test Starts Running In Google chrome.");
     System.setProperty("webdriver.chrome.driver",  "D:\\chromedriver_win32_2.3\\chromedriver.exe");
     driver1 = new ChromeDriver(); 
   }
   driver1.manage().window().maximize();
   driver1.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
  }


 @AfterClass
 public void closebrowser()
 {
    driver1.quit();
 }


 @Test
 public void MethodOne() {

  String a= "Selenium";
  if(a.equals("Selenium")
   throw new SkipException("Skipped");

  String title = driver1.getTitle();
  System.out.println("title"+title);   
 }



 @Test
 public void MethodTwo() {
  String title = driver1.getTitle();
  System.out.println("title"+title);   
 }


}




testng.xml

<suite name="webDriver" parallel="tests">

 <test name="Test In FireFox" >
    <parameter name="browser" value="FFX" />
    <classes>
      <class name="Testng_Pack.Test_Parallel" />
    </classes>
  </test>

   <test name="Test In Google Chrome" >
    <parameter name="browser" value="CRM"></parameter>
    <classes>
      <class name="Testng_Pack.Test_Parallel"></class>
    </classes>
  </test>

</suite>




From above example we can run same test in both Chrome and FireFox at same time.

From above Testng.xml file

-> parallel="tests" which inside <suite> tag will instruct TestNG to consider method of each <test> tag as seperate Thread. that Means If you wants to run your test In 2 different browsers then you need to create two <test> blocks for each browser Inside testng.xml file.

-> Inside each <test> tag block, define one more tag "parameter" with same name(In both <test> block) but with different values.Its used to pass in ParellelTestng.java method.



From ParellelTestng.java

-> Here we are used @Parameters annotation to pass parameter in method.Values of this parameter will be feed by testng.xml file.

-> If condition will check that value to decide which driver to use for test. This way, example testng.xml file will feed two values(FFX and CRM) In parameter so It will open Firefox and Google chrome browsers and run test In both browsers

1 comment:


  1. I am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.

    Top Software Testing Companies
    Top Security Testing Companies
    Top Mobile Testing Companies
    Top Test Automation Companies
    Top Performance Testing Companies
    Website testing services

    ReplyDelete