简体   繁体   English

将字符串列表作为 Cucumber 参数传递

[英]Passing list of strings as Cucumber parameter

I'm updating my cucumber version from old info.cukes to latest io.cucumber (v6.8.1) and some of my tests are failing now with java.lang.IllegalArgumentException at BuiltInParameterTransformer.java:114我正在将我的黄瓜版本从旧的info.cukes更新到最新的io.cucumber (v6.8.1) 并且我的一些测试现在失败了, java.lang.IllegalArgumentException at BuiltInParameterTransformer.java:114

How can I pass list of strings as parameter in new cucumber?如何在新黄瓜中将字符串列表作为参数传递?

I read this answer but it doesn't work for me.我阅读了这个答案,但它对我不起作用。 You can find below an example to reproduce my problem:您可以在下面找到一个示例来重现我的问题:

  Scenario Outline: Testing lists of strings
    When Great test <first> <second> <third>
    Examples:
      | first    | second                 | third            |
      | "simple" | "listOfParams"         | "another simple" |
      | "simple" | "list,OF,params"       | "another simple" |
    @When("^Great test \"(.*?)\" \"(.*?)\" \"(.*?)\"$")
    public void great_test(String string, List<String> string2, String string3) {
        System.out.println(string);
        System.out.println(string2); // I want to get here ["listOfParams"] and ["list", "OF", "params"]
        System.out.println(string3);
    }

You can define a parameter type for your list of strings:您可以为字符串列表定义参数类型:

    @ParameterType("(?:[^,]*)(?:,\\s?[^,]*)*")
    public List<String> listOfStrings(String arg){
        return Arrays.asList(arg.split(",\\s?"));
    }

Then you can either use this parameter type in a step definition with a Cucumber Expression:然后,您可以在带有 Cucumber 表达式的步骤定义中使用此参数类型:

    @Given("a list of strings \"{listOfStrings}\"")
    public void a_list_of_strings_cucumber_expression(List<String> list) {
        System.out.println(list);
    }

Or you can use the exact regular expression wrapped in a capture group:或者您可以使用包含在捕获组中的确切正则表达式:

    @Given("^a list of strings \"((?:[^,]*)(?:,\\s?[^,]*)*)\"$")
    public void a_list_of_strings_regex(List<String> list) {
        System.out.println(list);
    }

Note that you can't use .* because this is reserved for the anonymous parameter type.请注意,您不能使用.*因为这是为匿名参数类型保留的。 When using the anonymous parameter type Cucumber will guess what you want to do based on the type of the argument.当使用匿名参数类型时,Cucumber 会根据参数的类型猜测你想做什么。

And this works for Boolean , String , Numbers , ect because they have a single way to writ them down.这适用于BooleanStringNumbers ,因为它们有一种方法可以将它们写下来。 There are however many ways to write down a list of strings so Cucumber won't guess.然而,有很多方法可以写下字符串列表,这样 Cucumber 就不会猜测。

    // This will complain
    @ParameterType(value = ".*", preferForRegexMatch = true)
    public List<String> listOfStrings(String arg){
        return Arrays.asList(arg.split(",\\s?"));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM