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:

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.

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.


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


TestNG:
- TestNG – 1 || Introduction and benefits of TestNG Framework.
- TestNG – 2 || Installation process and a sample program of TestNG.
- TestNG – 3 || Create and execute multiple Test Cases.
- TestNG – 4 || Let’s understand @Test Annotation and attributes.
- TestNG – 5 || Understand Assertion in TestNG.
- TestNG – 6 || Use of @BeforeMethod and @AfterMethod.
- TestNG – 7 || Use of @BeforeClass and @AfterClass.
- TestNG – 8 || Creation and execution of Test Suites.
- TestNG – 9 || Let’s move deep into the Test Suites.
- TestNG – 10 || Use @BeforeTest and @AfterTest Annotations.
- TestNG – 11 || Groups attribute with @Test Annotation.
- TestNG – 12 || Use of @BeforeGroups & @AfterGroups.
- TestNG – 13 || Use of @BeforeSuite & @AfterSuite.
- TestNG – 14 || DataProvider annotation & attribute.
- TestNG – 15 || DataProvider with parameters.
- TestNG – 16 || Access data from Excel sheet using DataProvider.
- TestNG – 17 || Passing multiple Parameters in testng xml.
- TestNG – 18 || Multiple Browser and Parallel Execution in TestNG.
- TestNG -19 || Concept of Parallel Execution.
- TestNG – 20 || Run TestNG Program using main() method.
Computer Basics:
- Computer Basics -1 || Introduction and Structure of Computer.
- Computer Basics -2 || Types of Computers and Usage.
- Computer Basics -3 || What are the different types of Software?
- Computer Basics -4 || Importance of Operating System(OS).
- Computer Basics -5 || Understanding of Number System.
- Computer Basics -6 || Understanding MS-DOS Commands.
- Computer Basics -7 || Important Features of MS-Word.
- Computer Basics -8 || Let’s learn the usage of MS-Excel.
- Computer Basics -9 || Understand and Implement Data Validation in Excel.
- Computer Basics -10 || How to apply Filter in a data set in Excel?
- Computer Basics -11 || Using Charts in place of Data Tables in Excel.
- Computer Basics -12 || Advantages of PivotCharts over Simple Charts in Excel.
- Computer Basics -13 || Creating pivot charts/tables in Excel.
- Abbreviations to Full-Forms in Computer Basics.
Java Basics:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
- File Handling | Reading data from word document(.doc or .docx) in JAVA.
- File Handling | Reading data from Excel files (.xls or .xlsx) using JAVA.
- File Handling | Writing data into an Excel(.XLSX or .XLS) File.
- File Handling | Implement formatting in Excel using Java.
- File Handling | Copy existing data from one workbook to another workbook in Java.
- File Handling | Reading data from PDF file using JAVA.
- File Handling || Traverse folders and subfolders in Java.
- File Handling || Reading and Writing data from a text file.
- File Handling || Multiple file creation using template based input data.
- Framework || Simple example of Key Driven Framework using excel sheet in Selenium(JAVA).
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
- Different ways to Reverse a String in Java.
- Copy formatting & style of cells from one sheet to another.
- Getting IP address and Hostname using InetAddress Class.
- User inputs via Command Prompt using arguments of main() method of a class.
- Program for List and ArrayList in Java.
- Useful methods and implementation under Scanner Class.
- Swapping two variable values without using any third variable.
- Difference between int x= 10 and y=010 in Java.
- Parameterized Constructors v/s Setter and Getter function in JAVA.
- Override a Static Method.
- Selenium-12 || Select Class to handle drop-down.
- Selenium-11 || Use of Actions and Action Classes.
- Selenium-10 || Taking Screenshots using Selenium
- Selenium-9 || Understanding WebDriver API.
- Selenium-8 || Implementing Wait(s) in Selenium.
- Selenium-7 || Let’s learn to create complex XPath.
- Selenium-6 || XPath is the best way to locate web elements.
- Selenium-5 || Locating web elements using various type of Locators.
- Selenium-4 || Handling multiple web browsers.
- Selenium-3 || First program using Selenium Web Driver.
- Selenium-2 || Let’s learn Selenium IDE.
- Selenium-1 || Understanding Selenium and Selenium WebDriver.
- JIRA Tutorials-2 || Implement Search and Filter on JIRA Issues.
- JIRA Tutorials-1 || Basic understanding of JIRA.