trying to run in parallel for cucumber junit not working

75 Views Asked by At

I added the parallel and threadcount lines to surefire plugin configuration in pom file:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <parallel>all</parallel>
          <threadCount>2</threadCount>
          <argLine>-Xmx4g</argLine>
          <!--
          <useUnlimitedThreads>true</useUnlimitedThreads>
          -->
          <systemPropertyVariables>
            <databaseSchema>MY_TEST_SCHEMA_${surefire.forkNumber}</databaseSchema>
            <allure.results.directory>${project.build.directory}/allure-results</allure.results.directory>
          </systemPropertyVariables>
          <includes>
            <include>**/Test*.java</include>
            <include>**/*Test.java</include>
            <include>**/*Tests.java</include>
            <include>**/*TestCase.java</include>
          </includes>
        </configuration>
      </plugin>

When I run a few tests with maven I see Running parallel.Test_RunCucumber and I can see that multiple browsers are spawned. The problem is it only seems to execute tests in a single browser instance. The spawned browser instances are all "empty".

Here's one of the tests. The other is identical just goes to different sites.

Feature: Scenario Outlines feature file

  Scenario Outline: <site>
#    Given Step from '<scen_out_row_num>' in 'scenario-outlines' feature file
    Given the user navigates to home page '<site>'

    Examples:
      | site       |
      | https://google.com/ |
      | https://nationalpost.com/ |
      | https://ottawacitizen.com/ |

I've also tried using the failsafe plugin, but doesn't seem to make any difference.

I also tried following the example here:https://cucumber.io/docs/guides/parallel-execution/?lang=java#junit-4. My output though indicates all running on the same thread In the example the first test is:


  Scenario Outline: <scen_out_row_num>
    Given Step from '<scen_out_row_num>' in 'scenario-outlines' feature file

    Examples:
      | scen_out_row_num       |
      | Scenario Outline Row 1 |
      | Scenario Outline Row 2 |

The second is:

Feature: Scenarios feature file

  Scenario: Scenario Number One
    Given Step from 'Scenario 1' in 'scenarios' feature file

  Scenario: Scenario Number Two
    Given Step from 'Scenario 2' in 'scenarios' feature file

And the step def is:

    @Given("Step from {string} in {string} feature file")
    public void step(String scenario, String file) {
        System.out.format("@@@ Thread ID - %2d - %s from %s feature 
            file.\n", Thread.currentThread().getId(), 
            scenario,file);
    }
@@@ Thread ID -  1 - Scenario Outline Row 1 from scenario-outlines feature file.
@@@ Thread ID -  1 - Scenario Outline Row 2 from scenario-outlines feature file.
@@@ Thread ID -  1 - Scenario 1 from scenarios feature file.
@@@ Thread ID -  1 - Scenario 2 from scenarios feature file.

CODE TO RUN

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <selenium.version>4.16.1</selenium.version>
    <cucumber.version>7.15.0</cucumber.version>
  </properties>
  <groupId>postmedia-automated-testing</groupId>
  <artifactId>postmedia-automated-testing</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <parallel>all</parallel>
          <threadCount>4</threadCount>
          <includes>
            <include>**/Test*.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-api</artifactId>
      <version>${selenium.version}</version>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-chrome-driver</artifactId>
      <version>${selenium.version}</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>${cucumber.version}</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-core</artifactId>
      <version>${cucumber.version}</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>${cucumber.version}</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.10.1</version>
    </dependency>
  </dependencies>
</project>

src/test/resources/features/feature_1.feature

Feature: Navigate to https://google.com
  Scenario: Verify navigation to https://google.com
    Given the user navigates to home page "https://google.com"

src/test/resources/features/feature_2.feature

Feature: Navigate to  https://microsoft.com
  Scenario: Verify navigation to  https://microsoft.com
    Given the user navigates to home page "https://microsoft.com"

src/test/java/steps/StepDefs.java

package steps;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class StepDefs {
     private WebDriver driver;

    @Before()
    public void launchBrowser() {
        driver = new ChromeDriver();
    }

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

    @Given("the user navigates to home page {string}")
    public void navigateToHomePage(String url) throws InterruptedException {
        driver.navigate().to(url);
        System.out.println("@@@ navigated to site: " + url);
        Thread.sleep(5000);
    }
}

src/test/java/steps/Test_RunCucumber.java

package steps;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class StepDefs {
     private WebDriver driver;

    @Before()
    public void launchBrowser() {
        driver = new ChromeDriver();
    }

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

    @Given("the user navigates to home page {string}")
    public void navigateToHomePage(String url) throws InterruptedException {
        driver.navigate().to(url);
        System.out.println("@@@ navigated to site: " + url);
        Thread.sleep(5000);
    }
}
1

There are 1 best solutions below

14
M.P. Korstanje On

If you are using cucumber-junit you can only execute features in parallel. This due to technical limitations in JUnit 4.

If you upgrade to JUnit 5, you can use the cucumber-junit-platform-engine which can execute scenarios and examples in parallel.

See the example project and docs