如何使用 java 从我的功能定义“我想要”步骤

我的 cucumber 项目设置如下:

Login.feature

Feature: User Login
    I want to test on "www.google.com"

Scenario: Successfully log in
    Given I am logged out
    When I send a GET request to "/login"
    Then the response status should be "200"

然后,我的步骤定义如下:

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

public class Steps {
    @Given("^I am logged out$")
    public void i_am_logged_out() {
        //do stuff
    }

    @When("^I send a GET request to \"([^\"]*)\"$")
    public void i_send_a_GET_request_to(String arg1) {
        //do stuff
    }

    @Then("^the response status should be \"([^\"]*)\"$")
    public void the_response_status_should_be(String arg1) {
        //do stuff
    }
}

如何使用Cucumber-jvm在Java中定义“我想要”步骤?

这是我的尝试,但是@When不是有效的注释。
@Want("to test on \"([^\"]*)\"$")
public void to_test_on(String arg1) {
    //do stuff
}

最佳答案

“我要测试.......”不在正确的位置,因此不被视为有效步骤。 Cucumber认为它是对该功能的描述,对此不进行任何操作。如果您希望跨方案进行初始的通用步骤,则应添加“背景”。

只需在该步骤之前添加“@Given”注释即可。

Background:
    @Given I want to test on "www.google.com"

否则仅在一种情况下运行会与其他步骤保持一致。

09-10 20:45
查看更多