TestNG -19 || Concept of Parallel Execution.

The fundamental concept of parallel execution is based on the fact that each process should have its own processor.

To understand this observe the two different scenarios in the below image:

Concept of Parallel Execution

Scenario – 1: We have three containers and one truck to move from Source to Destination. If it takes 1 hour to cover the distance between Source and Destination we require more than 3 hours (due to round trips and sequence execution) to move all the container.

Scenario – 2: Here we have three containers with three trucks to move from Source to Destination. So, it will take only 1 hour to move all the containers in one go(as all trucks will go parallel). So we can see that here time is reduced by 3 times.

Parallel Execution for ‘tests’

While programming containers would be the Processes(tests, methods, classes etc). And trucks are the Threads which handles the process. So, the best practice is to keep the number of processes and threads the same for better performance.

Let’s revisit the example from the previous chapter, where we have already created a program to launch multiple browsers. If you haven’t gone through with that please visit below link:

MultiBrowserExample.java

package MultiBrowserExecution;
import java.awt.RenderingHints.Key;

import org.omg.PortableServer.THREAD_POLICY_ID;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
/**
 * @author ashok.kumar
 *
 */
public class MultiBrowserExample 
{
	WebDriver driver;
	
	@BeforeClass
	@Parameters("browserName")
	public void launchBrowser(String targetBrowser)
	{
		System.out.println("launchBrowser Thread ID: "+Thread.currentThread().getName()+" "
													+Thread.currentThread().getId());
		if(targetBrowser.equalsIgnoreCase("chrome"))
		{
			System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
			driver=new ChromeDriver();
			System.out.println("Chrome Browser Launched...");
		}
		else if(targetBrowser.equalsIgnoreCase("ff"))
		{
			System.setProperty("webdriver.gecko.driver", "c://geckodriver.exe");
			driver=new FirefoxDriver();
			System.out.println("Firefox Browser Launched...");
		}
		else if (targetBrowser.equalsIgnoreCase("edge"))
		{
			System.setProperty("webdriver.edge.driver", "c://MicrosoftWebDriver.exe");
			driver=new EdgeDriver();
			System.out.println("Edge Browser Launched...");
		}
		else
		{
			System.out.println("Invalid Browser Name...");
		}
	}
	
	@Test
	public void launchURL()
	{
		System.out.println("launchURL Thread ID: "+Thread.currentThread().getName()+" "
				+Thread.currentThread().getId());
		driver.get("https://www.google.com");
		System.out.println("URL launched...");
	}
	
	@Test
	public void searchKey()
	{
		System.out.println("searchKey Thread ID: "+Thread.currentThread().getName()+" "
				+Thread.currentThread().getId());
		String keyword="allinoneblogs";
		driver.findElement(By.name("q")).sendKeys(keyword+Keys.ENTER);
		System.out.println("Keyword search successfully.");
	}
}

MultiBrowserParallelXML.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="MultiBrowser" parallel="tests" thread-count="3">
	<test name="Test1">
	<parameter name="browserName" value="chrome"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
	<test name="Test2">
	<parameter name="browserName" value="ff"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
	<test name="Test3">
	<parameter name="browserName" value="edge"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
</suite>

Result Analysis

  • Class files contain methods to
    • launch any browser(based on the parameter passed by individual <test> tag)
    • launch the URL
    • Search any keyword.
  • In this example, we have three <test> tags in the XML file.
    • <test name=”Test1″>
    • <test name=”Test2″>
    • <test name=”Test3″>

So, we have taken parallel=”tests” thread-count=”3″ to handle each test via individual threads. As soon as we run the program we observe that three threads get created and all the three browsers launched in one go.

Also, the remaining activity gets performed simultaneously on all browsers rather than sequentially.

impontant: Change the value of thread-count=”2″ and re-execute the program. You will observe the change as now only 2 threads get created which are handling all the three tests from the XML file.

Console output for Parallel execution while using tests.

Parallel Execution for ‘methods’

The process will remain the same. This will execute different test methods simultaneously. These methods have been defined using @Test annotation in various class files. To understand this we will take two class file as mentioned below:

  • SimpleExample.java
    • method1()
    • method2()
  • SimpleExample_2.java
    • method3()
    • method4()
  • ParallelExecution_Sample.XML
    • parallel=”methods”
    • thread-count=”3″

SimpleExample.java

package ParallelExecution;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class SimpleExample 
{
	
	@Test
	public void method1()
	{
		System.out.println("method1 - ThreadName >> " 
                                +Thread.currentThread().getName()
				+" Thread ID >>"+Thread.currentThread().getId());
	}
	
	@Test
	public void method2()
	{
		System.out.println("method2 - ThreadName >> " 
                                +Thread.currentThread().getName()
				+" Thread ID >>"+Thread.currentThread().getId());
	}
}

SimpleExample_2.java

package ParallelExecution;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class SimpleExample_2 {
	
	@Test
	public void method3()
	{
		System.out.println("method3 - ThreadName >>" 
                               +Thread.currentThread().getName() 
                             +" Thread ID >> "+Thread.currentThread().getId());
	}
	
	@Test
	public void method4()
	{
		System.out.println("method4 - ThreadName >>" 
                                +Thread.currentThread().getName()
                               +" Thread ID >> "+Thread.currentThread().getId());
	}
}

ParallelExecution_Sample.XML

<?xml version="1.0" encoding="UTF-8"?>
<suite name="ParallelExecution_Sample" parallel="methods" thread-count="3">
	<test name="sampletest">
		<classes>
			<class name="ParallelExecution.SimpleExample"></class>
			<class name="ParallelExecution.SimpleExample_2"></class>
		</classes>
	</test>
</suite>

Result Analysis

We can see 3 threads get started in one go with ID 12, 13, and 14. These threads start execution of method1, method2, and method3. As soon as the execution of the first thread completed, execution of method4 gets started using ID 12.

In the second screenshot observe that start time for method1, method2 and method3 is 0 millisecond. And method4, start time is 14 millisecond.

To understand the use and usage of Parllel execution remove attributes parallel=”methods” thread-count=”3″ from the XML file and run the program.

Console Output while using methods.
Chronological view while using methods.

Parallel Execution for ‘classes’

To use “classes” for parallel execution, we just need to change the values in the XML file as parallel=”classes” thread-count=”2″. This will execute one method from each class simultaneously.

All the files will remain the same. Just update the XML file as mentioned below:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="ParallelExecution_Sample" parallel="classes" thread-count="2">
	<test name="sampletest">
		<classes>
			<class name="ParallelExecution.SimpleExample"></class>
			<class name="ParallelExecution.SimpleExample_2"></class>
		</classes>
	</test>
</suite>

Result Analysis

Console Output while using classes.
Chronological view while using classes.

TestNG:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


485total visits,3visits today

Leave a Reply

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