Cucumber Java - 如何在下一步中使用返回的字符串?

DET*_*T66 4 java cucumber cucumber-java

我需要自动化一些网络服务,我为此创建了一些方法,并且我想使用 Cucumber 来实现这一点,但我不知道如何在下一步中使用返回值。

所以,我有这个功能:

Feature: Create Client and place order

  Scenario: Syntax
    Given I create client type: "66"
    And I create for client: "OUTPUTVALUEfromGiven" an account type "123"
    And I create for client: "OUTPUTVALUEfromGiven" an account type "321"
    And I want to place order for: "outputvalueFromAnd1"
Run Code Online (Sandbox Code Playgroud)

我有这个步骤:

public class CreateClientSteps {


@Given("^I create client type: \"([^\"]*)\"$")
public static String iCreateClient(String clientType) {

    String clientID = "";
    System.out.println(clientType);
    try {
      clientID = util.createClient(clientType);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return clientID;

}

@And("^I create for client: \"([^\"]*)\" an account type \"([^\"]*)\"$")
public static String createAccount(String clientID, String accountType) {


    String orderID = "";
    try {
        orderID = util.createAccount(clientID,accountType);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return orderID;
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以一步步使用返回值吗?

谢谢你!

Tho*_*erg 6

步骤之间共享状态(这就是我解释您的问题的方式)不是通过检查返回值来完成的。这是通过在实例变量中设置值然后在另一个步骤中读取该实例变量来完成的。

为了实现这一目标,我将更改您的步骤:

public class CreateClientSteps {
    private String clientID;
    private String orderID;

    @Given("^I create client type: \"([^\"]*)\"$")
    public void iCreateClient(String clientType) {
        System.out.println(clientType);
        try {
            clientID = util.createClient(clientType);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @And("^I create for client: \"([^\"]*)\" an account type \"([^\"]*)\"$")
    public void createAccount(String clientID, String accountType) {
        try {
            orderID = util.createAccount(clientID, accountType);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我改变的事情是

  • 用于共享状态的两个字段 - 其他步骤可以稍后读取这些值
  • 非静态方法 - Cucumber 为每个场景重新创建步骤类,因此我不希望字段是静态的,因为这意味着它们的值会在场景之间泄漏

这是在同一类中的步骤之间共享状态的方式。还可以在不同类的步骤之间共享状态。情况有点复杂。询问您是否有兴趣。