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

时间:2019-04-24 13:51:32

标签: cucumber-jvm

我试图编写一个自定义的TestNGCucumberRunner(适用于最新版本的cucumber 4.2.6),在其中我可以在getFeatures()方法中基于运行时参数来过滤cumberfeatures的列表。

所有在线示例均通过info.cukes 1.2.5版本进行了说明,其中所有依赖类和方法都是公共的

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

1 个答案:

答案 0 :(得分:1)

第一- 按照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类

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类和框架的需要,在/ 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>