教程:https://cn.httprunner.org/examples/testerhome-login/
Demo:
config:
name: testcase description
variables: {}
teststeps:
- name: /account/sign_in
request:
headers:
DNT: '1'
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
Turbolinks-Referrer: https://testerhome.com/topics
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
method: GET
url: https://testerhome.com/account/sign_in
extract:
X_CSRF_Token: <meta name="csrf-token" content="(.*)" />
validate:
- eq:
- status_code
- 200
- eq:
- headers.Content-Type
- text/html; charset=utf-8
- name: /account/sign_in
request:
data:
commit: 登录
user[login]: asdfasdfasdf1dfasdf6@163.com
user[password]: ashdfasdfasdfasd
user[remember_me]: '0'
utf8: ✓
headers:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
DNT: '1'
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
X-CSRF-Token: $X-CSRF-Token
X-Requested-With: XMLHttpRequest
method: POST
url: https://testerhome.com/account/sign_in
validate:
- eq:
- status_code
- 200
- eq:
- headers.Content-Type
- text/javascript; charset=utf-8
原因是下面这行代码:
X-CSRF-Token: $X-CSRF-Token
** 正确的写法*:
X-CSRF-Token: $ X-CSRF-Token
也就是说,$ 和变量之间需要又一个空格。