简体   繁体   English

在空手道中将变量从一个特征文件传递到另一个特征文件作为请求 URL(不是查询参数)的一部分

[英]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.

相关问题 空手道:将变量从一个特征文件传递到另一个作为查询参数 - Karate : Passing variable from one feature file to another as a query parameter 空手道 API 测试 - 将变量从一个特征文件传递到另一个 - Karate API Testing - Passing variable from one feature file to another 如何使用空手道 API 测试将变量从一个特征文件调用到另一个特征文件 - How can I call a variable from one feature file to another feature file using Karate API Testing 空手道将 csv 详细信息传递到另一个功能文件 - Karate passing csv details to another feature file 空手道:如果该值不为空,则从一个响应中将值作为查询参数传递 - Karate : passing value as query parameter from one response if that value is not null 在空手道中 - 从另一个特征文件调用的特征文件以及变量值 - In Karate - Feature file calling from another feature file along with variable value 如何传递 karate.prevRequest 和 response 是 arguments 从一个功能文件到另一个功能文件 - How to pass karate.prevRequest and response are the arguments from one feature file to another feature file 在网址中传递变量-空手道 - Passing Variable in url - Karate 如何从一个特征文件表中读取数据并传递值以在空手道的 *.json 文件中设置 json 参数? - How to read data from one feature file table and pass the value to set json parameter in a *.json file in karate? 在一个功能中设置空手道中的全局变量以在另一功能中使用 - Setting global variables in karate from one feature to be used in another feature
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM