英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论