Karate – 在模式验证中使用变量

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

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.

huangapple
  • 本文由 发表于 2023年6月15日 23:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483450.html
匿名

发表评论

匿名网友

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

确定