当前位置:网站首页>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
边栏推荐
- 万字总结:分布式系统的38个知识点
- STC8H开发(十五): GPIO驱动Ci24R1无线模块
- AI识万物:从0搭建和部署手语识别系统
- Application of Acrel5000web Energy Consumption System in a College-Susie Week
- STC8H开发(十五): GPIO驱动Ci24R1无线模块
- 6 g underwater channel modeling were summarized based on optical communication
- DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine reduce the immunogenicity of peptides
- 几种绘制时间线图的方法
- APP自动化测试框架-UiAutomator2基础入门
- 2021(ICPC)亚洲区域赛昆明站(CGHIJLM)
猜你喜欢

XXE-XML外部实体注入-知识点

CMake installation upgrade higher version

筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
![[corctf 2022] section](/img/03/ee1ead55805a2ac690ec79c675c3e6.png)
[corctf 2022] section

Ankerui supports Ethernet communication, profibus communication embedded energy meter APM guiding technical requirements-Susie Week
![[corctf 2022] 部分](/img/03/ee1ead55805a2ac690ec79c675c3e6.png)
[corctf 2022] 部分

微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置

威纶通触摸屏制作自定义弹出窗口的具体方法(3种)

DSPE-PEG-Azide, DSPE-PEG-N3, phospholipid-polyethylene glycol-azide can react directly with DBCO

PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
随机推荐
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
如何让您的公司内容满足 GDPR 合规性
C语言预处理命令是什么?
Characteristics and Development Prospects of Korea's Cyber Security System
STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
自监督学习 —— MoCo v2
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
leetcode:数组中的第K个最大元素
How to fix Windows 11 not finding files
An overall security understanding and method of cyberspace based on connection and security entropy
NetCore路由的Endpoint模式
PHP 二维数组根据某个字段排序
URL Protocol web page to open the application
Daily practice of PMP | Do not get lost in the exam -8.8 (including agility + multiple choice)
Jensen (琴生) 不等式
cadence中复制一部分PCB到另一个PCB中去
mysql多表左链接查询
Optimization of SQL Statements and Indexes
np中的round函数,ceil函数与floor函数