Python login interface test problem record and solution (dry goods)


In a recent login interface test, the following scenarios were discovered. This time I will record the problems encountered and the resolution process:

1. When the login operation is found in the packet capture, three interfaces are requested successively, and each interface is associated

2. Cookie information needs to be retained for each request, and required for the next interface request

3. The data returned by each interface is not in json format, and additional operations are required to extract data

This document will start from these three points

1. First look at the situation of packet capture and general framework analysis

Request 3 interfaces, and then we will analyze them one by one

①On the whole, they are all get requests, and the common parameters are service, callback, and _. There are other parameters in Article 2 and Article 3.

②The two fields under data in the data returned by the first interface are the parameters of the second interface

③A certain field in the data returned by the second interface is a parameter of the third interface

Write a rough framework based on the above

#coding:utf-8
import requests,re
url = "xxxxx"
#tt就代替那个_作为参数了
def login1(service,callback,tt):
    pass
#三条接口相互依赖,第2个接口参数来源第1个接口返回值
def login2():
    pass
#三条接口相互依赖,第3个接口参数来源第2个接口返回值
def login3():
    pass

if __name__ == '__main__':
    service = 'xxxxxx'
    callback = 'xxxxxxx'
    tt = 'xxxxxxx'
    login3(service, callback, tt)

2. Then it is normal to write the get request

Since the return value here is not in json format, there is no need to write json.loads() like this, print it first

3. As mentioned above, lt and execution here are the parameters of the next interface, so their values ​​need to be extracted at this moment

This involves regular extraction

ps: Recommend a debugging website http://tools.jb51.net/regex/javascript

Python regular expression learning address: https://www.jb51.net/article/177521.htm

It is also simple to write here in python, see the remarks in the script below for details

def login1(service,callback,tt):
    print("开始执行login1")
    url = url1 + "/sso/login"
    params = {
    
    
        "service":service,
        "callback":callback,
        "_":tt
    }
    req = requests.get(url = url,params=params)
    print(req.text)
    #返回结果转换为字符串
    response_str = str(req.text)
    #正则提取字符串信息,返回列表,提取data下的信息
    m = re.findall(r'{.*?}',response_str)[0]
    #data下就是key-value形式,通过eval()将字符串转换为字典,然后字典中通过key提取到value值
    lt = eval(m)['lt']
    execution = eval(m)['execution']
    return lt,execution

4. Write the second interface and bring in the things returned by the first interface

def login2(service,callback,tt):
    lt,execution = login1(service,callback,tt)
    print("开始执行login2")

A 302 error was found during execution

302 error: redirect, representing a temporary transfer; it means that you visit URL a, but because of the interceptor on the server side or other code processing, you will be redirected to URL b.

When using requests to simulate login, directly using request.get(url) can easily cause 302 redirection. The reason is that cookies are not persistent (requests comes with cookie processing, but they are not persistent)

So here we need to store the cookie and use it

See the path for specific cookie usage: https://www.cnblogs.com/liuzhzhao/p/12114453.html

5. Know how to use it, so let's modify the code

The following script is just like a gourd, the cookie information of the first interface is saved for the second interface, and the cookie of the second interface is saved for the third interface.

It is finished as a whole, and it needs to be supplemented separately later.
The main knowledge points of this time: interface dependency call, cookie storage and use, python key information regular extraction


Finally: tester benefits

In the technology industry, you must improve your technical skills and enrich your practical experience in automation projects. This will be very helpful for your career planning in the next few years and the depth of your testing technology.

In the interview season of the Golden 9th and the Silver 10th, the job-hopping season, organizing interview questions has become my habit for many years! The following is my collection and sorting in recent years, the whole is organized around [software testing], the main content includes: python automation test exclusive video, Python automation details, a full set of interview questions and other knowledge content.

Don't be ashamed of getting an 8k salary, don't be complacent just because you get a salary of more than 20k, don't be complacent just because you get a 30-45 salary. Life is not to earn that little salary, what you need is to open a career.

May you and I meet and you will find something! Welcome to follow the WeChat public account: [Sad Spicy Article] Receive a 216-page software test engineer interview book for free. And the corresponding video learning tutorials are free to share!

Good article recommendation:

Ali is on the second side, it turns out that my understanding of automated testing is too shallow

Appium automation environment construction

After reading Daniel’s article, I won’t be afraid of slow server response.

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/112967813