我有一个生成车辆ID的功能,并作为变量存储在该功能中。我想将此ID作为其他功能中的请求活动的一部分作为请求URL的一部分传递。
这就是我从一个称为ActivateVehicle.feature的功能中调用它的方式
Scenario : Activate a vehicle
* header X-API-Key = apiKey
* def result = callonce read('createVehicle.feature')
* def vehicleId = result.vId
# some workflow steps
........
........
........
# tear down - delete the vehicle created
* call read('deleteVehicle.feature'){ vehcileId: '#(vehicleId)' }
在调用的功能中-deleteVehicle.feature
Scenario: Delete a vehicle
* header X-API-Key = apiKey
* def myurl = 'https://xxx/vehicle'+ vehicleId +'?permanent=yes'
Given myurl
And request ''
When method delete
Then status 200
我对吗?我也想在其他工作流程中重用deleteVehicle.feature,因此不要在相同的activateVehicle.feature中执行此操作(这很容易)。我也参考了文档,但是它显示了如何在请求正文中使用变量,而不是如何将其用作可在调用功能中的任何位置使用的变量。我不想在请求正文中使用它(但想将其用作请求URL的一部分)例如:
Scenario:
Given url loginUrlBase
And request { userId: '#(username)', userPass: '#(password)' }
我也提到了How can I call a variable from one feature file to another feature file using Karate API Testing。我遵循了一个解决方案,但遇到了JavaScript错误:
feature.deleteVehicle: -unknown-:11 - javascript evaluation failed:
'https://xxx/vehicle'+ vehicleId +'?permanent=yes', ReferenceError: "vehicleId"
is not defined in <eval> at line number 1
feature.SVT: SVT.feature:80 - javascript evaluation failed: vehicleId: '#(vehicleId)' }, <eval>:1:14 Expected eof
but found }
vehicleId: '#(vehicleId)' }
^ in <eval> at line number 1 at column number 14
有人可以请帮助和建议吗?
答案 0 :(得分:1)
您可以简化示例吗?我唯一能看出的是,您需要在call
功能之后和调用参数之前添加一个空格:
* call read('deleteVehicle.feature') { vehcileId: '#(vehicleId)' }
我们通常建议的模式是不设置upUp Down,因为如果发生错误,tearDown可能会不执行。也就是说,请参见钩子:https://github.com/intuit/karate#hooks
有时候,您应该保持简单,只在需要时调用功能(带有args)。