ehsan shirzadi
ehsan shirzadi

Reputation: 4859

Karate - How to call a function in a loop?

How to call my login function in a loop?

Feature: Validate correct user login
    Background:
        * call read('classpath:cleanup.feature')
        * def login = call read('classpath:account/init/init.user.feature')

Upvotes: 4

Views: 5864

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Refer to the documentation on data-driven features. So if you have a JSON array, you can do something like this:

* def users = [{ username: 'foo1', password: 'bar1' }, { username: 'foo2', password: 'bar2' }]
* def login = call read('classpath:account/init/init-user.feature') users

edit: since the question was not clear, adding a comment and one more example:

And inside init-user.feature you can just do * print __arg

Here is a an alternate way to iterate over a JSON array using a plain JavaScript function:

* def users = [{ username: 'foo1', password: 'bar1' }, { username: 'foo2', password: 'bar2' }]
* def fun = function(array){ for (var i = 0; i < array.length; i++) karate.log(array[i]) }
* call fun users

Upvotes: 2

Related Questions