当前位置:网站首页>技术分享 | 接口自动化测试如何处理 Header cookie
技术分享 | 接口自动化测试如何处理 Header cookie
2022-08-09 21:35:00 【hog_ceshiren】
Cookie(复数形态:Cookies)是某些网站为了辨别用户身份而储存在用户本地终端上的数据。在接口测试过程中,如果网站采取了 Cookie 认证的方式,那么发送的请求需要附带 Cookie,才会得到正常的响应的结果。接口自动化测试也是同理,需要在构造接口测试用例时加入 Cookie。
实战练习
对于要对 Cookie 进行验证的网站发起请求,如果在请求过程中没有传递 Cookie 数据,则会返回错误的响应结果。下面的实战以雪球为例。
没有 Cookie 数据的场景
下方示例对雪球的 url 发起一个 get 请求,在请求过程中没有添加 Cookie 数据。
>>> import requests
>>>
>>> url="https://xueqiu.com/stock/search.json"
>>> params={"code": "sogo", "size": "3", "page": "1"}
>>> header={ "Accept": "application/json",
... "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
... }
>>> r = requests.get(url, params=params, headers=header)
>>> r.text
'{"error_description":
"遇到错误,请刷新页面或者重新登录帐号后再试",\
"error_uri":"/stock/search.json",
"error_code":"400016"}'
>>>
由上面的例子可以看到,如果在请求过程中没有添加 Cookie 数据,结果报错,要求登录账号。
添加 Cookie 的场景
接下来,在请求过程中添加正确的 Cookie 信息,即可得到正常的响应信息。
Python 版本
同样访问雪球,通过关键字参数 cookies 传递正确的 Cookie 数据。
>>> import requests
>>>
>>> url="https://xueqiu.com/stock/search.json"
>>> params={"code": "sogo", "size": "3", "page": "1"}
>>> header={ "Accept": "application/json",
... "User-Agent": "Mozilla/5.0 \
(Macintosh; Intel Mac OS X 10_14_6)\
AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/77.0.3865.90 Safari/537.36",
... }
>>> cookies={
"xq_a_token":"...省略..."
}
>>> requests.get(url,
params=params, headers=header, cookies=cookies).text
'{"q":"sogo","page":1,"size":3,"stocks":
[{"code":"SOGO","name":"搜狗",
"enName":"","hasexist":"false","flag":null,
"type":0,"stock_id":1029472,"ind_id":0,
"nd_name":"通讯业务","ind_color":null,
"_source":"sc_1:1:sogo"}]}'
Java 版本
在 Java 中,可以使用 cookie() 方法传入雪球所需要的 Cookie 数据信息。
import static io.restassured.RestAssured.*;
public class Requests {
public static void main(String[] args) {
given().
params("code", "sogo", "size", 3, "page", 1).
cookie("xq_a_token", "...省略...").
when().
get("https://xueqiu.com/stock/search.json").
then().statusCode(200).log().all();
}
}
️ 复制“下方链接”,提升测试核心竞争力!
你好呀,喜欢这篇文章的话记得点个“赞”哦!大家的支持很重要~(▽) PS:有问题可以联系我们哦
边栏推荐
- C语言之实现倒置字符串的两种方法
- Interviewer: How to deal with Redis big key?
- 编程语言中,取余和取模的区别
- MySQL, which is asked on both sides of the byte, almost didn't answer well
- Word怎么制作一张标准的答题卡?
- windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
- Simulation of Water Temperature Control System Based on Fuzzy PID Controller
- 【stack】【queue】【priority_queue】【deque】详解
- 微软word怎么转换成pdf文件?微软word转换为pdf格式的方法
- Definition and Basic Operations of Sequence Tables
猜你喜欢

角度和弧度的相互换算

安科瑞支持以太网通讯、profibus通讯嵌入式电能表APM指导性技术要求-Susie 周

【云原生】4.2 DevOps 精讲篇

定投的基金

What are the benefits of enterprise data integration?How do different industries solve the problem of data access?

Word怎么制作一张标准的答题卡?

没有 accept,我可以建立 TCP 连接吗?

cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?

Ankerui supports Ethernet communication, profibus communication embedded energy meter APM guiding technical requirements-Susie Week

必看设计干货|易知微设计师是怎么做标准可视化设计服务的?
随机推荐
How to fix Windows 11 not finding files
[Essay] To the friends of the 19th issue
Characteristics and Development Prospects of Korea's Cyber Security System
Can I make a TCP connection without accept?
安科瑞无线物联网智能电表ADW300指导性技术要求-Susie 周
Daily practice of PMP | Do not get lost in the exam -8.8 (including agility + multiple choice)
定投的基金
Word第一页空白页怎么删除?删除Word第一页空白页方法教程
编程语言中,取余和取模的区别
Deceptive Dice(期望计算)
Two methods of implementing inverted strings in C language
DSPE-PEG-Azide,DSPE-PEG-N3,磷脂-聚乙二醇-叠氮可和DBCO直接反应
PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
Application of Acrel5000web Energy Consumption System in a College-Susie Week
C语言之实现倒置字符串的两种方法
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
DSPE-PEG-Azide, DSPE-PEG-N3, phospholipid-polyethylene glycol-azide can react directly with DBCO
FET Mosfet Leiditech corresponds to Infineon Infineon
SecureCRT 设置超时自动断开连接时长
威纶通触摸屏制作自定义弹出窗口的具体方法(3种)