[英]Passing a variable from one feature file into another as a part of request URL(not query parameter) in Karate
I have a feature that generates a vehicle id and is stored as a variable in the feature.我有一个功能可以生成车辆 ID,并作为变量存储在功能中。 I want to pass this id as a part of the request URL in another feature as a sort of a teardown activity.
我想将此 id 作为请求 URL 的一部分传递给另一个功能,作为一种拆解活动。
This is how I called it from a feature called activateVehicle.feature这就是我从一个名为 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)' }
In the called feature - deleteVehicle.feature在被调用的功能中 - 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
Am I right in the approach?我的方法正确吗? I want to reuse deleteVehicle.feature in other workflows as well and hence not doing this operation in the same activateVehicle.feature(which would have been very easy).
我也想在其他工作流程中重用 deleteVehicle.feature,因此不在同一个 activateVehicle.feature 中执行此操作(这本来很容易)。 I referred to the documentation too but it shows how we can use the variables in in the request body but not as a variable that can be used anywhere in the called feature.
我也参考了文档,但它显示了我们如何在请求正文中使用变量,而不是作为可以在被调用功能的任何位置使用的变量。 I don't want to use it in the request body (but want to use it as a part of the request URL) For example:
我不想在请求正文中使用它(但想将它用作请求 URL 的一部分)例如:
Scenario:
Given url loginUrlBase
And request { userId: '#(username)', userPass: '#(password)' }
I also referred to How can I call a variable from one feature file to another feature file using Karate API Testing .我还提到了How can I call a variable from a feature file to another feature file using Karate API Testing 。 I followed suit for a solution but am getting a javascript error:
我效仿寻求解决方案,但收到 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
Can someone kindly help and advise please?有人可以帮忙和建议吗?
Can you simplify your example?你能简化你的例子吗? The only thing I can make out is you need a space after the
call
feature and before the call argument:我唯一能弄清楚的是,在
call
功能之后和调用参数之前需要一个空格:
* call read('deleteVehicle.feature') { vehcileId: '#(vehicleId)' }
The pattern we generally recommend is to setUp not tearDown as tearDown has a risk of not executing if you had an error.我们通常推荐的模式是 setUp 而不是 tearDown ,因为如果出现错误,则 tearDown 有无法执行的风险。 That said, please see hooks: https://github.com/intuit/karate#hooks
也就是说,请参阅钩子: https://github.com/intuit/karate#hooks
Sometimes you should just keep it simple and call a feature (with args) only where you need it.有时你应该保持简单,只在你需要的地方调用一个特性(带参数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.