当前位置:网站首页>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
边栏推荐
猜你喜欢
kvm虚拟机出现启动不了,NOT available,PV大于分区
[corctf 2022] section
RHEL7系统修复rm -rf /boot /etc/fstab
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
Definition and Basic Operations of Linear Tables
Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
Number of daffodils within a thousand
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles
定投的基金
Word箭头上面怎么打字
随机推荐
LeetCode26:删除有序数组中的重复项
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
PMP每日一练 | 考试不迷路-8.8(包含敏捷+多选)
hdu 3341 Lost's revenge(dp+Ac自动机)
LoRa无线技术在物联网应用市场的概况和发展
Simulation of Water Temperature Control System Based on Fuzzy PID Controller
Two methods of implementing inverted strings in C language
6个规则去净化你的代码
laravel 表迁移报错[通俗易懂]
Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
An overall security understanding and method of cyberspace based on connection and security entropy
Hessian Matrix 海森矩阵
Beat the interviewer, the CURD system can also make technical content
MySQL Notes-06 Basic SQL Operations
How to deal with keys when Redis is large?
【双链表增删查改接口的实现】
题解:Edu Codeforces 109(div2)
Problems with compiling SIP with QGIS
AI识万物:从0搭建和部署手语识别系统