Multithreading helps to run test cases in parallel in Selenium

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.

            @Override
            public void run() {
                        System.out.println("Thread- Started" +Thread.currentThread().getName());
                        String threadname=Thread.currentThread().getName();
                        System.out.println(threadname);
                        try {
                                    Thread.sleep(1000);
                                    setUp(this.browsertype);
                                    testGoogleSearch();
                        } catch (InterruptedException e) {
                                    e.printStackTrace();
                        } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                        } finally {
                                    //tearDown();
                        }
                        System.out.println("Thread- END " + Thread.currentThread().getName());
            }

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.

 public static void main(String[] args) {
                        Thread t1 = new MultiThreadingExample("Thread Chrome", "Chrome");
                        Thread t2 = new MultiThreadingExample("Thread IE", "IE");
                        Thread t3 = new MultiThreadingExample("Thread Firefox", "Firefox");
                        System.out.println("Starting MyThreads");
                        t1.start();
                        t2.start();
                        t3.start();
                        System.out.println("Thread has been started");
            }

Sample Program:

package tests;

import java.io.File;

import java.util.concurrent.TimeUnit;

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.openqa.selenium.remote.DesiredCapabilities;

import org.testng.Assert;

import org.testng.annotations.AfterClass;

import org.testng.annotations.Test;


public class MultiThreadingExample extends Thread {

            private WebDriver driver;

            private String Url;

            private String browsertype;

            public MultiThreadingExample(String name, String browsertype) {

                        super(name);

                        this.browsertype = browsertype;

            }

            @Override
            public void run() {

                        System.out.println("Thread- Started" +Thread.currentThread().getName());

                        String threadname=Thread.currentThread().getName();

                        System.out.println(threadname);

                        try {
                                    Thread.sleep(1000);
                                    setUp(this.browsertype);
                                    testGoogleSearch();
                        } catch (InterruptedException e) {
                                    e.printStackTrace();

                        } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                        } finally {
                                    //tearDown();
                        }
                        System.out.println("Thread- END " + Thread.currentThread().getName());
            }

            // main method to create the thread and run multiple threads
            public static void main(String[] args) {
                        Thread t1 = new MultiThreadingExample("Thread Chrome", "Chrome");
                        Thread t2 = new MultiThreadingExample("Thread IE", "IE");
                        Thread t3 = new MultiThreadingExample("Thread Firefox", "Firefox");
                        System.out.println("Starting MyThreads");
                        t1.start();
                        t2.start();
                        t3.start();
                        System.out.println("Thread has been started");
            }
            // set up the method to initialize driver object
            public void setUp(String browsertype) throws Exception {
                        if (browsertype.contains("IE")) {
                                    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
                                    capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                                    System.setProperty("WebDriver.ie.driver", "C:\\Users\\abc\\Desktop\\Selenium\\IEDriverServer_Win32_2.39.0\\IEDriverServer.exe");
                                    driver = new InternetExplorerDriver(capabilities);

                        } else if (browsertype.contains("Firefox")) {

                                    System.setProperty("WebDriver.firefox.marionette","C:\\Users\\abc\\Desktop\\Selenium\\geckodriver-v0.22.0-win32\\geckodriver.exe");
                        }

                        else if(browsertype.contains("Chrome")){

                        System.setProperty("WebDriver.chrome.driver","C:\\Users\\abc\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");

                                    driver = new ChromeDriver();

                        }

                        Url = "https://www.google.co.in/";

                        driver.get(Url);

                        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                        driver.manage().window().maximize();

            }

            // test scripts

            public void testGoogleSearch() throws Exception {

                        String actualtitle = driver.getTitle();  

                        String expectedTitle="Google";

                        Assert.assertEquals(actualtitle, expectedTitle, "The expected title matched");

                        System.out.println("This is Sachin");

                        System.out.println("################Actual:"+actualtitle+ "Expected:"+expectedTitle+"#################");

            }

            // tear down function to close browser

            public void tearDown() {

                        driver.close();

                        driver.quit();

            }



}

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.

System.setProperty("WebDriver.gecko.driver","C:\\Users\\abc\\Desktop\\Selenium\\geckdriver-v0.22.0-win32\\geckodriver.exe");

driver = new FirefoxDriver();

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.

public class MultiThreadingExample extends Thread

multiThreadingExample class extends the thread class to create the thread.

public MultiThreadingExample(String name, String browsertype) {

                        super(name);

                        this.browsertype = browsertype;

            }

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

public void run()

//The life of the thread is starts from here

try {

Thread.sleep(1000);

setUp(this.browsertype);

testGoogleSearch();

}

Explanation of setup() method

public void setUp(String browsertype) throws Exception {

if (browsertype.contains("IE")) {

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

System.setProperty("WebDriver.ie.driver", "C:\\Users\\abc\\Desktop\\Selenium\\IEDriverServer_Win32_2.39.0\\IEDriverServer.exe");

driver = new InternetExplorerDriver(capabilities);

} else if (browsertype.contains("Firefox")) {

System.setProperty("WebDriver.firefox.marionette","C:\\Users\\abc\\Desktop\\Selenium\\geckodriver-v0.22.0-win32\\geckodriver.exe");

driver = new FirefoxDriver();

}

else if(browsertype.contains("Chrome")){

System.setProperty("WebDriver.chrome.driver","C:\\Users\\abc\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");

driver = new ChromeDriver();

}

Url = "https://www.google.co.in/";

driver.get(Url);

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.manage().window().maximize();

}

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-

public void tearDown()

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

 

2 Comments

Leave a Reply