Cucumber Java – 如何在下一步中使用返回的String?

我需要自动化一些web服务,我为此创建一些方法,我想使用Cucumber,但我无法计算如何在下一步使用返回值。

所以,我有这个function:

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为每个场景重新创建步骤类,因此我不希望这些字段是静态的,因为这意味着它们的值会在场景之间泄漏

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

我用其他方式解决了,我知道去年的问题,但也许有人会在将来发现这个问题。

所以,我创建了一个’ClientsMap.java’,我存储了最后句子的输出。 EX:

 public class ClientsMap { private static ArrayListMultimap multimapCardNumber = ArrayListMultimap.create(); ... public static void addCardNumber(String cardNumberValue) { multimapCardNumber.put(multimapCardNumber.size(), cardNumberValue); } public static String returnSpecificCardNumber(int specificCardNumberPosition) { String returnedCardNumber = multimapCardNumber.get(specificCardNumberPosition - 1).get(0); return returnedCardNumber; } } 

然后我创建了一些特定的关键字,用于句子,如:

 And I want to make a card transaction with this parameters: | XXX | 9999 | | accountNumber | account1-client1 | | cardNumber | cardNumber1 | 

然后我有一个方法,谁检查像’cardNumber’这样的关键字,并检索卡的位置,如下所示:

 if (paramsList[i].startsWith("cardNumber")) { String cardNumberPosition = paramsList[i].replaceAll("[^0-9]", ""); paramsList[i] = returnSpecificCardNumber(Integer.valueOf(cardNumberPosition)); } 

并且在每个场景之后不要忘记删除地图。