Cucumber Java - 如何在下一步中使用返回的String? [英] Cucumber Java - How to use returned String from a step in next step?

查看:256
本文介绍了Cucumber Java - 如何在下一步中使用返回的String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to automate some webservices, i create some methods for that and i want to use Cucumber for that but i can't figure how to use returned value in next step.

所以,我有这个功能:

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

这是使用从步骤返回的值的任何方式步骤?

It's any way to use the returned values from step to step?

谢谢!

推荐答案

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

I solved in other way, i know the question it's from last year but maybe someone will find this usefull in the future.

所以,我创建了一个'ClientsMap.java'来存储输出从最后一句话。
EX:

So, i created a 'ClientsMap.java' where i store the outputs from last sentences. EX:

public class ClientsMap {

private static ArrayListMultimap<Integer, String> 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;
    }

    }

然后我创建了一些特定的关键字用在以下句子中:

Then i created some specific keywords to be used in sentences like:

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

然后我有一个方法可以检查像'cardNumber'这样的关键字并检索这样的卡位置:

Then i have a method behind who check for keywords like 'cardNumber' and retrieve position of card like this:

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

在每个场景之后别忘了删除地图。

And after each Scenario don't forget to delete map.

这篇关于Cucumber Java - 如何在下一步中使用返回的String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆