0
votes

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.

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

3 Answers

3
votes

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

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)

0
votes

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.