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

Create WebDriver HTML Reports With The Allure Framework

cats and html reports

 

Allure framework can help with generating HTML reports for your Selenium WebDriver projects.

The reporting framework is very flexible and it works with many programming languages and unit testing frameworks.

You can read everything about it at http://allure.qatools.ru/.

JUNIT does not provide any HTML reports by default

If you use JUNIT for running your test automation scripts,everything that you get is a visual report of the test results.

This visual report is GREEN if all the test scripts pass and RED if at least one test script fails.

 

For each test script, the execution status (pass/fail) is displayed, together with the execution time and details of the error.

The problems with this visual report are that

  • it cannot be saved
  • the amount of information that it includes is very limited
  • it is not possible to see over time how the test scripts run

The Allure framework has solutions for all these problems

The reports that can be generated using Allure are very impressive.
You can get
– an overview report for multiple test suites

 

– detailed report for a test suite

 

– graphical reports

 

See more report templates on this link:
http://ci.qatools.ru/job/allure-core_master-deploy/lastSuccessfulBuild/Allure_report/

How can you get these great reports for your WebDriver test scripts?

It is quite easy to add the Allure framework to your WebDriver project.

 

this is relevant

 

1. Create a Maven project in Eclipse

After creating the Maven project, the project structure will be as follows:

 

 

 

2. Add the class files to the proper folders

 

The page object classes should be in the Src/Main/Java folder.

The test script classes should go to the Test/Java folder.

 

 

3. Configure the POM xml file

 

The POM file is very straightforward:

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

             

It includes dependencies for JUNIT and Selenium Firefox driver and a plugin for the maven compiler.

 


selenium free course

4. Run the test scripts

 

 

Run the test scripts using maven test:

 

 

 

 

 

 

 

The execution results are displayed in the console tab:

 

5. Add Allure framework details to the POM file

 

The POM file changes a bit after adding the Allure details.

I have highlighted with italic all changes:

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

 <properties>

         <aspectj.version>1.8.5</aspectj.version>

         <allure.version>1.4.11</allure.version>

 </properties>

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

         <dependency>

               <groupId>ru.yandex.qatools.allure</groupId>

               <artifactId>allure-junit-adaptor</artifactId>

              <version>${allure.version}</version>

        </dependency>

</dependencies>  

 

cat awake

<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.14</version>

                        <configuration>

                                  <testFailureIgnore>false</testFailureIgnore>

                                  <argLine>

                                       –                   javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar

                                 </argLine>

                                <properties>

                                     <property>

                                          <name>listener</name>                                         

                                          <value>

                                                      ru.yandex.qatools.allure.junit.AllureRunListener

                                          </value>

                                   </property>

                               </properties>

                     </configuration>
                      <dependencies>

                             <dependency>

                                      <groupId>org.aspectj</groupId>

                                     <artifactId>aspectjweaver</artifactId>

                                     <version>${aspectj.version}</version>

                            </dependency>

                    </dependencies>

        </plugin>

        <plugin>

               <groupId>org.eclipse.jetty</groupId>

               <artifactId>jetty-maven-plugin</artifactId>

               <version>9.2.10.v20150310</version>

               <configuration>

                       <webAppSourceDirectory>

                           ${project.build.directory}/site/allure-maven-plugin

                      </webAppSourceDirectory>

                      <stopKey>stop</stopKey>

                      <stopPort>1234</stopPort>

            </configuration>

     </plugin>

  </plugins>

</build>

<reporting>

                <excludeDefaults>true</excludeDefaults>

                <plugins>

                      <plugin>

                              <groupId>ru.yandex.qatools.allure</groupId>

                              <artifactId>allure-maven-plugin</artifactId>

                              <version>2.0</version>

                      </plugin>

             </plugins>

</reporting>

</project>

 


selenium free course

6. Run the test scripts in Command Prompt

 

 

 

7. Generate the report by running mvn site in the Command Prompt

 

 

The Allure report can be found on the following location:
C:\Users\home\workspace\AllureProject\target\site\allure-maven-plugin

 

 

The report can be displayed in the browser by double-clicking the index.html file:


selenium free course