1

I am calling the feature (which has some validations) in the loop N number of times. This code works and calls my feature 3 times.

* def xxx =
    """
      function(times){
         for(i=0;i<times;i++){
          karate.call('classpath:api/test/hello.feature');
        }
      }
    """

* call xxx 3

In the feature file I am calling, first line of code is:

* def someVariable = 0;
* def index = response[someVariable]
* some other code

I need someVariable to change based on the i index. For example, in the loop, first time feature is called * def someVariable = 0;Second time it is called * def someVariable = 1; Third time it is called * def someVariable = 2;

How can this be achieved? Or can I add this variable inside the JS loop? Or, maybe __loop can be used (looked at the examples, but was not able to implement it). Thanks in advance.

1
  • You can try passing the index as an argument while calling the feature file Commented Apr 9, 2021 at 5:36

2 Answers 2

1

Short answer (not recommended):

karate.call('classpath:api/test/hello.feature', { someVariable: i });

Recommended approach, read this part of the docs (if you can): https://github.com/intuit/karate#loops

Then read these answers also:

https://stackoverflow.com/a/56599134/143475

https://stackoverflow.com/a/60853173/143475

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking time sharing the solutions. i really appreciate it. First one works fine for my scenario, and that's exactly what I was looking for. I will review other ways as well, I have seen those solutions before, but they looked more complex to me.
1

I am not sure if I am not understanding correctly but why dont just pass the index variable to someVariable like

for(i=0;i<times;i++){
   someVariable = i
   ...
}

1 Comment

Yes, thats exactly what I wanted, for some reason it did not work. Peter shared a solution karate.call('classpath:api/test/hello.feature', { someVariable: i }) and it does work like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.