Parallel Execution in TestNG

Parallel Execution of test methods in TestNG

TestNG provides multiple ways to execute tests in separate threads. In testng.xml, if we set ‘parallel‘ attribute on the tag to ‘tests‘,  testNG will run all the ‘@Test’ methods in tag in the same thread, but each tag will be in a separate thread.

If we want to run methods/classes in separate threads, we need to set ‘parallel‘ attribute on the tag to ‘methods‘ / ‘classes

TestNG provides an ability to run test methods, test classes and tests in parallel. By using parallel execution, we can reduce the ‘execution time‘ as tests are started and executed simultaneously in different threads.

**A thread is an independent path of execution within a program. Every thread in Java is created and controlled by the java.lang.Thread class.

Let’s Consider the below example:

package pack1;

import org.testng.annotations.Test;

public class ParallelTestId {@Test
	public void testCaseOne() {

		System.out.println("Test Case One with Thread Id:- " + Thread.currentThread().getId());
	}@Test
	public void testCaseTwo() {

		System.out.println("Test Case two with Thread Id:- " + Thread.currentThread().getId());
	}
}

If you will run the above program, you will get the output as below.

[TestNG] Running:
  C:\Users\Sujoy\AppData\Local\Temp\testng-eclipse-1521962657\testng-customsuite.xml
Test Case One with Thread Id:- 1
Test Case two with Thread Id:- 1

From the above output you can observe that both the test methods are running under one thread. 
i.e. Under Thread id 1

If you want to run independent test methods in multiple threads, then you have to configure your testng.xml file as below.

We are defining two attributes ‘parallel‘ and ‘thread-count’ at suite level. As we want test methods to be executed in parallel, we have provided ‘methods’. And ‘thread-count’ attribute is to use to pass the number of maximum threads to be created.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
  <test name="Regression 1">
    <classes>
      <class name="pack1.ParallelTestId"/>
    </classes>
  </test>
</suite>

If you will run the above testng.xml file, you will get the output as below.

[TestNG] Running:
  E:\HardDisk\Sujoy\ SampleProject\testng.xml
Test Case One with Thread Id:- 11
Test Case two with Thread Id:- 12

The above result shows that two methods executed using different threads. When we run the same testng.xml again, the result may vary. The assigning of the thread is take care by the processor. So we can’t say which thread is going to execute which method.

Parallel Execution of test In multiple browsers using testNG

Selenium WebDriver does not have any feature which will allow us to perform parallel execution in multiple browsers. So with the help of TestNG we can perform parallel execution in selenium. 

This can be done by using @parameters annotation.

Let’s consider the below example:

package Testng_Pack;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Test_Parallel {

	private WebDriver driver = null;

	@BeforeClass
	//parameter value will retrieved from testng.xml file's <parameter> tag.
	@Parameters({
		"browser"
	})
	public void setupBrowser(String browser) { //Method will pass value of parameter.
		if (browser.equals("FFX")) { //If value Is FFX then webdriver will open Firefox Browser.
			System.out.println("Test Starts Running In Firefox Browser.");
			driver = new FirefoxDriver();
		}
		else if (browser.equals("CRM")) { //If value Is CRM then webdriver will open chrome Browser.
			System.out.println("Test Starts Running In Google chrome.");
			System.setProperty("webdriver.chrome.driver", "C:\\Users\\sujyonta.giri\\Downloads\\chromedriver.exe");
			driver = new ChromeDriver();
		}
		else if (browser.equals("IE")) { //If value Is ie then webdriver will open IE Browser.
			System.out.println("Test Starts Running In IE.");
			System.setProperty("webdriver.ie.driver", "C:\\Users\\sujyonta.giri\\Downloads\\IEDriverServer.exe");
			driver = new InternetExplorerDriver();
		}
		driver.manage().window().maximize();
		driver.get("http://newtours.demoaut.com/");
	}
	//Both bellow given tests will be executed In all the browsers.
	@Test
	public void verify_title() {
		String title = driver.getTitle();
		Assert.assertEquals("Welcome: Mercury Tours", title);
		System.out.println("Title Is Fine.");
	}@Test
	public void verify_message() {
		driver.findElement(By.name("userName")).sendKeys("UID1");
		driver.findElement(By.name("password")).sendKeys("pass1");
		driver.findElement(By.name("login")).click();
		driver.findElement(By.linkText("Home")).click();
	}@AfterClass
	public void closebrowser() {
		driver.quit();
	}
}

In the above code, we have setupBrowser method with @Parameters annotation along with parameter ‘browser‘. In the xml we will define three tests tags to run each test with different browser.

We will compare the browser value with the parameter value and based on that we will create the driver instance. We have now defined three tests with three browsers (Firefox, Google Chrome and Internet Explorer).

Testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<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> 
  <test name="Test In IE" >
    <parameter name="browser" value="IE"></parameter>
    <classes>
      <class name="Testng_Pack.Test_Parallel"></class>
    </classes>
  </test> 
</suite>

In the above testng.xml we have defined parallel=”tests” in suite tag. So all the browsers will run in parallel.

If you want to run the browsers one after another then simply remove parallel=”‘tests” from testng.xml file.

Leave a Reply

Your email address will not be published. Required fields are marked *