F05_HttpRunner关联:使用提取到的 SessionID
我们要实现关联的目的是模拟用户身份访问接口,这就需要:
- 从前面步骤的响应中获取用户身份(本案例是保存在cookie中)
- 拿到该用户身份后访问其他接口资源
那么整个用例的基本结构就变为:
- config:
name: 测试用例
- test:
name: 测试步骤1
request:
url: http://xxxxxxx
extract:
# 提取cookies
p_cookies: headers.Set-Cookie
- test:
name: 测试步骤2
request:
url: http://xxxxxxx
headers:
# 将前面提取的 cookies 应用到本次请求中
cookie: $p_cookies
- test:
name: 测试步骤3
request:
url: http://xxxxxxx
headers:
# 将前面提取的 cookies 应用到本次请求中
cookie: $p_cookies
关联过程如图所示:
那么完整的一个用例如下:
- config:
name: open_danwei
- test:
name: index - 打开登录页面
request:
url: http://localhost/myweb/jxc/index.asp
method: GET
extract:
# 从上面请求结果(响应)中提取 cookies
- cookievalue: headers.Set-Cookie
validata:
- eq: [status_code, 200]
- test:
name: login - 提交用户名和密码,完成登录操作
request:
url: http://localhost/myweb/jxc/index.asp?action=login
method: POST
data:
username: admin
pwd: admin
headers:
# 把提取到的 cookies 附加到本次请求头域
cookie: $cookievalue
extract:
- framesetname: (.*)frameset(.*)
validata:
- eq: [status_code, 200]
- eq: [$framesetname, frameset]
- test:
name: get_danwei - 登录成功后,打开系统设置页面
request:
url: http://localhost/myweb/jxc/system/danwei.asp
method: GET
headers:
# 把提取到的 cookies 附加到本次请求头域
cookie: $cookievalue
extract:
- info: <form name=\"form1\" action=\"(.*)\">
validate:
- eq: [status_code, 200]
- eq: [$info, danwei_del.asp]
执行测试用例,成功。