当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
[Generic Programming] Full Detailed Explanation of Templates
Error when source install/setup.bash
蓝牙模块有哪些种类?BLE低功耗蓝牙模块有什么特点?
CMake installation upgrade higher version
技术分享 | 接口自动化测试如何处理 Header cookie
Simulation of Water Temperature Control System Based on Fuzzy PID Controller
蔚来杯2022牛客暑期多校训练营7 CFGJ
6 g underwater channel modeling were summarized based on optical communication
STC8H开发(十五): GPIO驱动Ci24R1无线模块
Ali Ermi: Without accept, can a TCP connection be established?
Hessian Matrix 海森矩阵
POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
XXE-XML外部实体注入-知识点
kvm虚拟机出现启动不了,NOT available,PV大于分区
hdu 1333 Smith Numbers(暴力思路)
自监督学习 —— MoCo v2
ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花
Word文档怎么输入无穷大符号∞
Photometric Stereo 光度立体法三维重建
Several ways to draw timeline diagrams









![[corctf 2022] 部分](/img/03/ee1ead55805a2ac690ec79c675c3e6.png)