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

标签 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"

我有这个步骤:

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;
    }
}

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

谢谢!

最佳答案

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

为了实现这一点,我会改变你的步骤:

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();
        }
    }
}

我改变的是

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

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

关于java - Cucumber Java - 如何在下一步中使用返回的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43067617/

相关文章:

javascript - cucumber JS : Custom parameter types not matching

selenium - 如何处理 Cucumber AmbiguousStepDefinitions 异常?

java - 正在重新初始化的变量的值

maven - cucumber : no backend found when running from Spring Boot jar

java - Java 包是否等同于 .Net 程序集?

java - 如何仅访问大型 n*m 矩阵的指定部分作为子矩阵的数量

java - ImageView 只能工作一次

java - 这个 Java 语法(看起来方法是参数化的)是什么意思?

java - 在 Cucumber 中,是否可以以编程方式获取当前正在执行的步骤?

cucumber - 如何读取 Karate 框架功能文件中的响应 header value ?