简体   繁体   English

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

[英]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')

Refer to the documentation on data-driven features . 请参阅有关数据驱动功能的文档。 So if you have a JSON array, you can do something like this: 因此,如果您有JSON数组,则可以执行以下操作:

* 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 init-user.feature您可以执行* print __arg

Here is a an alternate way to iterate over a JSON array using a plain JavaScript function: 这是使用普通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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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