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
The POM.XML includes
- plugin for Maven Compiler
<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<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>
<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>

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 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.

If you have any questions about this article, please leave them in the Comments section.
Like this:
Like Loading...
Related
Thanks for the great tip, wasn't aware of the fact that Maven surefire plugin supports parallel execution. Another workaround of running Selenium tests in parallel is using Apache JMeter which is capable of running JUnit tests in multithreaded manner. See How to Use JUnit With JMeter guide for general concept and detailed instructions.
LikeLike
you are welcome.
LikeLike
Superbly written article, if only all bloggers oered the same content as you, the internet would be a far better place..
SEO services pakistan
LikeLike
thanks, James
LikeLike