Explain in detail, interface automation – the actual combat of Cookie authentication association interface of Requests

Directory

Foreword:

1. Introduction

2. Practical operation

1. Login interface

2. Query order interface

3. Add order interface

4. Modify the order interface

5. Delete order interface

3. Conclusion


Foreword:

Interface automated testing is an important part of the software testing process, and now more and more companies are beginning to use automated testing to improve testing efficiency and testing quality. In interface automation testing, the transfer of request parameters, the analysis of interface return data, and the judgment of status codes are all issues that testers need to pay attention to and deal with. Among them, interface authentication is relatively common, and cookie authentication is a commonly used authentication method. In this article, we will learn how to use the Requests library to implement cookie authentication and the actual operation process of associating interfaces.

1. Introduction

Requests is one of the most popular HTTP libraries in Python, which provides a simple and easy-to-use interface for sending HTTP/1.1 requests. In the test interface, Requests is widely used in HTTP request sending, interface performance testing, interface automation testing, etc.

2. Actual combat operation

In actual combat, we take an order management interface as an example to demonstrate the related operations of cookie authentication. This interface can only be accessed after logging in, and the data format returned by the interface is in JSON format. We will demonstrate how to use the Requests library for interface automation testing to complete order query, order addition, order modification and order deletion operations.

1. Login interface

Before performing authentication association, we need to log in to obtain the cookie for authentication. The request parameters of the login interface are user name and password, and the data returned by the interface contains a cookie similar to token. Here is the code demo:

import requests

# login interface
def login():
    url = 'http://xxxxx/login'
    user = {'username': 'test', 'passwor': '123456'}
    res = requests. post(url, data=user)
    # get cookies
    cookie = res. cookies. get('token')
    return cookie

2. Query order interface

Next, we will demonstrate how to use cookie authentication to implement the order query function. This interface needs to add Cookie to the request header. We can use the headers parameter of the Requests library to set the request header. The specific code is as follows:

import requests

# query order interface
def query_order(cookie):
    url = 'http://xxxxx/query_order'
    headers = {'Cookie': 'token=' + cookie}
    res = requests. get(url, headers=headers)
    data = res.json()
    return data

3. Add order interface

The new order interface needs to submit order information. Before that, we need to save the order information to be submitted in a dictionary, and then use the json parameter of the Requests library to submit the data in JSON format. Here is the code demo:

import requests

# Add order interface
def add_order(cookie):
    url = 'http://xxxxx/add_order'
    order_info = {'order_no': 'xxxxxx', 'order_amount': 100}
    headers = {'Cookie': 'token=' + cookie}
    res = requests. post(url, json=order_info, headers=headers)
    data = res.json()
    return data

4. Modify order interface

The modify order interface needs to provide the order ID and the modified order information. We can save the order ID and order information in a dictionary, and then use the put method of the Requests library to submit the modification request. Here is the code demo:

import requests

# Modify the order interface
def modify_order(cookie):
    url = 'http://xxxxx/modify_order'
    order_info = {'order_id': 1, 'order_no': 'yyyyyy', 'order_amount': 200}
    headers = {'Cookie': 'token=' + cookie}
    res = requests. put(url, json=order_info, headers=headers)
    data = res.json()
    return data

5. Delete order interface

The delete order interface needs to provide an order ID. We can save the order ID in a dictionary, and then use the delete method of the Requests library to submit a delete request. Here is the code demo:

import requests

# delete order interface
def del_order(cookie):
    url = 'http://xxxxx/del_order'
    order_info = {'order_id': 1}
    headers = {'Cookie': 'token=' + cookie}
    res = requests.delete(url, json=order_info, headers=headers)
    data = res.json()

return data

So far, we have completed the actual operation of the cookie authentication association interface. Below, we connect the entire code in series to implement a complete test case. The complete code is as follows:

import requests

# login interface
def login():
    url = 'http://xxxxx/login'
    user = {'username': 'test', 'passwor': '123456'}
    res = requests. post(url, data=user)
    # get cookies
    cookie = res. cookies. get('token')
    return cookie

# query order interface
def query_order(cookie):
    url = 'http://xxxxx/query_order'
    headers = {'Cookie': 'token=' + cookie}
    res = requests. get(url, headers=headers)
    data = res.json()
    return data

# Add order interface
def add_order(cookie):
    url = 'http://xxxxx/add_order'
    order_info = {'order_no': 'xxxxxx', 'order_amount': 100}
    headers = {'Cookie': 'token=' + cookie}
    res = requests. post(url, json=order_info, headers=headers)
    data = res.json()
    return data

# Modify the order interface
def modify_order(cookie):
    url = 'http://xxxxx/modify_order'
    order_info = {'order_id': 1, 'order_no': 'yyyyyy', 'order_amount': 200}
    headers = {'Cookie': 'token=' + cookie}
    res = requests. put(url, json=order_info, headers=headers)
    data = res.json()
    return data

# delete order interface
def del_order(cookie):
    url = 'http://xxxxx/del_order'
    order_info = {'order_id': 1}
    headers = {'Cookie': 'token=' + cookie}
    res = requests.delete(url, json=order_info, headers=headers)
    data = res.json()
    return data

if __name__ == '__main__':
    # get cookies
    cookie = login()
    # checking order
    query_result = query_order(cookie)
    print('query order result:', query_result)
    # add order
    add_result = add_order(cookie)
    print('New order result:', add_result)
    # Change Order
    modify_result = modify_order(cookie)
    print('modify order result:', modify_result)
    # delete order
    del_result = del_order(cookie)
    print('Delete order result:', del_result)

3. Conclusion

This article mainly introduces how to use the Requests library to implement the actual operation process of the Cookie authentication association interface. In practical applications, we also need to consider interface exception handling, performance testing, output and reporting of test results, etc.

[Automated test exchange]: 574737577http://qm.qq.com/cgi-bin/qm/qr?_wv=1027 &k=L7ciHIyOnE5wR-WjW99VYUQELBbNvwEi &authKey=d28p8qACMAvFA0DEZRwQEVTHiANFRKlRSSnAMBo7r nwuJexGvBynhSCyZ4CAZZz8 & amp;noverify=0 & amp;group_code=574737577

Interface automated testing: