Parallel Execution Using Session Handling in Selenium WebDriver

We start automation scripting with a single test case; Gradually, we increase the number as the business approves stories. A point comes where our automation pack grows in such a huge number wherein we started looking for the performance improvement. So, in that case, parallel execution helps a lot. Today we are going to discuss the execution of multiple test cases simultaneously to improve the performance. In that case, there comes the concept of parallel execution and we are going to discuss it using session handling.

The script can be run parallel in two ways.

1.Using session handling

2.Using Multi-threading

Let’s first discuss session handling today.

Don’t Miss: How to set up Selenium Grid?

Why Session Handling in selenium?

When we execute any test case, the WebDriver interacts with the browser of the machine. If you want to execute another test case in the same machine either with the same browser or different browser along with the test case which is already running, then in that case session handling gives the best solution.

This is basically the ability to execute multiple parts or sub-components of the code in parallel.

WebDriver interface’s source code has one variable declaration, which is sessionId. When we create an object of the WebDriver then one sessionId will be attached to the corresponding driver like chrome/IE or Firefox.

So, for parallel execution for multiples tests, we need to create different sessions which will perform solely for a particular execution.

Parliament session handling

Parliament Session. Speaker as WebDriver Session Handler and other MPs are Test cases. All starts running together in parallel 🙂

How to run scripts in parallel?

Here are the steps to run multiple tests simultaneously.

Step# 1: Create tests by defining different sessions

Step# 2: Configure your testing.xml

Add parallel attribute within the suite tag:

The parallel attribute can include methods, classes, tests, and instances.

If you have mentioned parallel as methods, then all the methods under @Test annotation will be executed. Similarly, for classes, all the test classes under java classes will run in parallel. The same tactic applies to Tests and Instances as well.

Suppose you have several instances of test cases, like A and B then all the methods of instance A will be executed in thread TA. All the methods of instance B will be executed in thread TB and TA! =TB.

The attribute thread-count=4 means 4 sessions or browser will be opened for the execution.

Defining Dependencies

If you want to set the execution dependency, then you can use dependsOnMethods attribute in the testing.xml

Suppose if you want to execute createSession2 method before execution createSession1 then you have to use the below testng.xml


Right click on testing.xml then select Run as TestNG

Here testcase2 i.e. Createsession2 is executed before createsession1

Session handling

This was all about parallel execution of the test cases through session handling. We will discuss another technique for parallel execution through Multi-threading in another post.

Join Inviul fb group

Leave a Reply