如何在Karate功能文件中在match语句中使用索引(数组索引)。

huangapple go评论75阅读模式
英文:

how to use the index (array index) in the match statement in Karate feature file

问题

以下是我的特性:

特性:f1

场景:S1

  • def values = [1]
  • def fun =
    """
    function(values,index){
    karate.call('pass-array-index.feature',{arrayIndexValue:index})
    }
    """
  • karate.forEach(values, fun)

以下是我的另一个特性文件,特性名称为pass-array-index.feature

特性:f2

场景:s2

  • def data = [1]
  • print arrayIndexValue
  • 然后匹配associatedRules[arrayIndexValue] == 1

我的期望是arrayIndexValue变量将解析为0,但收到以下错误消息。

无法解析从位置1开始的令牌。期望?、'、0-9、*

src/test/java/research/pass-array-index.feature:6

使用match语句时,变量arrayIndexValue存在问题,无法解析。请建议。

英文:

Below is my feature

Feature: f1

Scenario: S1

 * def values = [1]
    * def fun =
"""
function(values,index){
karate.call('pass-array-index.feature',{arrayIndexValue:index})
}
"""
    * karate.forEach(values, fun)

Below is my another feature file and feature name is pass-array-index.feature

Feature: f2

  Scenario: s2
    * def data = [1]
    * print arrayIndexValue
    Then match associatedRules[arrayIndexValue] == 1

My Expectation was arrayIndexValue variable will resolve to 0 however got below error.

Could not parse token starting at position 1. Expected ?, ', 0-9, *

src/test/java/research/pass-array-index.feature:6

There is a issue with the variable arrayIndexValue with used with match statement and this is not getting resolved. Please suggest.

答案1

得分: 0

要强制评估 match 表达式为 JavaScript 而不是 JsonPath,请用圆括号括起来。应该可以这样做:

Then match (associatedRules[arrayIndexValue]) == 1

在这里解释:https://github.com/karatelabs/karate#json-lookup

英文:

To force match expressions to be evaluated as JS and not JsonPath, enclose in round brackets. This should work:

Then match (associatedRules[arrayIndexValue]) == 1

Explained here: https://github.com/karatelabs/karate#json-lookup

huangapple
  • 本文由 发表于 2023年7月13日 21:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定