2

我正在尝试为多个功能创建几个步骤定义类。这是我的项目结构:

.
├── CucumberPOC.iml
└── src
    └── test
        ├── CucumberRunner.java
        └── features
            ├── CheeseStepDefinition.java
            ├── StepDefinition.java
            ├── cheese.feature
            └── myfeature.feature

它是 CucumberRunner.java 类:

package test;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        format = {" pretty", "json:target/cucumber.js"},
        features = { "src/test/" }
)
public class CucumberRunner {
}

有两个步骤定义类。当我运行 cheese.feature 时出现错误:

/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java ...
Testing started at 9:26 AM ...

Undefined step: Given  I go to google

Undefined step: When  I ask for cheese

Undefined step: Then  I should see many offers


1 scenario (0 passed)


3 steps (0 passed)


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


You can implement missing steps with the snippets below:

@Given("^I go to google$")
public void I_go_to_google() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

@When("^I ask for cheese$")
public void I_ask_for_cheese() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

@Then("^I should see many offers$")
public void I_should_see_many_offers() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}


Process finished with exit code 0

但是这些步骤是在 CheeseStepDefinition 中定义的:

package test.features;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class CheeseStepDefinition {

    WebDriver driver = null;

    @Given("^I go to google$")
    public void I_go_to_google() throws Throwable {
        driver = new HtmlUnitDriver();
        driver.get("http://www.google.com");
    }

    @When("^I ask for cheese$")
    public void I_ask_for_cheese() throws Throwable {
        WebElement search = driver.findElement(By.name("q"));
        search.sendKeys("cheese");
        search.submit();
    }

    @Then("^I should see many offers$")
    public void I_should_see_many_offers() throws Throwable {
        System.out.println(driver.getTitle());
        driver.close();
    }
}

所以我不知道为什么黄瓜java看不到它的步骤定义。我需要做任何其他配置吗?我运行 myfeature.feature 一切正常。

工具信息。

我正在使用这个罐子:

.
├── cucumber-core-1.1.5.jar
├── cucumber-html-0.2.3.jar
├── cucumber-java-1.1.5.jar
├── cucumber-junit-1.1.5.jar
├── cucumber-jvm-deps-1.0.5.jar
├── gherkin-2.12.1.jar
├── hamcrest-all-1.3.jar
├── jsoup-1.8.3.jar
├── junit-4.12.jar
└── selenium-server-standalone-2.47.1.jar

IDE 是 Mac 上的 Intellij 14.1 Community。

如果您需要任何其他信息,请告诉我。

4

2 回答 2

1

像这样重新排列文件:

└── src
    └── test
        ├── java
            ├── <project>
                 ├── CheeseStepDefinition.java
                 ├── StepDefinition.java
                 └── CucumberRunner.java
        ├── resources
            ├── <project>
                 ├── cheese.feature
                 └── myfeature.feature

应该是您项目的目录名称。src/test/java将文件夹标记为test root. src/test/recources将文件夹标记为test resources root. 标记文件夹:在 IntelliJ 中,右键单击左侧项目视图中的文件夹,然后选择将目录标记为并选择适当的选项。

于 2018-01-11T16:11:15.303 回答
1

您需要在黄瓜选项中定义“胶水”选项。

在您的跑步者中,您可以定义胶水和特征的位置。>>> Glue 是stepdefinitions,feature 是特征。

在您的情况下,您的 stepdefinition 与功能位于同一位置,但您必须将其告诉跑步者。

在你的情况下,它应该是

@CucumberOptions(
    format = {" pretty", "json:target/cucumber.js"},
    features = { "src/test/"}, glue ={ "src/test/"  })

问候,

于 2016-06-23T10:55:31.733 回答