首页 > 解决方案 > 空手道:将 jsonPath 发送到另一个功能时,“设置”功能看不到该 jsonPath

问题描述

我有两个功能:第一个是我有参数的地方,第二个是我想从第一个功能发送参数的地方。

第一个特点:

Feature: test
@dev
  Scenario: test

    * def arguments = { value: '123.00', jspath: '..transferData.amount',consent_body: 'classpath:consent_domestic.json' }
    * def createConsentResult = call read('classpath:reuseable/features/changeConsentBody.feature') arguments

第二个特点:

@ignore
Feature: Change consent body - 1 parameter

  Background:
    * url ''
    * call read('classpath:reuseable/features/commonFunction.feature')

  @act
  Scenario : Change consent body - 1 parameter

    * path 'consents'
    * def consentBody = read(consent_body)
    * print "jsonPath: "+jspath
    * set consentBody jspath= value
    When request consentBody
    And method post
    Then status 201

这里* def consentBody = read(consent_body)karate 将 'consent_body' 视为一个变量并使用该变量的值。

这里* print "jsonPath: "+jspathkarate 将 'jspath' 视为一个变量,并使用该变量的值并打印:[print] jsonPath: ..transferData.amount

但是这里* set consentBody jspath = value的空手道没有将 'jspath' 视为一个变量,也没有使用这个变量的值。相反,空手道显示错误:

changeConsentBody.feature:17 - unexpected path: jspath

在这种情况下,空手道需要将“jspath”视为变量并使用该变量的值。

标签: karate

解决方案


抱歉,如果您真的希望人们有耐心去解决问题并理解问题,那么您的问题必须简化。以后请参考:https ://stackoverflow.com/help/mcve

也就是说,我在这里看到了两个明显的问题。=顺便说一句,请在标志的每一侧保留空白:

* set consentBody jspath = value

变量不适用于set命令。而且你不能使用 JsonPath。如果你有动态路径,你可以这样做,参考:https ://github.com/intuit/karate#eval

* def foo = {}
* def path = 'bar'
* eval foo[path] = 'baz'
* match foo == { bar: 'baz' }

如果您想“批量”更新数组,而不是..transferData.amount- 请使用以下karate.map()操作:

* def foo = [{},{}]
* def fun = function(x){ x.transferData = { amount: 100 }; return x }
* def res = karate.map(foo, fun)
* match res == [{ transferData: { amount: 100 } }, { transferData: { amount: 100 } }]

最后我敦促你仔细阅读这篇文章,尽量避免“通用”测试用例和使用call,它只会使事情不必要地复杂化:https ://stackoverflow.com/a/54126724/143475


推荐阅读