【问题标题】:Karate: Pass array into HTTP Request空手道:将数组传递给 HTTP 请求
【发布时间】:2019-11-03 04:44:31
【问题描述】:

这是ticket的延续。

我已经尝试过循环和数据驱动,所有的 id 都会一次传递到删除请求中。

Scenario: Get all users
        #Get all users
        Given path  'users'
        And header Authorization = authId
        And header Accept = 'application/json;version=2'
        When method GET
        Then status 200
        * def userids = karate.map(response, function(x){ var link = x.links[0].href; return link.substring(link.lastIndexOf('/') + 1) })
        Then print userids
        Given path 'users'
        And path {userid: '#(userids)'}
        When method DELETE
        Then status 204

我想一次只从数组中传递一个 id,发送将删除用户的请求,然后再次传递其他 id,直到同一 HTTP 请求中的数组长度为 1。

【问题讨论】:

  • 不,您还没有尝试过数据驱动。请再读一遍。您将需要第二个功能文件。另请参阅karate.mapWithKey() 将字符串数组转换为 json 对象数组。再试一次:github.com/intuit/karate#karate-mapwithkey

标签: karate


【解决方案1】:

让我介绍一下我能想到的可能方法,

您可以将用户 ID 列表生成部分移至 Background:,并使用 userIds 变量作为输入创建动态场景大纲。

Feature : delete user

Background:
        * url baseUrl
        Given path  'users'
        And header Authorization = authId
        And header Accept = 'application/json;version=2'
        When method GET
        Then status 200
        * def userids = karate.map(response, function(x){ var link = x.links[0].href; return link.substring(link.lastIndexOf('/') + 1) }) 
        * def userids = karate.mapWithKey(userids, "userId")

   Scenario Outline: Delete Users
        Given path 'users'
        And path userId
        When method DELETE
        Then status 204

        Examples:
            | userids |

【讨论】:

猜你喜欢
  • 2018-03-09
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多