如何在空手道的特征文件中循环

How to loop with in the feature file in karate

您好,我正在尝试循环要素文件中的地图列表,下面是我的代码

test.feature
Feature:
 Background: 
   * def jsonPathTest = Java.type('com.test.utility.FileUtility').getNames()

 Scenario:
   * def jsonName = karate.get('jsonPathTest[' + __loop + ']')

低于错误

java.lang.RuntimeException: javascript evaluation failed: karate.get('jsonPathTest[' + __loop + ']')
    at com.intuit.karate.ScriptBindings.eval(ScriptBindings.java:115)
Caused by: javax.script.ScriptException: ReferenceError: "__loop" is not defined in <eval> at line number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)

如果我正在调用另一个包含其工作数据的功能文件:

test.feature
Feature:
 Background: 
   * def jsonPathTest = Java.type('com.test.utility.FileUtility').getNames()

 Scenario:
   * call read('test2.feature') jsonPathTest

test2.feature
 Feature:
  Scenario:
     * print __arg

但我只想在特征文件中循环,而不是调用另一个特征文件并传递数据。请建议我该怎么做。 提前致谢。

我个人不明白为什么你不愿意使用第二个特征文件来循环。实际上它促进了重用,空手道对此有很好的支持。

如果你真的坚持,你必须使用 Cucumber "native" 循环方式,称为 Scenario Outline。这是文档:https://github.com/intuit/karate#the-cucumber-way

请注意,缺点是您不能拥有动态行数。这似乎是你想要做的。如果您有动态行数 并且 您想要使用单个特征文件,这在空手道中不受支持。

两种方法的代码比较,参考这个例子:https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/search/dynamic-params.feature