我如何使用最新的 io.cucumber.cucumber-testng 版本 4.2.6 编写自定义的 TestNGCucumberRunner

How can i write a customized TestNGCucumberRunner with the latest io.cucumber.cucumber-testng version 4.2.6

我正在尝试编写自定义 TestNGCucumberRunner(适用于最新版本的 Cucumber 4.2.6),我可以在其中根据运行时参数在 getFeatures() 方法中过滤 cucumberfeatures 列表。

网上的所有例子都是用info.cukes1.2.5版本解释的,其中所有的依赖类和方法都是public

我以前从未写过测试运行器。有人可以帮忙吗?

首先 - 根据 v 4.2.6[使用正确的 io.cucumber 依赖集更新 POM.xml

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.6</version>
    <scope>test</scope>
</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>
</dependency>

其次 - 根据您的框架需求自定义 TestNGRunner class

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

注意 - 我没有以这种方式实现。但据我所知,它可能会起作用。请检查并分享您的经验。

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

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

    <test thread-count="1" name="Test" parallel="tests">
        <parameter name="browser" value="chrome" />
        <classes>
            <class
                name="com.jacksparrow.automation.suite.runner.RunCukeTest" />
        </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>