Thursday, June 19, 2014

How to execute single testcase in two different browsers parallel in single machine using TestNG.

Sample Test Case

package hello;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelExecution {
        @Test
        @Parameters("browserType")  // use @Parameters to pass the input through xml
        public void dummyTest(String browserType){
            WebDriver driver;
            if(browserType.equals("FF")){
                driver= new FirefoxDriver();
                driver.quit();
            }else if(browserType.equals("IE")){
                System.setProperty("webdriver.ie.driver", "./Drivers/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                driver.quit();
            }
     }
}


testng.xml

<suite name="Suite" parallel="tests" thread-count="2"> //here parallel="tests" means run both the test in parallel and thread-count="2" means run two test cases parallel. 
  <test name="Test1">
  <parameter name="browserType" value="FF" />  //to pass the input to the java class
   <classes>
          <class name="hello.ParallelExecution"/>   //here hello is package name and ParallelExecution is class name
   </classes>
  </test>
  <test name="Test2">
  <parameter name="browserType" value="IE" />
   <classes>
          <class name="hello.ParallelExecution"/> //here hello is package name and ParallelExecution is class name
   </classes>
  </test>
</suite> 

No comments:

Post a Comment