One of the best ways of making your Selenium WebDriver scripts faster is to run them in parallel.

You can do this with the Maven SureFire plugin.

Maven SureFire has settings for running in parallel both methods and classes.

 

By default, Selenium WebDriver scripts are executed sequentially

I will use a sample project to explain how to run Selenium WebDriver scripts in parallel.

The sample project is created as a Maven project in Eclipse and it includes

  • one test script

 

 The POM.XML includes
  • plugin for Maven Compiler

 

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;

<modelVersion>4.0.0</modelVersion>

<groupId>com.siminiuc</groupId>

<artifactId>MavenProject</artifactId>

<version>0.0.1</version>

<name>Maven Project</name>

<url>http://maven.apache.org</url&gt;

<dependencies>
 
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
         <scope>test</scope>
     </dependency>
 
     <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>2.47.1</version>
      </dependency>
 
</dependencies>
 
<build>
<plugins>
 
     <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.3.2</version>
             <configuration>
                      <fork>true</fork>
                      <source>1.7</source>
                      <target>1.7</target>
                      <executable>C:\JDK\bin\javac.exe</executable>
             </configuration>
        </plugin>
 
</plugins>
</build>
 
</project>

 


selenium free course

The test class includes 5 JUNIT test scripts, all of them having the same code:

 

import static org.junit.Assert.*;import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

 

public class TestClass1
{

 

WebDriver driver;
String siteUrl = “http://www.vpl.ca&#8221;;
String keyword = “java”;

 

@Before
public void setUp()  {
driver = new FirefoxDriver();
}

 

@After
public void tearDown() {
driver.quit();
}

 

@Test
public void testFirstResult() throws InterruptedException 
{
HomePage homePage = new HomePage(driver);
ResultsPage resultsPage =
homePage.open(siteUrl).typeKeyword(keyword).executeSearch();
DetailsPage detailsPage = resultsPage.selectResult(1);
assertTrue(detailsPage.visibilityBookTitle() == true);
assertTrue(detailsPage.lengthBookTitle() > 0);
assertTrue(detailsPage.visibilityBookAuthor() == true);
assertTrue(detailsPage.lengthBookAuthor() > 0);
}

 

@Test
public void testSecondResult() throws InterruptedException
{ ………..}

 

@Test
public void testThirdResult() throws InterruptedException
{ ………. }@Test
public void testFourthResult() throws InterruptedException
{ ……… }@Test
public void testFifthResult() throws InterruptedException
{ …….. }}The project is executed using the Maven Test command:

 

 

The JUNIT test scripts from the test class are executed sequentially.

The execution time is 1.44 minutes.

 

Run the JUNIT test scripts in parallel

A few changes are needed to the POM.XML file.

We need to add a new plugin for Maven Surefire.

For the Maven Surefire plugin, the following settings are required:

parallel = methods
threadCounts = 5

 

 

<modelVersion>4.0.0</modelVersion>
<groupId>com.siminiuc</groupId>
<artifactId>MavenProject</artifactId>
<version>0.0.1</version>
<name>Maven Project</name>
<dependencies>
 
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
                         <scope>test</scope>
                </dependency>
 
                <dependency>
                       <groupId>org.seleniumhq.selenium</groupId>
                       <artifactId>selenium-firefox-driver</artifactId>
                       <version>2.47.1</version>
                </dependency>
 
</dependencies>
 
<build>
 
<plugins>
 
              <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>2.3.2</version>
                   <configuration>
                           <fork>true</fork>
                           <source>1.7</source>
                           <target>1.7</target>
                           <executable>C:\JDK\bin\javac.exe</executable>
                   </configuration>
            </plugin>
 
           <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.19</version>
                  <configuration>
                        <parallel>methods</parallel>
                        <ThreadCount>5</threadCount>
                  </configuration>
           </plugin>
 
</plugins>
</build>
 
</project>
 

Running the project again shows the JUNIT scripts running in parallel.

 

The execution time is 41 seconds.

 

 

The execution time was reduced with 60% through by running the scripts in parallel.


selenium free course

If you have any questions about this article, please leave them in the Comments section.

4 Comments

Leave a reply to glinius Cancel reply