httprunner學習2-har2case錄製生成腳本

前言

複製毀一輩子,錄製窮三代,若是你只是由於不想寫腳本,而去錄製腳本,那我建議你仍是別學錄製了。
錄製腳本,只是一個過渡,從0到1的一個過渡,若是讓你直接寫腳本,你會無從下手,能夠將錄製的腳本快速轉化成httprunner腳本文件。
har2case能夠將.har文件轉化成yaml格式或者json格式的httprunner的腳本文件,生成.har格式文件能夠藉助fiddler或Charles抓包工具。
httprunner==1.5.8html

環境準備

若是你已經安裝過httprunner,那應該是自帶了har2case包,若是沒有的話,能夠用pip安裝python

pip install har2case==0.3.1json

查看版本號api

har2case -V
0.3.1app

-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文件

以登陸接口爲案例,登陸接口相關文檔信息以下:測試

在Fiddler上發送接口請求後,抓包以下this

抓到這個請求後,右上角File->Export Sessions->Selected Sessions->Select Export Format->勾選HTTPArchive v1.1url

勾選HTTPArchive v1.1類型後,下一步導出爲test_login_demo.har文件3d

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校驗不能寫死了

解決辦法很簡單,去掉這個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

相關文章
相關標籤/搜索