如何并行执行黄瓜功能?

时间:2019-05-16 15:30:10

标签: maven selenium parallel-processing cucumber testng

我在src / test / resources / feature /中有以下功能文件(单独的功能文件),我想并行运行它们。就像:一个功能文件必须在chrome中执行,而另一个功能文件必须在另一个chrome实例中执行,如@Tags名称所述。

    @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

我使用的是cucumber-java 1.2.5版本,并且使用AbstractTestNGCucumberTests作为运行程序。我能够运行一个功能部件文件,但是当我尝试使用Cucumber-jvm-parallel-plugin v#4.0.0和Maven surefire插件v#2.40运行2个功能部件文件时,它没有初始化测试类 (错误:cucumber.runtime.CucumberException:cucumber.runtime.CucumberException:无法实例化com.cvs.stepdefinition.LoginPage类)

使用更新的黄瓜依赖项后,此错误消失了

cucumber-jvm-parallel-plugin--不再使用,因为最新版本的黄瓜库不再需要

<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--已更新

<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--已更新

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

我尝试从AbstractTestNGCucumberTests重写该方法,并将@DataProvider批注中的parallel属性设置为true,但仍然遇到相同的错误。

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

亚军

@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();
        }
    }

只有一个具有2个方案的功能文件,我希望这2个方案可以在两个不同的浏览器上并行运行。请帮助我解决这个问题。

3 个答案:

答案 0 :(得分:2)

关键点::我们将要求您专门使用 Cucumber-JVM v4.xx 来实现并行执行,而无需在使用时使用Cucumber-jvm-parallel-plugin相当老的Cucumber依赖项( v1.2.5 )。

注意::在以下实现中,我们将从TestNG.xml文件读取浏览器参数

首先-按照任何黄瓜v> = 4.0.0的正确一组io.cucumber依赖关系更新POM.xml,让我们选择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>

第二-根据您的框架需求自定义TestNGRunner类

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 {

} 

第三-实现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
    }
}

第四-根据您的TestNGRunner类和框架的需要,在/ src / test / resources /下更新TestNG.xml。

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

第五-您将被设置为以下列任何一种方式使用TestNG运行自动化套件

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

答案 1 :(得分:0)

您是否尝试在.xml文件中添加胎面数,我确实在其中添加了胎面数。

因此,您的.xml文件将是:

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

(也尝试将parallel =“ tests”更改为并行方法。如果您在测试中使用优先级,则并行运行将无法工作)

答案 2 :(得分:0)

黄瓜4提供本机支持以并行运行方案(不按功能)。 您必须将pom.xml依赖项更新为最新的Cucumber版本。 黄瓜核心4.2.0,黄瓜Java 4.2.0,黄瓜Junit 4.2.0

在运行程序文件中,您添加了“ --thread 2”(如插件)。这将在2个线程中运行该方案。