嗨,我正在尝试在功能文件中循环显示地图列表,以下是我的代码
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
但是我只想在功能文件中循环而不是调用另一个功能文件并传递数据。请建议我该怎么做。 提前致谢。
答案 0 :(得分:0)
我个人不明白为什么您不愿意使用第二个功能文件进行循环。实际上,它促进重复使用,而空手道对此有很好的支持。
如果您真的坚持,则必须使用Cucumber的“本机”循环方式,称为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