温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How do I define "I Want" steps in cucumber using Java?
cucumber cucumber-java cucumber-jvm gherkin cucumber-junit

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

发布于 2020-05-03 02:07:28

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

我的 Cucumber 项目设置如下:

登录功能

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
}

查看更多

提问者
Katie
被浏览
11
Grasshopper 2016-09-02 21:49

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

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

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

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