从同一功能文件中的另一个场景调用常见场景,其中请求正文存储在空手道的 json 文件中

Calling common scenario from another scenario in same feature file where request body is store in json file in karate

我有点困惑,需要澄清一下

  Background:
    * def successBody = 'util/successRequestBody.json'

  @test1 @ignore
  Scenario: Verify user 
    Given url
    * def id = id
    * def requestBody = read (successBody)
    And request requestBody
    When method post
    Then status 201

  @test2
  Scenario: First create new user and then delete same user 
    * def id = '123'
    # First call POST user to create a new user
    * def postuser = call read('user.feature@test1') {id: id}
    Given url
    When method delete
    Then status 204

我在请求正文中提供价值,以便在 successRequestBody.json

中创建这样的新用户
{
  "id": "#(id)",
  "name": "abc"
}

上述方法无效。但是当我这样提供时,它就起作用了。请指导如何在从另一个调用功能时传递参数。我在调用 test1 时从 test2 传递变量名称 id 但在 test1 中它正在读取 id1 而不是 id?有人可以解释一下吗?

  Background:
    * def successBody = 'util/successRequestBody.json'

  @test1 @ignore
  Scenario: Verify user 
    Given url
    # I am setting variable name id from test2 but here it is reading id1 not id?
    * def id = id1
    * def requestBody = read (successBody)
    And request requestBody
    When method post
    Then status 201

  @test2
  Scenario: First create new user and then delete same user 
    * def id1 = '123'
    # First call POST user to create a new user
    * def postuser = call read('user.feature@test1') {id: id1}
    Given url
    When method delete
    Then status 204

call语法错误,必须使用内嵌表达式:

* def postuser = call read('user.feature@test1') { id: '#(id1)' }

这是一个提示。传递参数不是强制性的。 “调用者”中的变量将对“被调用”功能可见。下面这个和上面的效果一样。

* def id = id1
* def postuser = call read('user.feature@test1')

请仔细阅读文档和示例。如果仍然卡住,请按照以下过程操作:https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue