如何在空手道中的特征文件中循环播放 [英] How to loop with in the feature file in karate
问题描述
我正在尝试在功能文件中循环显示地图列表,以下是我的代码
Hi i am trying to loop a List of Map in a feature file, below is my code
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)
如果我正在调用具有功能的数据的另一个功能文件:
If i am calling another feature file with data its working:
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
但是我只想在功能文件中循环而不是调用另一个功能文件并传递数据.请建议我该怎么做.提前致谢.
But i want to loop with in the feature file only instead of calling another feature file and passing data. Please suggest me how can i do this. Thanks in advance.
推荐答案
我个人不明白为什么您不愿意使用第二个功能文件循环播放.实际上,它促进重复使用,而空手道对此有很好的支持.
Personally I don't understand why you have this reluctance to use a second feature file to loop. Actually it promotes re-use and Karate has good support for this.
如果您真的坚持,则必须使用Cucumber的本机"循环方式,称为方案大纲".这是文档: https://github.com/intuit/karate#the-cucumber方式
If you really insist, you have to use the Cucumber "native" way of looping which is called a Scenario Outline
. Here is the documentation: https://github.com/intuit/karate#the-cucumber-way
请注意,缺点是您不能拥有动态的行数.这似乎是您要尝试执行的操作.如果您有动态的行和行,而您想使用单个功能文件,则空手道不支持该功能文件.
Note that the disadvantage is that you can't have a dynamic number of rows. Which seems to be what you are trying to do. If you have a dynamic number of rows and you want to use a single feature file, that is not supported in Karate.
For a comparison between the two approaches in code, refer this example: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/search/dynamic-params.feature
这篇关于如何在空手道中的特征文件中循环播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!