1. Summary
This article explains how to use postman to do interface testing and batch interface testing.
2. Practice content
2.1 Setting of environment variables and global variables:
a. The setting method of environment variables is as shown in the figure below. Click to set an environment variable named "user_pwd", set the value of username and passwd in it, and then apply this environment variable "user_pwd" in the request.
Environment variables can be used in the following places:
- URL
- URL params
- Header values
- form-data/url-encoded values
- Raw body content
- Helper fields
Note: enclosing double curly braces around the variable name you want to use, only one environment variable can be applied per request.
The values of "username" and "passwd" in the figure below apply the environment variable "user_pwd", so in the body, just write the variables { {username}}, {
{passwd}} in the value corresponding to the key.
Note: If a project needs to test several environments, you can set an environment variable for different environments, such as setting an environment variable for "test environment", setting an environment variable for "stage environment", and setting an environment variable for "production environment". an environment variable.
b. The setting method of global variables is similar, as shown in the figure below, after clicking "Globals", a page similar to adding environment variables will appear to set variables and values.
Note: The global variable settings are applied to the requests in the entire collection (Collection), and do not need to be selected like environment variables.
2.2 Example of interface testing with Postman
Commonly used requests in interface testing are GET and POST, and these two requests are used as examples below.
The difference between GET and POST:
- GET uses URL or Cookie to pass parameters, while POST puts data in Body.
- GET URLs are limited in length, but POST is not.
- POST is relatively safer than GET because it is not visible in the address bar.
- Generally, POST requests are used to obtain data, and POST requests are used to send data.
Regarding the above differences, in fact, the first point is that POST can also put data in the URL, and there is actually no length limit for GET requests. POST requests seem to be implicit, but parameters can be obtained by capturing packets.
1. GET request:
Usually we use a url to access the page, which is the so-called get request.
Example 1. (stu_info interface: get information named xx)
Set the request method to GET, and enter the complete url at the same time, just like accessing a browser, or enter the interface url, click "Params" to enter the required key and value, and click "Send" to view the returned response in "Response" result.
Note: The content of the get request cannot be placed in the body, and the length is limited. Since a global variable is set for domain, the url
is replaced with the variable {{domain}}, and this global variable is applied in the following requests.
Example 2. (all_stu interface: get all user information)
In addition to the GET request in Example 1, there are some interfaces that not only send the key-value, but also send the Headers information when sending the GET request, so that it can be realized with the help of Postman. Take the following figure as an example, you need to add "Referer" information in Headers to get all user information.
2. POST request:
POST requests cannot be requested directly by typing in the browser like GET, and need to be completed with the help of tools.
How to use: Select the request method as post, enter the requested url, and enter the necessary "Authorization", "header" and "Body" data. The post request can send key-value, json format, file, etc.
For the use of "Authorization", it is often encountered that the Type is "Basic Auth", and then set the corresponding Username and Password.
The values of username and password here can be obtained by setting environment variables.
a. Use Postman to send key-value requests:
Take the login interface as an example, select the "form-data" format in the Body, enter the required key-value, and select the corresponding environment variable.
b. Use Postman to send a request in json format:
Take the add user interface as an example, select the "raw" format in the Body, input the json data according to the interface document, and select the environment variable that needs to be applied.
c. Request to send files with Postman:
Take the file upload interface as an example, select the "form-data" format in the Body, enter "file" in the key, select "File" as the type in the drop-down list on the right, and click "Choose Files" to upload local files.
2.3 Use of Pre-requestScript
For the use of environment variables and global variables, in addition to the methods mentioned above, you can also use the Pre-requestScript method.
Take the login interface as an example, set the environment variables "username" and "passwd" in "Pre-requestScript", select the "form-data"** format in the Body, and input the required key-value, the value is the variable { {username}}, { {passwd}}.
postman. setEnvironmentVariable (“key”, “value”);
postman. setGlobalVariable (“key”, “value”);
getEnvironmentVariable ("key");//Get the environment variable of the key
getGlobalVariable("key");//Get the global variable of key
2.4 Use of Tests
2.4.1. Application of Tests as test cases
Tests are mainly used to design use cases. For example, to test whether the returned result contains a certain string, you can use Tests. Take the gold_add interface as an example to write a test case to test whether the returned results contain, as follows:
If the response is successful, it returns PASS, and if it fails, it returns FAIL.
commonly used
The tests are as follows:
1. Check whether the response body contains a string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
Note: "Body matches string" must be unique.
2. Check whether a value in JSON is equal to the expected value
var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;
The JSON.parse() method converts the json string into an object. parse() will check the json format and is a safe function.
Such as: check the number of elements in an array in json (here check the length of programs)
var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;
3. Check whether the response body is equal to a string
4. Convert XML body to JSON object
var jsonObject = xml2Json(responseBody);`
tests["Body is correct"] = responseBody === "response_body_string";
5. Test whether an element in the response Headers exists (eg: Content-Type)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
//getResponseHeader() method will return the header value, if the value exists
or:
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
The above method is not case sensitive. The following methods are case sensitive.
6. Verify the value of the Status code
tests["Status code is 200"] = responseCode.code === 200;
7. Verify that the Response time is less than a certain value
tests["Response time is less than 200ms"] = responseTime < 200;
8. Does name contain a certain value
tests["Status code name has string"] = responseCode.name.has("Created");
9. Whether the status response code of the POST request is a certain value
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
10. Tiny JSON data validator
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
console.log(tv4.error);
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
2.4.2. Set environment variables and establish associations between multiple interfaces
Take the gold_add interface as an example. Since this interface has authority verification, the admin user is required to perform operations and cookies need to be added, so the login interface needs to be associated for use.
In the login request, add in "Tests"
var jsonData =JSON.parse(responseBody);//Get all the parameters returned in the body
postman.setEnvironmentVariable(“sign”,jsonData.login_info.sign);//Set the sign in the returned parameter as an environment variable
In this way, sign can be used as an environment variable and applied to the gold_add interface.
In the gold_add interface, { {username}} has been set in the environment variable "user_pwd", you can directly enter the variable name, { { sign}} dynamically obtains the "sign" value of the Response in the login interface, { {sign}} has been set Defined in "Tests" of the login request above.
2.5 Use Postman Interceptor to send requests with cookies
When using Postman to send a cookie request, it was found that the request could not be sent successfully, and the "Restricted Header (use Postman Interceptor)" prompt was displayed. After searching on the Internet, it turns out that the "Postman Interceptor" plug-in should be installed in Chrome, which allows postman to use the browser cookie of this website when sending requests.
After Chrome is installed, as shown in the figure below, a small icon in the figure below appears.
When using Postman, you need to open the Interceptor, as shown in the figure below, the icon in the red box should be highlighted, so that the above-mentioned prompt message will not appear when sending the Cookie request.
If the request is successful, you can view the Cookie value in "Cookies".
Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:
These materials 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, and I hope it can help you! Partners can click the small card below to receive