【问题标题】:Calling common scenario from another scenario in same feature file where request body is store in json file in karate [duplicate]从同一功能文件中的另一个场景调用常见场景,其中请求正文存储在空手道的 json 文件中 [重复]
【发布时间】:2021-10-25 02:28:09
【问题描述】:

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

  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

【问题讨论】:

    标签: karate


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-15
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多