如何在多行中实现步骤定义?

时间:2018-07-31 13:49:55

标签: scala cucumber gherkin

我正在使用Scala,Cucumber,Gherkin语言进行自动化测试。我的功能文件中使用小黄瓜语言编写了以下方案:

Scenario Outline: Do something
  When I do something  
  Then result should have field1 as "<filed_1>", field2 as "<filed_2>" and filed3 as "<filed_3>"

生成的stepdef是:

When("""^I do something$""") {
        // do operation
}
Then("""^result should haveresult should have field1 as "([^"]*)", 
    field2 as "([^"]*)" and filed3 as "([^"]*)"$""") {
     // do operation
}

这里 然后 语句在一行中包含120个以上的字符,我必须将其分成两部分。我不应该更改功能文件。

在两行中中断 然后

Then("""^result should haveresult should have field1 as "([^"]*)", 
      | field2 as "([^"]*)" and filed3 as "([^"]*)"$""".stripMargin)

它说您可以使用以下代码片段执行缺少的步骤:

Then("""^result should have field1 as "([^"]*)", field2 as "([^"]*)" and filed3 as "([^"]*)"$"""){ (arg0:String, arg1:String, arg2:String) =>
  //// Write code here that turns the phrase above into concrete actions
  throw new PendingException()
} 

我不确定应该采用哪种方法来将 Then 分解为多行。感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以将String分成两部分并将其添加到一起:

Then("""^result should have field1 as "(.*)", field2 as "(.*)" """ + 
  """and filed3 as "(.*)"$""") { (arg0:String, arg1:String, arg2:String) =>

  //etc
}

这与您要拆分然后累加的所有String相同:

"""1234567890""" == """12345""" + """67890""" // true