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

时间:2017-12-14 13:41:56

标签: selenium-webdriver cucumber-java

在IntelliJ编辑器中使用testNG运行黄瓜测试时出现“有未定义的步骤”错误,请指导缺少的步骤

错误输出为

 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 /主/爪哇/   特征      loginnewtour.feature   myRunner      TestRunner.java   stepDef      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{

}

1 个答案:

答案 0 :(得分:0)

您为胶水路径指定了错误的值。您需要指定要搜索的Java包。

而不是

<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aaa-bb-ccc">

应该是

<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="aaa-bb-ccc">

方法签名也是错误的

            try{
                $request->validate([
                    'aadhar' => 'required|digits:12|numeric',
                    'name' => 'required|string|max:511',
                    'dob' => 'required|date_format:Y-m-d',
                    'email' => 'required|email|max:255',
                    'address' => 'required|string',
                    'insuranceid' => 'required|digits_between:1,15|integer',
                    'password' => 'required|min:59|max:60',
                ]);
                }
            catch(Exception $error){
                    $message = $error->getMessage();
                    $status_code=400;
                    return response()->json(["message" => $message,"status_code" => $status_code]);
                }

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

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

<强> loginstepdefnewtour.java

glue={"stepDef"}, 

<强> loginnewtour.feature

是否有您提供的内容。

<强> TestRunner.java

打包myRunner;

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

执行测试

src/main/java/stepDef/loginstepdefnewtour.java
src/main/java/features/loginnewtour.feature
src/test/java/myRunner/TestRunner.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 { }
}