Interface test file download

At work, we often have questions about download interfaces: This type of interface generally has relatively stable functions, but it is also important and requires time for regression testing . Is there an alternative?

       The answer is definitely yes. You can intervene from interface testing/ UI automated testing . This article mainly talks about interface testing and provides two methods.

General implementation of file download interface:

        A. In general projects, file storage/downloading is a capability provided by the company's infrastructure. Most implementations obtain the URL of the downloaded file, and then the browser will perform the download operation (the effect is the same as directly inputting the file into the browser). Download URL), therefore, you need to first find the download URL of the file, and then simulate the download through a script/tool

        B. The download interface needs to be written by yourself to provide download capabilities.

        The above two methods have the same download principle. Essentially, they obtain the download URL and then let the browser perform the download. In actual projects, you can find the corresponding URL according to your own project type.

In this article, we use the method in B above (the download interface demo is written in python+django, and the download file information is hard-coded in the interface. How to implement it will be described later). The interface information is as follows:

  1. URL:http://127.0.0.1:8001/case/down

  2. 请求方式:不限

Method 1: Use postman, a must-have artifact for testing, to script the download interface

  1. Fill in the URL and headers and other information into postman, as shown below:

      2. Click send and download, and a system pop-up window will be called to rename the downloaded file.

    Click Save to download successfully

Method 2: Use python+requests to write interface test scripts

             When the file is downloaded, it is transferred in binary format, and the sream=True parameter needs to be set.

             Write the binary content to the file in the download directory

1 import requests
2 from contextlib import closing
3 
4 url = 'http://127.0.0.1:8001/case/down'
5 
6 with closing(requests.get(url, stream=True)) as response:
7     with open("文件名1.py", "wb") as file:
8         for data in response.iter_content(128):
9             file.write(data)

 As shown in the picture, the file download is successful.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/133273294