cucumber.runtime.CucumberException: 有未定义的步骤

cucumber.runtime.CucumberException: There are undefined steps

在使用 intelliJ 编辑器中使用 testNG 进行黄瓜测试时 "There are undefined steps" 出现错误 "There are undefined steps",请指导缺少的步骤

错误输出为

 Feature: New Tour Login Testing

  Scenario: Valid data testing           # src/main/java/features/loginnewtour.feature:2
    Given user is already on Login Page
    When title of login page is new tour
    Then user enters "mercury"

cucumber.runtime.CucumberException: cucumber.runtime.CucumberException: There are undefined steps

    at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:69)
    at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Caused by: cucumber.runtime.CucumberException: There are undefined steps
    at cucumber.api.testng.FeatureResultListener.collectError(FeatureResultListener.java:60)
    at cucumber.api.testng.FeatureResultListener.result(FeatureResultListener.java:45)
    at cucumber.runtime.Runtime.runStep(Runtime.java:282)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
    at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:63)
    ... 24 more


Undefined scenarios:
src/main/java/features/loginnewtour.feature:2 # Scenario: Valid data testing

1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.000s

以下是我在

下使用的文件

src/main/java/ 特征 loginnewtour.feature 我的跑步者 TestRunner.java 步骤定义 loginstepdefnewtour.java

我有一个功能文件 "loginnewtour.feature"

Feature: New Tour Login Testing
  Scenario: Valid data testing
    Given user is already on Login Page
    When title of login page is new tour
    Then user enters "mercury"

将功能文件中提到的所有步骤实现为 "loginstepdefnewtour.java"

package stepDef;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.chrome.ChromeDriver;

public class loginstepdefnewtour {

    WebDriver driver;

    @Given("^user is already on Login Page$")
    public void user_is_already_on_Login_Page() throws Throwable {
        System.setProperty("webdriver.chrome.driver","c:\Grid\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://newtours.demoaut.com/");
    }

    @When("^title of login page is new tour$")
    public void title_of_login_page_is_new_tour() throws Throwable {
        String title = driver.getTitle();
        System.out.println(title);
    }

    @Then("^user enters \"([^\"]*)\"$")
    public void user_enters_and(String arg1, String arg2) throws Throwable {
        driver.findElement(By.name("userName")).sendKeys("sunil");
    }
}

和 "testrunner.java" 等同于

package myRunner;

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

//@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/main/java/features/loginnewtour.feature",
        glue={"src/main/java/stepDef/loginstepdefnewtour.java"}, 
        plugin= {"pretty","html:test-outout", "json:json_output/cucumber.json", "junit:junit_xml/cucumber.xml"}, 
        monochrome = true,
        strict = true, 
        dryRun = false
)

public class TestRunner extends AbstractTestNGCucumberTests{

}

您为粘合路径指定了错误的值。您需要指定要搜索的 Java 个包。

而不是

glue={"src/main/java/stepDef/loginstepdefnewtour.java"}, 

应该是

glue={"stepDef"}, 

还有一个方法签名错误

-- public void user_enters_and(String arg1, String arg2) throws Throwable {
public void user_enters_and(String arg1) throws Throwable {

编辑 项目中您提供的命名文件(请考虑遵循文件的 Java 命名约定)

src/main/java/stepDef/loginstepdefnewtour.java
src/main/java/features/loginnewtour.feature
src/test/java/myRunner/TestRunner.java

loginstepdefnewtour.java

package stepDef;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class loginstepdefnewtour {

    @Given("^user is already on Login Page$")
    public void user_is_already_on_Login_Page() throws Throwable { }

    @When("^title of login page is new tour$")
    public void title_of_login_page_is_new_tour() throws Throwable { }

    @Then("^user enters \"([^\"]*)\"$")
    public void user_enters_and(String arg1) throws Throwable { }
}

loginnewtour.feature

有你提供的内容。

TestRunner.java

打包 myRunner;

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

//@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/main/java/features/loginnewtour.feature",
        glue={"stepDef"}, 
        plugin= {
            "pretty","html:test-outout", 
            "json:json_output/cucumber.json", 
            "junit:junit_xml/cucumber.xml"
        }, 
        monochrome = true,
        strict = true, 
        dryRun = false
)

public class TestRunner extends AbstractTestNGCucumberTests{

}

执行测试

mvn compile test

产出

Running myRunner.TestRunner
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@6bf2d08e
Feature: New Tour Login Testing

  Scenario: Valid data testing           # src/main/java/features/loginnewtour.feature:2
    Given user is already on Login Page  # loginstepdefnewtour.user_is_already_on_Login_Page()
    When title of login page is new tour # loginstepdefnewtour.title_of_login_page_is_new_tour()
    Then user enters "mercury"           # loginstepdefnewtour.user_enters_and(String)

1 Scenarios (1 passed)
3 Steps (3 passed)