如何在Intellij上为Cucumber Java功能设置多个步骤定义

时间:2015-08-19 14:36:10

标签: intellij-idea cucumber bdd cucumber-jvm cucumber-java

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

.
├── 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社区。

如果您需要任何其他信息,请与我们联系。

2 个答案:

答案 0 :(得分:1)

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

在跑步者中,您可以定义胶水和功能的位置。 >>>胶水是步骤定义,功能是特征。

在你的情况下,你的步骤定义与功能相同,但你必须告诉跑步者。

在你的情况下它应该是

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

此致

答案 1 :(得分: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中,右键单击左侧项目视图中的文件夹,然后选择将目录标记为并选择相应的选项。