空手道 - 如何在循环中调用函数?

Karate - How to call a 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')

请参阅 data-driven features 上的文档。所以如果你有一个 JSON 数组,你可以这样做:

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

编辑: 问题没说清楚,加个评论,再举个例子:

init-user.feature 里面你可以做 * print __arg

这是使用普通 JavaScript 函数迭代 JSON 数组的另一种方法:

* 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