ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HttpRunner学习3--extract提取数据和引用

2019-11-01 21:04:09  阅读:518  来源: 互联网

标签:code 提取 eq 接口 sign content HttpRunner extract


前言

在HttpRunner中,我们要想从当前 HTTP 请求的响应结果中提取参数,可以通过 extract 关键字来实现。

测试场景

在这里,我将以一个学生充值金币的接口来模拟测试,这个接口在 Jmeter接口测试实例-牛刀小试 文章中有说明。

学生金币充值接口:http://doc.nnzhp.cn/index.php?s=/6&page_id=11

这个接口有权限验证,我们需要先通过接口A登录,然后在接口B中进行充值操作。

extract提取数据

- test:
    name: login case
    request:
      url: /api/user/login
      method: POST
      headers:
        Content-Type: application/x-www-form-urlencoded
        User-Agent: Fiddler
      data:
        username: test1010
        passwd: aA123456
    extract:
      - sign: content.login_info.sign
    validate:
      - eq: [status_code, 200]
      - eq: [content.error_code, 0]

在这个接口A中,通过 extract 关键字提取 sign 值,sign 将用于后续添加cookie进行身份验证。

 extract:
      - sign: content.login_info.sign

引用数据

- test:
    name: add gold
    request:
      url: api/user/gold_add
      method: POST
      headers:
        Content-Type: application/x-www-form-urlencoded
        User-Agent: Fiddler
        Cookie: test1010=$sign
      data:
        stu_id: 2114
        gold: 500
    validate:
      - eq: [status_code, 200]
      - eq: [content.error_code, 0]
      - eq: [content.msg, "操作成功!"]

在这个接口B中,先添加 cookie 完成身份验证( test1010 是上一接口登录的用户),然后进行充值金币操作。

若想使用 extract 提取出来的 sign,是 $sign 的形式进行引用。

Cookie: test1010=$sign

运行用例

完整的YAML格式用例如下(test_extract.yml):

- config:
    name: extract test
    request:
      base_url: http://api.nnzhp.cn

- test:
    name: login case
    request:
      url: /api/user/login
      method: POST
      headers:
        Content-Type: application/x-www-form-urlencoded
        User-Agent: Fiddler
      data:
        username: test1010
        passwd: aA123456
    extract:
      - sign: content.login_info.sign
    validate:
      - eq: [status_code, 200]
      - eq: [content.error_code, 0]

- test:
    name: add gold
    request:
      url: api/user/gold_add
      method: POST
      headers:
        Content-Type: application/x-www-form-urlencoded
        User-Agent: Fiddler
        Cookie: test1010=$sign
      data:
        stu_id: 2114
        gold: 500
    validate:
      - eq: [status_code, 200]
      - eq: [content.error_code, 0]
      - eq: [content.msg, "操作成功!"]

在当前 YAML用例 的目录下,执行命令:hrun test_extract.yml

运行用例

查看测试报告

查看报告

点击 log,查看报告详情,可以看到接口A中通过extract提取的数据 sign ,在接口B中引用成功。

标签:code,提取,eq,接口,sign,content,HttpRunner,extract
来源: https://www.cnblogs.com/wintest/p/11779526.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有