黄瓜 - 未定义的步骤虽然已定义

Cucumber - Undefined Steps Though defined

我正在创建一个简单的 Selenium Cucumber 项目并使用 "Lambda Expressions Constructor" 方式为功能文件定义步骤但是当我 运行 CucumberTest class 我得到失败异常作为

There are undefined steps!

我的StepDefinition低于一个

特征文件是下面的

CucumberRunner class 如下:

所以请建议我,如果我使用 Lambda 表达式,调用步骤定义文件有什么不同的方法吗?

正如我在评论中提到的那样。选项 glue 需要一个包名列表,而不是目录。将其更改为

glue = {"src/test/java/my.project.automation.wolfram_alpha" }

glue = {"my.project.automation.wolfram_alpha" }

将解决问题。

在下面找到工作片段。假设以下结构

src/test/java/my/project/automation/wolfram_alpha/StepDef.java
src/test/java/my/project/automation/wolfram_alpha/cucumberTest.java
src/test/resources/wolfram.feature
pom.xml

pom.xml(依赖部分)

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <version.cucumber>3.0.2</version.cucumber>
</properties>
<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java8</artifactId>
        <version>${version.cucumber}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${version.cucumber}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>${version.cucumber}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

cucumberTest.java

package my.project.automation.wolfram_alpha;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(
        features = { "src/test/resources/wolfram.feature" },
        glue = {"my.project.automation.wolfram_alpha" }
)
public class cucumberTest extends AbstractTestNGCucumberTests {
}

StepDef.java

package my.project.automation.wolfram_alpha;

import cucumber.api.java8.En;

public class StepDef implements En {
    public StepDef() {
        Given("URL of WolframAlpha" , () -> {
            System.out.println("Given URL of WolframAlpha");
        });
        When("user logged in as {string} with {string}" , (String user, String password) -> {
            System.out.printf("When user logged in as {%s} with {%s}%n", user, password);
        });
        And("login is successful" , () -> {
            System.out.println("And login is successful");
        });
        And("user search for a {string}" , (String topic) -> {
            System.out.printf("And user search for a {%s}%n", topic);
        });
        Then("results are displayed in a creative way" , () -> {
            System.out.println("Then results are displayed in a creative way");
        });
    }
}

wolfram.feature 来自问题

运行 mvn test 的测试产生以下输出。

Running my.project.automation.wolfram_alpha.cucumberTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@726f3b58
Given URL of WolframAlpha
When user logged in as {user} with {password}
And login is successful
And user search for a {IDOL}
Then results are displayed in a creative way

1 Scenarios (1 passed)
5 Steps (5 passed)