【问题标题】:Call feature in a loop and have one variable in the called feature as iteration number循环调用特征并在被调用特征中有一个变量作为迭代次数
【发布时间】:2021-04-09 05:14:21
【问题描述】:

我在循环 N 次中调用了该功能(有一些验证)。此代码有效并调用了我的功能 3 次。

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

* call xxx 3

在我调用的功能文件中,第一行代码是:

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

我需要 someVariable 根据 i 索引进行更改。例如,在循环中,第一次调用* def someVariable = 0;第二次调用* def someVariable = 1; 第三次调用* def someVariable = 2;

如何做到这一点?或者我可以在 JS 循环中添加这个变量吗?或者,也许可以使用 __loop (查看了示例,但无法实现它)。提前致谢。

【问题讨论】:

  • 您可以尝试在调用功能文件时将索引作为参数传递

标签: javascript arrays iteration karate


【解决方案1】:

简答(不推荐):

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

推荐的方法,阅读这部分文档(如果可以的话):https://github.com/intuit/karate#loops

然后阅读这些答案:

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

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

【讨论】:

  • 感谢您花时间分享解决方案。对此,我真的非常感激。第一个适用于我的场景,这正是我想要的。我也会回顾其他方法,我以前见过这些解决方案,但它们对我来说看起来更复杂。
【解决方案2】:

我不确定我是否理解不正确,但为什么不直接将索引变量传递给 someVariable 之类的

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

【讨论】:

  • 是的,这正是我想要的,但由于某种原因它不起作用。 Peter 分享了一个解决方案 karate.call('classpath:api/test/hello.feature', { someVariable: i }) 它确实是这样工作的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
相关资源
最近更新 更多