Executing Selenium WebDriver Test Classes Parallel Using TestNG

Sometimes you need to execute multiple test cases at same time to save test execution time. TestNG has very good feature to execute test cases/classes In parallel to each other means you can execute two different test cases In two different windows of same browser simultaneously. If you remember, We have already learnt how to execute two different tests In two different browsers(Example : Firefox and Google Chrome) simultaneously In THIS POST.

Now let us try to Implement test classes parallel execution practically. Supposing I have two calc test classes as Test_One.java to sum values and Test_Two.java to subtract values as bellow. Each having two different @Test methods.

1. Test_One.java
package Testing_Pack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_One {
 WebDriver driver;
 WebElement dragElementFrom;

 @BeforeTest
 public void setup() throws Exception {
  System.out.println("In @BeforeTest Of Test_One.");
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
 }
 
 @Test(priority=1)
 public void testCaseOne_Test_One() {
  System.out.println("Executing testCaseOne_Test_One.");
  driver.findElement(By.xpath("//input[@id='2']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of testCaseOne_Test_One = "+Result);
 }
 
 @Test(priority=2)
 public void testCaseTwo_Test_One() {
  System.out.println("Executing testCaseTwo_Test_One.");
  driver.findElement(By.xpath("//input[@id='Resultbox']")).clear();
  driver.findElement(By.xpath("//input[@id='3']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='7']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of testCaseTwo_Test_One = "+Result);
 }
}


2. Test_Two.java
package Testing_Pack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_Two {
 WebDriver driver;
 WebElement dragElementFrom;
 
 @BeforeTest
 public void setup() throws Exception {
  System.out.println("In @BeforeTest Of Test_Two.");
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
 }
 
 @Test(priority=1)
 public void testCaseOne_Test_Two() {
  System.out.println("Executing testCaseOne_Test_Two.");
  driver.findElement(By.xpath("//input[@id='2']")).click();
  driver.findElement(By.xpath("//input[@id='minus']")).click();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of testCaseOne_Test_Two = "+Result);
 }
 
 @Test(priority=2)
 public void testCaseTwo_Test_Two() {
  System.out.println("Executing testCaseTwo_Test_Two.");
  driver.findElement(By.xpath("//input[@id='Resultbox']")).clear();
  driver.findElement(By.xpath("//input[@id='7']")).click();
  driver.findElement(By.xpath("//input[@id='minus']")).click();
  driver.findElement(By.xpath("//input[@id='3']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of testCaseTwo_Test_Two = "+Result);
 }
}

I wants to execute above given selenium webdriver test classes simultaneously. For that, I need to prepare testng.xml file as bellow.


testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Class Suite" parallel="classes" thread-count="2">
  <test name="Parallel Class Test" >
    <classes>
      <class name="Testing_Pack.Test_One"/>
      <class name="Testing_Pack.Test_Two"/>
    </classes>
  </test>
</suite>

In above testng.xml file, I have used parallel="classes" that means testng will execute both test classes(Test_One.java and Test_Two.java) In two different thread/browser window. thread-count="2" parameter describes maximum number of threads allowed In execution at same time. Run above example and observe console result at the end of execution. It will looks like bellow.


As per console result, Both test classes executed parallel In two different threads.

4 comments:

  1. Nice Post and useful. Thank you so much.

    ReplyDelete
  2. Its not working for me.org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms

    ReplyDelete
  3. Hi, If i keep thread-count value greater than 2 in testng xml file code then will it effect anything in script execution??
    Is it thread count equal to number of tests or classes that we want to run??
    Could anyone answer plz..

    ReplyDelete