Input your search keywords and press Enter.

Multithreading helps to run test cases in parallel in Selenium

multithreading

In my previous post, I discussed the parallel execution of multiple test cases through session handling. Today we are going to discuss the same technique through Multithreading. Multithreading in Java has its own beauty to handle the scenario. I am very much sure that you will get some good knowledge after finishing this tutorial.

Let’s start now.

What is Multithreading?

It’s a feature of java that enables us to execute multiple parts of the program concurrently. It makes each part a thread.

multithreading

How to create a Thread?

We create the thread by following techniques:

  1. By extending the thread class
  2. By implementing the Runnable interface

In the below sample of code, we create the thread by extending the thread class i.e. extends the java.lang.thread class, further, this class override the run() method of the thread class.

We first create the object of the newly created class then we call the start() method to invoke the run() method. Here calling the start methods starts the execution of the thread.

Sample Program:

Note: In case of Firefox from the above code, if above line of code for Firefox does not work your then you can use the below line of code which implements Geckodriver for the Firefox browser.

As per above code, scripts will be executed in 3 different browsers (Chrome, IE, and Firefox) altogether in parallel.

Cross-browser testing with multithreading

We see the same script is running in three different browsers, thus, it could be the case of cross-browser testing.

Here we created the constructor, namely MultiThreadingExample with two signature threads name and browser type to initialize the thread details.

Explanation of setup() method

We have created the corresponding browser instances according to the current thread.

Please make sure that you keep the ChromeDriver for Chrome, IEdriver for IE and Geckodriver for Firefox in the mentioned directory.

Note:

  • Geckodriver is required only for Firefox version above 47
  • For IE, make sure zoom is 100%, then only URL will hit in IE.

In the end, we kill the driver instance after completion of the execution through-

We create the object of the class for all the three browsers, which means creating 3 threads and calling the start method to start the execution of the thread.

Join Inviul fb group

 

I am Sachin Samanta, having 6 year experience in Automation testing. I play with Java, Python, Cucumber and Selenium. I am fully a Devops engineer and spending my time on playing with these tools. I like R&D.

Leave a Reply

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