<Cucumber-JVM> 如何使用 Java 在黄瓜中定义 "I Want" 个步骤?

<Cucumber-JVM> How do I define "I Want" steps in cucumber using Java?

如何使用java从我的特征中定义"I Want"步骤

我的黄瓜项目设置如下:

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 中的 "I want" 步骤?

这是我的尝试,但 @When 不是有效的注释。

@Want("to test on \"([^\"]*)\"$")
public void to_test_on(String arg1) {
    //do stuff
}

"I want to test......." 的位置不正确,无法被视为有效步骤。 Cucumber 认为它是对功能的描述并且不对其进行任何操作。如果您想要跨场景的初始通用步骤,您应该添加 'Background'.

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

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

否则 运行 仅针对一种情况将其与其他步骤一起坚持。

您也可以这样做:

Feature: User login

Scenario: Successfully log in

Given I want to test on "www.google.com"
When I am logged out
Then I send a GET request to "/login"
And the response status should be "200"

"I want"不是场景中的一个步骤,它是场景或故事的叙述概述的一部分。

叙述: 在我的(角色) 我要(特征) 实现(利益)

该功能应包括多个由步骤组成的场景。

我建议你看看 BDD 中的 "Imperative vs declarative BDD" 和 "ubiquitous language"。一般来说,在编写 BDD 时,您应该以无处不在(通用而非技术)和声明式语言为目标。

Given I am logged out - Declarative style in ubiquitous language
When I send a GET request to "/login" - Imperative and geek domain language.
Then the response status should be "200" - Imperative and geek domain language.

用一种无处不在的语言

Given I am logged out
When I log in
Then the response is logged in

更好,通用的第三人称语言

Given an existing customer
When the customer authenticates 
Then the search page is shown

另请参阅:http://grammarist.com/spelling/log-in-login/

专题文件:

背景:

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

步骤定义 Class:

@Given("I want to test on (.*)")

public void I_want_to_test_on(String arg1)

{

  //Here you can write your code..driver.get(arg1);

}