Detailed explanation of the Requests module for interface automated testing

In Python, the system's own urllib and urllib2 both provide powerful HTTP support, but the API interface is indeed too difficult to use. As a higher-level encapsulation, Requests is worthy of its slogan - HTTP for Humans in most cases.

Let's take a look at the use of the Requests HTTP library in our interface automation testing.

send request

Sending network requests using Requests is very simple, and can often be done with one or two lines of code.

Requests is a third-party library that needs to be installed before using it:

Example of sending a GET request:

Obtaining the response data is also very simple and straightforward.

Example:

Requests automatically decode content from the server. Most unicode character sets can be decoded seamlessly. After a request is made, Requests makes an educated guess about the response encoding based on the HTTP headers. When accessing the response body, Requests uses its inferred text encoding. You can find out what encoding Requests use and change it using the response.encoding property.

Example of sending a POST request:

redirect,

Timeouts and proxies

The reason why Requests is called "HTTP for human" is because of its high level of encapsulation, one of which is that Requests will automatically handle the redirection of the server response.

Note:

If the allow_redirects parameter is False, it means that there will be no active redirection.

Sometimes the response time of the other party's website is too long, and we hope to complete the request within the specified time, or directly stop the request. Example:

Note:

timeout indicates the maximum number of seconds this request can wait. An error will be reported after the set time is exceeded.

In order to prevent mechanisms such as anti-crawlers, it is also very simple to apply a layer of proxy to Requests:

session object

Session objects are able to persist certain parameters across requests. It will also keep the cookie between all requests made by the same Session instance, using urllib3's connection pooling function during this period. So if multiple requests are sent to the same host, the underlying TCP connection will be reused, resulting in significant performance improvements.

Let's take the login request operation of Django Web application, which requires the csrf_token value in the previous GET request as an example:

postscript

Of course, the functions of Requests are more than just that. We also need to learn a lot when using Requests as an interface automation test based on the HTTP protocol. You can refer to the official documentation for more learning:

https://requests.readthedocs.io/en/master/

After practicing 30 practical projects of interface automation testing in 7 days, 28K people joined the byte testing position. [Automated testing/interface testing/software testing/performance testing/Jmeter]

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/qq_48811377/article/details/133207051