Postman used to be a plugin for Chrome, but now you have to download the app to use it.
The following is the postman interface:
The use of each functional area is as follows:
- Shortcut area: The shortcut area provides common operation entries, including running a set of favorite test data, importing favorite test data shared by others (Import from file, Import from folder, Import from link, etc.), or creating new requests and favorites , environment variables, etc.
- Sidebar: Includes a search bar, Request history and favorites management.
- Functional area: Request request settings, view Response response results and test results, and save requests to favorites.
- Settings area: set and manage environment variables and global variables
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. An example of interface testing with Postman
**The 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 the data in the 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, some interfaces send the GET request in addition to sending the key-value, but also send the Headers information together, 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.
3. The 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, enter the required key-value, and 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
Fourth, the use of Tests
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 tests are as follows:
1. Check whether the response body contains a stringtests["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["Your test name"] = data.value === 100;
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 certain string
4. Convert the XML body to a 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. 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.
The above is a summary of the basic usage methods of the postman interface test.
Thanks to everyone who reads my article carefully, there is always a gift in exchange, although it is not a very valuable thing, if you can use it, you can take it away:
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