httprunner学习2-har2case录制生成脚本

前言

复制毁一生,录制穷三代,如果你只是因为不想写脚本,而去录制脚本,那我建议你还是别学录制了。
录制脚本,只是一个过渡,从0到1的一个过渡,如果让你直接写脚本,你会无从下手,可以将录制的脚本快速转化成httprunner脚本文件。
har2case可以将.har文件转化成yaml格式或者json格式的httprunner的脚本文件,生成.har格式文件可以借助fiddler或Charles抓包工具。
httprunner==1.5.8

环境准备

如果你已经安装过httprunner,那应该是自带了har2case包,如果没有的话,可以用pip安装

pip install har2case==0.3.1

查看版本号

har2case -V
0.3.1

-h查看帮助选项

C:\Users\dell>har2case -h
usage: har2case [-h] [-V] [--log-level LOG_LEVEL] [-2y] [-fmt FMT_VERSION]
                [--filter FILTER] [--exclude EXCLUDE]
                [har_source_file]

Convert HAR(HTTP Archive) to YAML/JSON testcases for HttpRunner.

positional arguments:
  har_source_file       Specify HAR source file

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show version
  --log-level LOG_LEVEL
                        Specify logging level, default is INFO.
  -2y, --to-yml, --to-yaml
                        Convert to YAML format, if not specified, convert to
                        JSON format by default.
  -fmt FMT_VERSION, --format FMT_VERSION
                        Specify YAML/JSON testcase format version, v2
                        corresponds to HttpRunner 2.2.0+.
  --filter FILTER       Specify filter keyword, only url include filter string
                        will be converted.
  --exclude EXCLUDE     Specify exclude keyword, url that includes exclude
                        string will be ignored, multiple keywords can be
                        joined with '|'

Fiddler抓包生成.har文件

以登录接口为案例,登录接口相关文档信息如下:

  • 访问地址:http://127.0.0.1:8000/api/v1/login/
  • 请求类型:POST
  • 请求头部:application/json
  • 请求参数:{"username":"test", "password":"123456"}

在Fiddler上发送接口请求后,抓包如下

httprunner学习2-har2case录制生成脚本_第1张图片

抓到这个请求后,右上角File->Export Sessions->Selected Sessions->Select Export Format->勾选HTTPArchive v1.1

httprunner学习2-har2case录制生成脚本_第2张图片

勾选HTTPArchive v1.1类型后,下一步导出为test_login_demo.har文件

har2case转yaml格式脚本

接下来将刚才生成的test_login_demo.har文件,使用har2case转成yam格式的脚本文件

har2case test_login_demo.har -2y

-2y参数是设置转成.yml格式的脚本,如果不加这个参数,默认转成json格式

D:\>har2case test_login_demo.har -2y
INFO:root:Start to generate testcase.
INFO:root:dump testcase to YAML format.
INFO:root:Generate YAML testcase successfully: test_login_demo.yml

查看刚才生的的test_login_demo.yml,内容如下

# 上海悠悠,QQ交流群:750815713
-   config:
        name: testcase description
        variables: {}
-   test:
        name: /api/v1/login/
        request:
            headers:
                Content-Type: application/json
                User-Agent: Fiddler
            json:
                password: '123456'
                username: test
            method: POST
            url: http://127.0.0.1:8000/api/v1/login/
        validate:
        -   eq:
            - status_code
            - 200
        -   eq:
            - headers.Content-Type
            - application/json
        -   eq:
            - content.code
            - 0
        -   eq:
            - content.msg
            - login success!
        -   eq:
            - content.username
            - test
        -   eq:
            - content.token
            - a95b077eb4b884b9186f60a47f37b4746c7c6a60

运行用例

.yml格式脚本生成后,接下来使用hrun运行用例

hrun test_login_demo.yml

D:\>hrun test_login_demo.yml
/api/v1/login/
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 437.79 ms, response_length: 109 bytes
INFO     start to validate.
ERROR    validate: content.token equals a95b077eb4b884b9186f60a47f37b4746c7c6a60(str)   ==> fail
c7dca34cc6ff93049985c44967f132c4146e995e(str) equals a95b077eb4b884b9186f60a47f37b4746c7c6a60(str)
ERROR    request:
headers: {'content-type': 'application/json', 'user-agent': 'Fiddler'}
json: {'password': '123456', 'username': 'test'}

ERROR    response:
status_code: 200
headers: {'Date': 'Sat, 21 Sep 2019 09:54:57 GMT', 'Server': 'WSGIServer/0.2 CPython/3.6.0', 'Content-Type': 'application/json', 'Vary': 'Accept, Cookie', 'Allow': 'POST, OPTIONS', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Length': '109'}
body: '{"code": 0, "msg": "login success!", "username": "test", "token": "c7dca34cc6ff93049985c44967f132c4146e995e"}'

F

======================================================================
FAIL: runTest (httprunner.task.TestCase)
/api/v1/login/
----------------------------------------------------------------------
Traceback (most recent call last):
  File "e:\python36\lib\site-packages\httprunner\task.py", line 27, in runTest
    self.test_runner.run_test(self.testcase_dict)
httprunner.exceptions.ValidationFailure

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\python36\lib\site-packages\httprunner\task.py", line 29, in runTest
    self.fail(repr(ex))
AssertionError: ValidationFailure()

----------------------------------------------------------------------
Ran 1 test in 0.468s

FAILED (failures=1)
INFO     Start to render Html report ...
INFO     Generated Html report: D:\reports\1569059697.html

D:\>

你会发现运行用例会失败,打开测试报告,会发现是token校验失败了,因为token每次都是动态生成的,所以token校验不能写死了

httprunner学习2-har2case录制生成脚本_第3张图片

解决办法很简单,去掉这个token校验即可

        -   eq:
            - content.token
            - a95b077eb4b884b9186f60a47f37b4746c7c6a60

生成json格式脚本

har2case默认生成json格式的脚本,因为个人更喜欢yaml格式,所以json格式写在后面了.

har2case test_login_demo.har

D:\>har2case test_login_demo.har
INFO:root:Start to generate testcase.
INFO:root:dump testcase to JSON format.
INFO:root:Generate JSON testcase successfully: test_login_demo.json

D:\>

生成的test_login_demo.json内容如下

# 上海悠悠,QQ交流群:750815713
[
    {
        "config": {
            "name": "testcase description",
            "variables": {}
        }
    },
    {
        "test": {
            "name": "/api/v1/login/",
            "request": {
                "url": "http://127.0.0.1:8000/api/v1/login/",
                "method": "POST",
                "headers": {
                    "User-Agent": "Fiddler",
                    "Content-Type": "application/json"
                },
                "json": {
                    "username": "test",
                    "password": "123456"
                }
            },
            "validate": [
                {
                    "eq": [
                        "status_code",
                        200
                    ]
                },
                {
                    "eq": [
                        "headers.Content-Type",
                        "application/json"
                    ]
                },
                {
                    "eq": [
                        "content.code",
                        0
                    ]
                },
                {
                    "eq": [
                        "content.msg",
                        "login success!"
                    ]
                },
                {
                    "eq": [
                        "content.username",
                        "test"
                    ]
                },
                {
                    "eq": [
                        "content.token",
                        "a95b077eb4b884b9186f60a47f37b4746c7c6a60"
                    ]
                }
            ]
        }
    }
]

filter和exclude过滤

你可以使用filter参数,过滤url包含xxx.com的内容,如只转包含127.0.0.1的url请求

$ har2case demo.har --filter 127.0.0.1

也可以使用exclude来过滤,除xxx.com以外的内容

$ har2case demo.har--exclude xxxx.com

复制毁一生,录制穷三代 上海-悠悠,QQ交流群:750815713

转载于:https://www.cnblogs.com/yoyoketang/p/11564028.html

你可能感兴趣的:(httprunner学习2-har2case录制生成脚本)