前言
復制毀一生,錄制窮三代
,如果你只是因為不想寫腳本,而去錄制腳本,那我建議你還是別學錄制了。
錄制腳本,只是一個過渡,從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上發送接口請求后,抓包如下
抓到這個請求后,右上角File->Export Sessions->Selected Sessions->Select Export Format->勾選HTTPArchive v1.1
勾選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校驗不能寫死了
解決辦法很簡單,去掉這個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