程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Implementation of session persistence for python+pytest interface automation

編輯:Python

Catalog

Preface

One 、session( conversation )

Two 、 Conversation retention

3、 ... and 、python Conduct session Conversation retention

summary

Preface

In the process of interface test , It is often encountered that some interfaces need to be logged in to request , Otherwise, you will be prompted to log in , So how to solve it ? We can go through Cookie Bypass login , In fact, this is one of the ways to stay logged in . So today I want to talk about passing session Keep a conversation .

One 、session( conversation )

session, Conversational . So what is conversation ? Let's take a look at the life cycle of the session to get a general idea , as follows :

Start : client ( Usually a browser )--> Send the first request --> An application server , Successfully connected to each other , The session is created ;

In the session : The client then requests other resources of the application server ;

end : Close client ( Usually a browser ) Or session timeout , End of session .

Two 、 Conversation retention

Conversation retention , It can be understood as making the associated request sent by the same user in the same session without being disconnected . For example, use session Successfully logged in to a website , Use the... Again session Object requests other pages of the site , This will be used by default session Previously saved in cookie Wait for parameters to request , No need to log in again .

session The general principle of conversation retention is as follows :

When the client application requests the application server for the first time , The server will create one session, The session Object will store the properties and configuration information required for a specific user session ( Such as user information or login status ), And save it in the server .

establish session when , Will give it a session ID, The session ID Will be placed in set-cookie Returns to the client with the request , And keep it in the local cookie in .

follow-up , When the client requests other resources in this application , The server will receive cookie Medium session ID, And according to ID Find the previously created... In memory session object , If you can find it and it hasn't expired , Then it indicates that it is a request from the same user .

3、 ... and 、python Conduct session Conversation retention

In the interface automation test , In some cases, you can use session Session persistence mechanism to maintain login status , In this way, you don't need to log in every time you clear the interface .

Next , Let's take a look TesterHome Personal notification messages on the website 、 Take personal information as an example , To illustrate that in python How to use... In interface automation testing session Keep logged in .

Be careful :

Here, I'll try to request the login interface to construct session, Then use the session To request other interfaces , Still prompted to log in first , It shows that this method of the website is not feasible .

Because after logging in, you jump to the home page , So here we can use the request home page interface to construct session, You can then use the session To request other interfaces .

1、 First ,Fiddler Capture the information when requesting the homepage interface after successful login cookie

 2、 Get cookie, Use the request home page interface to construct session object .

import requestsheaders = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36", "cookie": ' As shown in the screenshot above cookie, That is, when you jump after successful login , Request home page interface https://testerhome.com/ At the time of the cookie'}# Construct a global session object S = requests.session()# Use session The object is S After successful simulated Login, request the home page interface , to update Sh_url = "https://testerhome.com/"h_res = S.get(url=h_url, headers=headers).text

This step will get session object S It contains the information after successful login cookie Wait for the information , Use this again session Object to request other interfaces, there is no need to log in again .

3、 Use what you got in the previous step session object , View personal notification messages 、 Personal information interface .

# Use session object S Request personal notification message interface n_url = "https://testerhome.com/notifications/personal"n_res = S.get(url=n_url).textprint(n_res)# Use session object S Request personal information interface s_url = "https://testerhome.com/setting"s_res = S.get(url=s_url).textprint(s_res)

Request personal information interface , The results are as follows :

 4、 The complete code is as follows :

import requestsheaders = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36", "cookie": ' As shown in the screenshot above cookie, That is, when you jump after successful login , Request home page interface https://testerhome.com/ At the time of the cookie'}# Construct a global session object S = requests.session()# Use session The object is S After successful simulated Login, request the home page interface , to update Sh_url = "https://testerhome.com/"h_res = S.get(url=h_url, headers=headers).text# Use session object S Request personal notification message interface n_url = "https://testerhome.com/notifications/personal"n_res = S.get(url=n_url).textprint(n_res)# Use session object S Request personal information interface s_url = "https://testerhome.com/setting"s_res = S.get(url=s_url).textprint(s_res) summary

session And cookie It's a different mechanism .

The same thing : Both can record the user's status , And they are all generated by the server .

Difference :cookie It is stored in the local client , and session Then it is stored in the server .

There is a connection between the two :session Session persistence mechanism needs to rely on cookie, because session ID Is stored in the cookie Medium .

This is about python+pytest Interface automation session This is the end of the article on the implementation of session persistence , More about python pytest session Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved