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

问题描述 投票:0回答:1

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

所有在线示例都使用info.cukes 1.2.5版本进行解释,其中所有依赖类和方法都是公开的

我以前从未写过一个testrunner。有人可以帮忙吗?

cucumber-jvm
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.