英文:
Karate - Use a variable in a schema validation
问题
我想使用正则表达式来检查响应的模式
响应必须以我的变量'myVar'的内容开头,并以任何内容结束
示例:
Scenario: 带变量的模式
* def myVar = 'https://google.com'
Given path 'path'
When method Get
And match response ==
"""
{
"id": "#uuid",
"name": "#string",
"url": "#regex ^#(myVar).*"
}
"""
但似乎无法正确检索我的变量内容
可以使用连接吗?
"url": "#regex ^" + #(myVar) + ".*"
感谢您的帮助
英文:
I'd like to check the schema of my response using a regex
The response must start with the content of my variable 'myVar' and end with anything
Example:
Scenario: Schema with variable
* def myVar = 'https://google.com'
Given path 'path'
When method Get
And match response ==
"""
{
"id": "#uuid",
"name": "#string",
"url": "#regex ^#(myVar).*"
}
"""
But it seems that the contents of my variable are not being retrieved correctly
its possible to use concatenation?
"url": "#regex ^" + #(myVar) + ".*"
Thanks for your help
答案1
得分: 0
我建议使用以下方式:
* def response = { url: 'foobar' }
* def myVar = 'foo'
* def isValidUrl = function(x){ return x.startsWith(myVar) }
* match response == { url: '#? isValidUrl(_)' }
参考链接:https://github.com/karatelabs/karate#self-validation-expressions
在#regex
中无法使用变量。
英文:
I recommend this instead:
* def response = { url: 'foobar' }
* def myVar = 'foo'
* def isValidUrl = function(x){ return x.startsWith(myVar) }
* match response == { url: '#? isValidUrl(_)' }
Refer: https://github.com/karatelabs/karate#self-validation-expressions
You can't use variables in a #regex
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论