How to execute cucumber features parallely?

I have below feature files (Separate feature files) in src/test/resources/feature/ and I would like to run them in parallel. Like: One feature file has to execute in chrome and another one has to execute in another chrome instance as mentioned @Tags name.

    @Regression 
    Scenario: Searching for HelpMate on Company Hompe page
    Given I navigate to application URL
    Then I verified title "Company - Production - Sign In" on Login Page 
    after 
    launched the URL
    When I type username as "defined in config" in username filed on Login 
    page
    And I type password as "defined in config" in password filed on Login 
    page
    And I click Login button on Login page
    And I wait for 15 seconds
    Then I verified title "Company - Production - My Applications" on 
    Login Page

    @Regression 
    Scenario Outline: Searching for different options on Company Home 
    page
    Given I navigate to application URL
    Then I verified title "Company - Production - Sign In" on Login Page 
    after launched the URL
    When I type username as "defined in config" in username filed on Login 
    page
    And I type password as "defined in config" in password filed on Login 
    page
    And I click Login button on Login page
    And I wait for 15 seconds

I'm using cucumber-java 1.2.5 version, and AbstractTestNGCucumberTests as runner. I'm able to run a single feature file but when i try to run 2 feature files using cucumber-jvm-parallel-plugin v#4.0.0 and maven surefire plugin v#2.40, it is not initialing the test class (Error:cucumber.runtime.CucumberException: cucumber.runtime.CucumberException: Failed to instantiate class com.cvs.stepdefinition.LoginPage)

This error is gone after I used updated cucumber dependencies

cucumber-jvm-parallel-plugin-- Not using anymore as it is not required with latest version of cucumber libraries

<plugin>
  <groupId>com.github.temyers</groupId>
    <artifactId>cucumber-jvm-parallel-plugin</artifactId>
        <version>4.0.0</version>
        <executions>
          <execution>
            <id>generateRunners</id>
            <phase>validate</phase>
            <goals>
              <goal>generateRunners</goal>
            </goals>
        <configuration>
        <glue>
          <pakage>com.cvs.stepdefinition</pakage>
        </glue>
                     <featuresDirectory>src/test/resources/features
                     </featuresDirectory>
                      <cucumberOutputDir>${project.build.directory}/
                       cucumberparallel</cucumberOutputDir>          
                      <format>json,html</format>
                      <testFailureIgnore>true</testFailureIgnore>
              <tags>
            <tag>@Regression</tag>
              </tags>
        </configuration>
            </execution>
      </executions>
    </plugin>

maven-surefire-plugin--UPDATED

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
          <suiteXmlFiles>
               <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
    </configuration>
</plugin>   

TestNG.xml--UPDATED

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Testng Cucumber Suite" parallel="tests"configfailurepolicy="continue" thread-count="2">
  <test name="SmokeSuite">
  <parameter name="browserName" value="chrome"/>
  <classes>
    <class name="com.cvs.runner.TestSuiteRunner"></class>
  </classes>
  </test> 
 </suite>

I have tried overriding the method from AbstractTestNGCucumberTests and set the parallel attribute in @DataProvider annotation to true but still getting the same error.

@DataProvider(parallel=true)
    public Object[][] features() {
        return testNGCucumberRunner.provideFeatures();
    }

POM.XML

<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>testNewBDD</groupId>
    <artifactId>TestAutomation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestAutomation</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <mainClass>ReportGenerator</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.shared</groupId>
                        <artifactId>maven-filtering</artifactId>
                        <version>1.3</version>
                    </dependency>
                </dependencies>
            </plugin>       
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java8 -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>4.2.6</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>4.2.6</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>4.2.6</version>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.14.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.0</version>
        </dependency>
    </dependencies>
</project>

Runner

@CucumberOptions(
            strict = true,
            monochrome = true, 
            features = {"src/test/resources/features"},
            tags={"@Regression"},
            glue = {"stepDef", "utils"},
            plugin = {"pretty", "html:target/cucumber-html-report","json:target/cucumber-html-report/TestHomePage.json"},
            //junit ={ "--step-notifications"},
            dryRun = false

            )

    public class UITest {
        private TestNGCucumberRunner testNGCucumberRunner;

        @BeforeClass(alwaysRun = true)
        public void setUpClass() throws Exception {
            testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
        }

        @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")
        public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper cucumberFeature) throws Throwable {
            testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
        }

        @DataProvider(parallel=true)
        public Object[][] scenarios() {
            return testNGCucumberRunner.provideScenarios();
        }

        @AfterClass(alwaysRun = true)
        public void tearDownClass() throws Exception {
            testNGCucumberRunner.finish();
        }
    }

There is only one feature file having 2 scenarios and i want these 2 scenarios to run on two different browser parallely. Please help me to resolve this.

Key Point : We would request you to use Cucumber-JVM v4.x.x specially to implement parallel execution without using cucumber-jvm-parallel-plugin as you are using pretty old dependency(v1.2.5) of Cucumber.

Note : In below implementation, we would be reading browser parameter from TestNG.xml file

First - Update POM.xml with correct set of io.cucumber dependencies as per any cucumber v >= 4.0.0 lets pick v4.2.6

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.2.6</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>4.2.6</version>
</dependency>

Second - Customize TestNGRunner class as per your framework need

package com.jacksparrow.automation.suite.runner;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import com.jacksparrow.automation.steps_definitions.functional.BaseSteps;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(features = "classpath:features/functional/",
                     glue = {"com.jacksparrow.automation.steps_definitions.functional" },
                   plugin = { "pretty","json:target/cucumber-json/cucumber.json",
                            "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
                   tags = { "@BAMS_Submitted_State_Guest_User" },
                   junit ={ "--step-notifications"},
                   strict = false,
                   dryRun = false,
               monochrome = true)

public class RunCukeTest extends Hooks {

} 

Third - Implement Hooks.java

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import cucumber.api.testng.AbstractTestNGCucumberTests;

public class Hooks extends AbstractTestNGCucumberTests {

    @Parameters({ "browser" })
    @BeforeTest
    public void setUpScenario(String browser){
        //BaseSteps.getInstance().getBrowserInstantiation(browser); your browser setup method
    }
}

Fourth - Update TestNG.xml under /src/test/resources/ as per your TestNGRunner Class and framework need.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Testng Cucumber Suite" parallel="tests" thread-count="2">
    <test name="SmokeTest">
        <parameter name="browser" value="chrome" />
        <classes>
            <class name="com.cvs.runner.TestSuiteRunner" />
        </classes>
    </test>
</suite>

Fifth - You shall be all set to run automation suite using TestNG in any of the following ways

 -    Run TestNG.xml directly from IDE 
 -    From CMD - mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng.xml
 -    From POM.xml - Using Surefire Plugin

<profiles>
   <profile>
      <id>selenium-tests</id>
      <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.0.0-M3</version>
               <configuration>
                  <suiteXmlFiles>
                     <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                  </suiteXmlFiles>
               </configuration>
            </plugin>     
         </plugins>
      </build>
   </profile>
</profiles>

Running Cucumber Scenarios in Parallel & NOT Feature Files , Update: 4.0.0 version is available at maven central repository with bunch of changes.for more details go here. Update: 2.2.0 version is available� Cucumber can be executed in parallel using JUnit and Maven test execution plugins. In JUnit the feature files are run in parallel rather than scenarios, which means all the scenarios in a feature file will be executed by the same thread. You can use either Maven Surefire or Failsafe plugin to execute the runners.

Did you try to add number of treads in your .xml file, I do have it in mine.

So your .xml file will be:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Testng Cucumber Suite" parallel="tests" thread-count="2">
  <test name="SmokeSuite">
  <classes>
    <class name="com.cvs.runner.TestSuiteRunner"></class>
  </classes>
  </test> 
 </suite>

(Also try to change parallel = "tests" to parallel methods. And if you r using priorities in your tests parallel running is not gonna work)

How to execute cucumber feature file parallel, One such setting is features which allow you to make a runner for each and every feature file. Running JUnit test in parallel. Maven Surefire� In order to run one or several .feature files, an empty class is created. It is annotated with @RunWith(Cucumber.class) . If the class name starts or ends with “ test ” then JUnit automatically runs this class, which then calls Cucumber which is picking feature files to be executed.

Cucumber 4 provides native support to run scenarios(not by features) in parallel. you have to update your pom.xml dependencies to latest cucumber version. cucumber-core 4.2.0, cucumber-java 4.2.0, cucumber-junit 4.2.0

In the runner file, you have add "--thread 2 " like a plugin. this will run the scenario in 2 threads.

Running Cucumber tests in parallel – Automation Rhapsody, In this video we will discuss Cucumber 4 Test runner and running the scenarios in Parallel Duration: 9:52 Posted: Nov 17, 2019 The Cucumber JVM Parallel Plugin works with the maven-failsafe-plugin, so your Cucumber scenarios will be executed when you run mvn verify. By default, it generates test runners with the IT suffix, so make sure your maven-failsafe-plugin configuration will look for files matching this format.

Cucumber 4 Test Runner and running scenarios in parallel, Cucumber feature file Parallel Execution two way One way to creating multiple runner class Duration: 9:50 Posted: Feb 16, 2019 Running single Cucumber Feature file or single Cucumber Tag. Execute all tests tagged as @SmokeTests. Note: In the excel sheet and in the feature file paste above if you count the scenarios which are tagged as @SmokeTests, you will find the count is 6 and the same count is also displayed under Junit tab.

Cucumber feature file Parallel Execution Java, Run Cucumber tests faster by running them in parallel using maven plugins cucumber-jvm Duration: 5:22 Posted: Jan 4, 2018 Run a feature Click in the gutter next to the feature that you want to run and select Run 'Feature: <name>'. You can also place the caret at Feature and press Ctrl+Shift+F10. In the Project tool window (Alt+1), right-click a feature file and select Run Feature <name>.

Run Cucumber tests faster by running them in parallel (January , When I am running the cucumber test from testng.xml, scenarios are not executed in parallel. Scenarios are executed one after another. Project has three layer approach or calling: feature -> stepdefinitions -> businessflows -> screens. feature -> It is test/resources. It will contain all feature files (.feature). stepdefinitions -> This folder has java files which contains step definitions corresponding in feature file.

Comments
  • This has been resolved, we just need to use the updated version of cucumber jars and add parallel=true in DataProvider annotation of AbstractTestNGCucumberTests class.
  • I have tried all the things above but my scenarios are executing in the same browser at the same time.
  • If that's the case then would request to update your pom.xml, runner class, hooks class, testng.xml in the problem statement so we can understand where exactly things are breaking
  • I have updated my files. rather using hooks class iI'm implementing SharedDriver class.
  • Are you using cucumber-jvm-parallel-plugin ?
  • No, I removed cucumber-jvm-parallel-plugin. Only latest version of cucumber libraries. See the pom.xml pls.
  • Yes you are right latest version supports to run scenarios, I have updated my dependencies to latest but still it is not running my scenarios in parallel. Could you pls tell me where do you want me to add thread 2 like a plugin in runner?
  • @Jitendra would suggest to create a runner class with main method to trigger the cucumber scenarios.
  • U need to use the Main class contained in the cucumber.api.cli package if u want to use the 'threads' option. The command would be something like - Main.main(new String[]{ "–threads", "4", "-g", "stepdef", "src/test/resources/features/parallel/"});. No runner is required.