空手道:获取所有用户并使用参考ID删除 [英] Karate: Get all users and delete with the reference id

查看:58
本文介绍了空手道:获取所有用户并使用参考ID删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试完成后,我想删除所有已创建的用户.我可以一次删除一个用户.我们可以使用条件逻辑来做到这一点吗?

After my tests are completed, I would like to delete all the users that were created. I am able to delete one user at a time. Can we do this using conditional logic?

Feature file:

Given path  'users'
    And header Authorization = authId
    And header Accept = 'application/json;version=2'
    When method Get
    Then status 200
    Then print 'Response Time: '+ responseTime + ' milliseconds'
    * def resp = $
    * def size = karate.sizeOf(resp)
    * print 'Number of users: ' + size

这给了我以下答复:

[
  {
    "firstName": "Edit User",
    "lastName": "API Test",
    "emailAddress": "edituserapitesting@gmail.com",
    "ada": true,
    "isDeleted": false,
    "alerts": [],
    "links": [
      {
        "href": "https://someurl/api/users/0219360d-5ca6-42af-9e9c-10be1e32d219",
        "rel": "self"
      }
    ]
  },
  {
    "firstName": "Create",
    "lastName": "Test",
    "emailAddress": "api@test.com",
    "ada": true,
    "isDeleted": false,
    "alerts": [],
    "links": [
      {
        "href": "https://someurl/api/users/5e3c9be1-2863-4a2d-85e9-966582b127ac",
        "rel": "self"
      }
    ]
  },
  {
    "firstName": "Create",
    "lastName": "Test",
    "emailAddress": "api@test.com",
    "ada": true,
    "isDeleted": false,
    "alerts": [],
    "links": [
      {
        "href": "https://someurl/api/users/b6feb126-bca2-43e6-ba2d-87aaae81fef0",
        "rel": "self"
      }
    ]
  },
  {
    "firstName": "Create",
    "lastName": "Test",
    "emailAddress": "api@test.com",
    "ada": true,
    "isDeleted": false,
    "alerts": [],
    "links": [
      {
        "href": "https://someurl/api/users/4f07f234-9606-4cf5-94ce-3a42fb11a6d7",
        "rel": "self"
      }
    ]
  }
]

我必须从每个json获取ID [Eg:4f07f234-9606-4cf5-94ce-3a42fb11a6d7],并将其传递给删除路径.

I have to get the id [Eg:4f07f234-9606-4cf5-94ce-3a42fb11a6d7] from each json and pass it to delete path.

推荐答案

请阅读有关转换的信息: https://github.com/intuit/karate#json-transforms

Please read about transforms: https://github.com/intuit/karate#json-transforms

现在在一行中,您可以获取一个ID数组:

Now in one line you can get an array of ids:

* def ids = karate.map(response, function(x){ var link = x.links[0].href; return link.substring(link.lastIndexOf('/') + 1) })

现在,您可以在循环或数据

Now you can use this in a loop or data driven test and do what you want.

这篇关于空手道:获取所有用户并使用参考ID删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆