D08_用例中提取数据:headers 响应头提取案例
在 HttpRunner 中提取数据时,根据界定符的方式进行提取,涉及到的内容包括如下:
- ["status_code", "encoding", "ok", "reason", "url"]
- cookies
- elapsed
- headers
- ["body", "content", "text", "json"]
在 HttpRunner 中,可以从响应头域(headers)中提取需要的数据。
访问百度首页抓包如图所示,在用例中将从这些头域中提取需要的内容。
编写测试用例:
- config:
name: TestCase
- test:
name: TestStep-1
request:
url: https://www.baidu.com/
method: GET
headers:
# 模拟浏览器进行请求的提交
User-Agent: 'ozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
extract:
# 从响应头域提取
- d1: headers.Bdpagetype
- d2: headers.Content-Encoding
- d3: headers.Expires
# 提取头域中的所有 cookies(拼接组成一个字符串)
- d4: headers.Set-Cookie
validate:
- eq: [$d1, '1']
- eq: [$d2, gzip]
# contains:包含,验证返回的 Expires 头域中,年份是否 2020 年
- contains: [$d3, "2020"]
# regex_match:正则匹配,验证是否有名称为 BAIDUID 的 cookie
- regex_match: [$d4, '.*(BAIDUID).*']