【问题标题】:<Cucumber-JVM> How do I define "I Want" steps in cucumber using Java?<Cucumber-JVM> 如何在 Cucumber 中使用 Java 定义“我想要”的步骤?
【发布时间】:2016-09-01 22:20:28
【问题描述】:

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

我的黄瓜项目设置如下:

登录功能

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 cucumber-jvm gherkin cucumber-junit cucumber-java


    【解决方案1】:

    “我要测试.......”的位置不正确,不能被视为有效步骤。 Cucumber 认为它是对功能的描述,并没有做任何事情。如果您想要跨场景的初始通用步骤,您应该添加一个“背景”。

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

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

    如果只运行一个场景,则将其与其他步骤一起运行。

    【讨论】:

      【解决方案2】:

      您也可以这样做:

      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"
      

      【讨论】:

        【解决方案3】:

        “我想要”不是场景中的一个步骤,它是场景或故事叙述概述的一部分。

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

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

        我建议您看一下 BDD 中的“命令式与声明式 BDD”和“无处不在的语言”。一般来说,在编写 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/

        【讨论】:

          【解决方案4】:

          功能文件:

          背景:

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

          步骤定义类:

          @Given("I want to test on (.*)")
          
          public void I_want_to_test_on(String arg1)
          
          {
          
            //Here you can write your code..driver.get(arg1);
          
          }
          

          【讨论】: