httprunner2.x--通过头域提取sessionid并进行关联
对于名称不固定的sessionid可以先获取头域中的Set-Cookie值,然后再将Set-Cookie值,在下个接口中headers以cookies的方式传入
具体实现
httprunnermanager_login.yml
name: httprunnermanager_login api # 获取固定名称sessionid base_url: http://127.0.0.1:8000 request: data: account: test password: 123456 url: /api/login/ method: POST headers: Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 # 禁止重定向 allow_redirects: False #validate: # - eq: ["status_code", 302]
index2.yml
name: index base_url: http://127.0.0.1:8000 request: url: /api/index/ method: GET headers: User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 cookie: $cookievalue #validate: # - eq: ["status_code", 200]
测试用例
config: name: 通过头域提取 base_url: http://127.0.0.1:8000 teststeps: - name: 第一步:调用登录接口,并提取cookies api: api/httprunnermanager_login.yml extract: - cookievalue: headers.Set-Cookie #从上面请求结果(响应)中提取 cookies validate: - eq: ['status_code', 302] - name: 第二步:传入cookievalue api: api/index2.yml extract: - s1: status_code #提取响应状态码 - s2: '\> (.*) \ \;\ ' # 通过正则提取页面中欢迎您:test validate: - eq: [$s1, 200] #“status_code” 进行HTTP响应状态码断言 - eq: [$s2, '欢迎您:test']
命令行查看打印执行情况
>hrun testcases\test_httprunnnermanagerlogin2.yml --log-level debug
在 HttpRunner 中可以选择五种日志模式,分别是:
DEBUG -> INFO -> WARNING -> ERROR -> CRITICAL
默认级别: INFO
其中:
debug: 级别最低,打印日志信息最为详细,可用于代码调试
info:打印正常信息
warn: 打印警告信息
error: 打印错误信息
critical: 打印严重信息
E:\MyHttpRunner>hrun testcases\test_httprunnnermanagerlogin2.yml --log-level deb ug INFO HttpRunner version: 2.4.3 INFO Start to run testcase: 通过头域提取 第一步:调用登录接口,并提取cookies INFO POST http://123.56.231.107:8000/api/login/ DEBUG request kwargs(raw): {'data': {'account': 'test', 'password': 123456}, 'headers': {'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': ' Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'}, 'allow_redirects': False, 'verify': True} DEBUG processed request: > POST http://123.56.231.107:8000/api/login/ > kwargs: {'data': {'account': 'test', 'password': 123456}, 'headers': {'Content -Type': 'application/x-www-form-urlencoded', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'}, 'allow_redirects': False, 'verify': True, 'timeout': 120} DEBUG ================== request details ================== url : 'http://123.56.231.107:8000/api/login/' method : 'POST' headers : {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36', 'Accept- Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Conten t-Type': 'application/x-www-form-urlencoded', 'Content-Length': '28'} body : 'account=test&password=123456' DEBUG ================== response details ================== ok : True url : 'http://123.56.231.107:8000/api/login/' status_code : 302 reason : 'Found' cookies : <RequestsCookieJar[Cookie(version=0, name='sessionid', value= 'erq5gzr0s846q56tk9xvvbjrfx0zywl2', port=None, port_specified=False, domain='123 .56.231.107', domain_specified=False, domain_initial_dot=False, path='/', path_s pecified=True, secure=False, expires=1622119211, discard=False, comment=None, co mment_url=None, rest={'HttpOnly': None}, rfc2109=False)]> encoding : 'utf-8' headers : {'Date': 'Thu, 27 May 2021 07:39:42 GMT', 'Server': 'WSGIServ er/0.2 CPython/3.6.5', 'Content-Type': 'text/html; charset=utf-8', 'Location': ' /api/index/', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Length': '0', 'Vary': 'C ookie', 'Set-Cookie': 'sessionid=erq5gzr0s846q56tk9xvvbjrfx0zywl2; expires=Thu, 27-May-2021 12:39:42 GMT; HttpOnly; Max-Age=18000; Path=/'} content_type : 'text/html; charset=utf-8' text : '' INFO status_code: 302, response_time(ms): 161.0 ms, response_length: 0 bytes DEBUG start to extract from response object. DEBUG extract: headers.Set-Cookie => sessionid=erq5gzr0s846q56tk9xvvbjrfx0 zywl2; expires=Thu, 27-May-2021 12:39:42 GMT; HttpOnly; Max-Age=18000; Path=/ DEBUG start to validate. DEBUG extract: status_code => 302 DEBUG validate: status_code equals 302(int) ==> pass . 第二步:传入cookievalue INFO GET http://123.56.231.107:8000/api/index/ DEBUG request kwargs(raw): {'headers': {'User-Agent': 'Mozilla/5.0 (Windows N T 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 S afari/537.36', 'cookie': 'sessionid=erq5gzr0s846q56tk9xvvbjrfx0zywl2; expires=Th u, 27-May-2021 12:39:42 GMT; HttpOnly; Max-Age=18000; Path=/'}, 'verify': True} DEBUG processed request: > GET http://123.56.231.107:8000/api/index/ > kwargs: {'headers': {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) A ppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36', 'cook ie': 'sessionid=erq5gzr0s846q56tk9xvvbjrfx0zywl2; expires=Thu, 27-May-2021 12:39 :42 GMT; HttpOnly; Max-Age=18000; Path=/'}, 'verify': True, 'timeout': 120} DEBUG ================== request details ================== url : 'http://123.56.231.107:8000/api/index/' method : 'GET' headers : {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36', 'Accept- Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'cookie ': 'sessionid=erq5gzr0s846q56tk9xvvbjrfx0zywl2; expires=Thu, 27-May-2021 12:39:4 2 GMT; HttpOnly; Max-Age=18000; Path=/'} DEBUG ================== response details ================== ok : True url : 'http://123.56.231.107:8000/api/index/' status_code : 200 reason : 'OK' cookies : <RequestsCookieJar[Cookie(version=0, name='sessionid', value= 'erq5gzr0s846q56tk9xvvbjrfx0zywl2', port=None, port_specified=False, domain='123 .56.231.107', domain_specified=False, domain_initial_dot=False, path='/', path_s pecified=True, secure=False, expires=1622119211, discard=False, comment=None, co mment_url=None, rest={'HttpOnly': None}, rfc2109=False)]> encoding : 'utf-8' headers : {'Date': 'Thu, 27 May 2021 07:39:42 GMT', 'Server': 'WSGIServ er/0.2 CPython/3.6.5', 'Content-Type': 'text/html; charset=utf-8', 'X-Frame-Opti ons': 'SAMEORIGIN', 'Content-Length': '18452', 'Vary': 'Cookie', 'Set-Cookie': ' sessionid=erq5gzr0s846q56tk9xvvbjrfx0zywl2; expires=Thu, 27-May-2021 12:39:42 GM T; HttpOnly; Max-Age=18000; Path=/'} content_type : 'text/html; charset=utf-8' text : '<!doctype html>\n<html class="no-js" lang="zh-CN">\n<head>\n \n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" content="IE =edge">\n <title>首页</title>\n <meta name="keywords" content="index">\n <meta name="viewport" content="width=device-width, initial-scale=1, maximum-sc ale=1, user-scalable=yes">\n <meta name="renderer" content="webkit">\n <me ta http-equiv="Cache-Control" content="no-siteapp"/>\n\n \n <meta name="ap ple-mobile-web-app-title" content="HttpRunnerManager"/>\n <link h ... OMITTED 17234 CHARACTORS ...' INFO status_code: 200, response_time(ms): 281.0 ms, response_length: 18452 b ytes DEBUG start to extract from response object. DEBUG extract: status_code => 200 DEBUG extract: \> (.*) \ \;\  => 欢迎您:test DEBUG start to validate. DEBUG validate: LazyString($s1) equals 200(int) ==> pass DEBUG validate: LazyString($s2) equals 欢迎您:test(str) ==> pass . ---------------------------------------------------------------------- Ran 2 tests in 0.534s OK DEBUG No html report template specified, use default. INFO Start to render Html report ... INFO Generated Html report: E:\MyHttpRunner\reports\1622101211.html
查看报告