【问题标题】:How apply conditional assertion on the basis of environment in karate如何在空手道中根据环境应用条件断言
【发布时间】:2022-07-05 22:25:22
【问题描述】:

我已经阅读了多种解决方案并且对此感到厌烦。但是根据环境断言找不到任何东西。

Is it possible to use karate 'match' inside conditional statement?

我只想检查 ST 环境是否存在 userId 响应标头,因为这是基于环境的响应标头。

下面的断言通过了,但我很确定这不是正确的方法。即使我在这里传递数字代替字符串它也可以工作,并且 userId 是字符串而不是数字。

* def userId = karate.get("responseHeaders['user-id'][0]")
* if (env == 'ST') karate.match("userId == '#string'")

在下面也尝试过,许多其他选项都通过了,而其中一个应该失败,它说"userId == '#null'"

* if (env == 'ST') karate.match("userId != '#null'")
* if (env == 'ST') karate.match("userId == '#null'")
* if (env == 'ST') karate.match("userId contains 'u'")

【问题讨论】:

    标签: karate


    【解决方案1】:

    您错过的是 karate.match() 返回一个 JSON,您应该使用它来做有条件的事情或在需要时使测试失败。我希望下面的示例能告诉您您需要或可以做的所有事情:

    * def condition = true
    * def result = null
    * if (condition) result = karate.match('1 == 2')
    * print result
    * karate.fail(result.message)
    

    【讨论】:

    • 那么如何应用断言仅根据条件验证特定响应头不为空?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 2020-01-29
    相关资源
    最近更新 更多