当前位置:网站首页>Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
2022-08-09 23:11:00 【hog_ceshiren】
Cookie (plural form: Cookies) is the data stored on the user's local terminal by some websites in order to identify the user's identity.During the interface testing process, if the website adopts the cookie authentication method, the sent request needs to be accompanied by a cookie to get a normal response result.The same is true for interface automation testing. Cookies need to be added when constructing interface test cases.
Practical practice
A request from a website that wants to verify cookies will return an incorrect response if no cookie data is passed during the request.The following actual combat takes snowball as an example.
Scenario without cookie data
The example below initiates a get request to the url of the snowball without adding cookie data during the request.
>>> 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":"Encountered an error, please refresh the page or log in again and try again",\"error_uri":"/stock/search.json","error_code":"400016"}'>>>As you can see from the above example, if no cookie data is added during the request process, an error will be reported and an account login will be required.
Scenario for adding cookies
Next, add the correct cookie information in the request process to get the normal response information.
Python version
Also visit Snowball, passing the correct cookie data via the keyword argument cookies.
>>> 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":"...omit..."}>>> requests.get(url,params=params, headers=header, cookies=cookies).text'{"q":"sogo","page":1,"size":3,"stocks":[{"code":"SOGO","name":"Sogou","enName":"","hasexist":"false","flag":null,"type":0,"stock_id":1029472,"ind_id":0,"nd_name":"Communication business","ind_color":null,"_source":"sc_1:1:sogo"}]}'Java version
In Java, you can use the cookie() method to pass in the cookie data information that Snowball needs.
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", "...omit...").when().get("https://xueqiu.com/stock/search.json").then().statusCode(200).log().all();}}️ Copy the "link below" to improve the core competitiveness of the test!
Hello, if you like this article, remember to click "Like"!Your support is very important~(▽) PS: If you have any questions, please contact us
边栏推荐
- 定投的基金
- 编程时请选择正确的输入法,严格区分中英文
- FS4066耐高压1到4节内置MOS的锂电池充电管理芯片
- 10个 Istio 流量管理 最常用的例子,你知道几个?
- kvm虚拟机出现启动不了,NOT available,PV大于分区
- How to deal with keys when Redis is large?
- Two methods of implementing inverted strings in C language
- 微软word怎么转换成pdf文件?微软word转换为pdf格式的方法
- Skywalking系列学习之Trace Profiling源码分析
- 同步锁synchronized追本溯源
猜你喜欢
![[corctf 2022] section](/img/03/ee1ead55805a2ac690ec79c675c3e6.png)
[corctf 2022] section

windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)

Word怎么制作双面席卡?使用Word制作双面席卡方法

Optimization of SQL Statements and Indexes

Ali Ermi: Without accept, can a TCP connection be established?

论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》

hdu 1503 Advanced Fruits(最长公共子序列的应用)

Simulation of Water Temperature Control System Based on Fuzzy PID Controller

筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
![[Graphic and textual] How to reinstall Win7 system](/img/24/3acccb93e5e219f39477dc77229a58.png)
[Graphic and textual] How to reinstall Win7 system
随机推荐
TF生成均匀分布的tensor
【stack】【queue】【priority_queue】【deque】Detailed explanation
STC8H开发(十五): GPIO驱动Ci24R1无线模块
How to deal with keys when Redis is large?
What to do if Windows 11 can't find Internet Explorer
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
QGIS编译SIP的问题
leetcode:数组中的第K个最大元素
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
fixed investment fund
Cholesterol-PEG-Thiol, CLS-PEG-SH, Cholesterol-PEG-Sulfhydryl for improved solubility
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
LED闪烁 闪灯芯片IC 手电筒IC 闪灯控制IC 闪烁IC流水灯
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
6 g underwater channel modeling were summarized based on optical communication
定投的基金
上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具
Application of Acrel5000web Energy Consumption System in a College-Susie Week
Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?