在Cucumber场景中给出空格作为参数表的值

时间:2014-08-20 19:10:16

标签: cucumber cucumber-jvm gherkin

我使用Cucumber框架,WebDriver和Java进行自动测试。对于参数化测试,我使用参数表。而且我不知道如何将空格作为参数值。我开始使用关键字 - WHITESPACE。我的例子如下:

第一种方法

Scenario Outline: checking of sending feedback
Given User wants send feedback
When User enters to the feedback field the following text:
"""
<suggestion>
"""
And User presses on the 'Send' button
Then User should see message <message>

Examples:
|suggestion|message|
|| Suggestion's field is empty!|
|\n| Suggestion's field is empty!|
|\n text text text| Successfuly send!|
|WHITESPACE| Successfuly send!|

然后在步骤定义中:

@When("^User enters to the feedback field the following text:$")
public void user_enters_feedback(String textOfSuggestion) {
      textOfSuggestion.equals("WHITESPACE")?" ":textOfSuggestion;
      new pageSuggestion().inpSuggestion.sendKeys(textOfSuggestion)
}

同一场景的第二种方法

此外,我尝试使用Transformer,但它不起作用。

我创建了课程

class MyTransformer extends Transformer<String> {
    public String transform(String value) {
         value.equals("WHITESPACE")?" ":value;
         return value;
     }
}

然后在步骤定义中:

@When("^User enters to the feedback field the following text:$")
public void user_enters_feedback(@Transform(MyTransformer.class) String textOfSuggestion) {
      new pageSuggestion().inpSuggestion.sendKeys(textOfSuggestion)
}

如何更好地重复使用其他步骤或添加新关键字?

提前致谢。

1 个答案:

答案 0 :(得分:1)

你根本不需要做任何事情。考虑一下这个

Scenario Outline: Something
  Given I have "<param>"
  ...    
  Examples:
  |param|
  |     | #spaces
  ||      #empty

当param为空/空时,这很快乐地打印出“Empty ...”

@Given("^I have \"(.*?)\"$")
public void i_have(String arg1) {
  if (arg1.trim().equals("")) {
    System.out.println("Empty...")
  }
}