简体   繁体   中英

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 + ']')

Getting below error

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM