Postman gets the return value and the usage of tests[]

1. Question:

When implementing automatic interface testing, you will often encounter the problem of interface parameter dependence. For example, when you call the login interface, you need to get the login key value first, and the key value returned by each request is different, then this In this case, if you want to automate the interface, you must use the function of setting environment variables in postman.

In postman, you can use tests to set the response returned by the interface as an environment variable for subsequent interface use (similar to the concept of parameterization)

2. A collection of common postman methods:

1. Set environment variables

postman.setEnvironmentVariable("key", "value");
pm.environment.set("key", "value");

Practice:

You can use pm.environment.get("test"); to view the value

View:

 

2. Set global variables

postman.setGlobalVariable("key", "value");

pm.globals.set("variable_key", "variable_value”);

Practice:

postman.setGlobalVariable("gv", "111111");

pm.globals.set("g", "2222");

View:

3. Check whether a string is contained in the resonse body

tests["Body matches string"] = responseBody.has("string_you_want_to_search");


pm.test("Body is correct", function () {

    pm.response.to.have.body("response_body_string");

});

Practice:

4. Check whether a value in JSON is equal to the expected value

var jd= JSON.parse(responseBody);

tests["pageSize "] = jd.pageSize === 20;

 

The JSON.parse() method converts the json string into an object. parse() is a safe function to check the json format. 

5. Convert XML body to JSON object

var jsonObject = xml2Json(responseBody);

tests["Body is correct"] = responseBody === "response_body_string";

6. Test whether an element in the response Headers exists (such as: Content-Type)

//getResponseHeader()方法会返回header的值,如果该值存在

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

The above method is not case sensitive. The following method is case sensitive. 

7. Verify the value of Status code

tests["Status code is 200"] = responseCode.code === 200;


pm.test("Status code is 200", function () {

pm.response.to.have.status(200);

});

 

8. Verify that the Response time is less than a certain value

tests["Response time is less than 200ms"] = responseTime < 200;


pm.test("Response time is less than 200ms", function () {

    pm.expect(pm.response.responseTime).to.be.below(200);

});

9. Does the name contain a certain value?

tests["Status code name has string"] = responseCode.name.has("OK");


pm.test("Status code name has string", function () {

    pm.response.to.have.status("OK");

});

 

10. Is the status response code of the POST request a certain value?

tests["Successful POST request"] = responseCode.code === 200 || responseCode.code === 202;


pm.test("Successful POST request", function () {

    pm.expect(pm.response.code).to.be.oneOf([200,202]);

});

11. A small 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);

12. Get request.data value

var Json = JSON.parse(request.data);

data {object}:
this is a dictionary of form data for the request. (request.data["key"]=="value")
headers {object}:
this is a dictionary of headers for the request (request.headers["key"]=="value")
method {string}:
GET/POST/PUT etc.
url {string}:
the url for the request.
假设requestBody中有"loginName: "10001";这个值,如果想获取到loginName的value值,代码如下

var jd = JSON.parse(request.data);

console.log(jd);

var loginName = jd.loginName;

console.log(loginName);

Practice:

13、JSON.parse()和JSON.stringify()

JSON.parse() [parse a json object from a string]-convert the string to an object

JSON.stringify() [parse out a string from an object, mainly for the conversion of [object object] type data]-convert the object to String

Practice:

14. Determine whether the field is empty

var jd = JSON.parse(responseBody);

if(typeof(jd.data) != "undefined" );

15. Methods to get variables in pre-request and tests:

Variables can be used in pre-request and test script. Because these parts are written through JavaScript

You can initialize and retrieve these variables in different ways. You can initialize variables in the script and place them in a specific scope

1. Define a variable in the script 

Set a variable in the script according to the predetermined range of the variable through pm.environment.set("variable_key", "variable_value"); method or pm.globals.set("variable_key", "variable_value"); method, this method requires Provide the key and value of the variable to set the variable. When you send the request, the script will be executed and the value will be stored in the variable, as shown below:

2. Take a predefined variable

Once a variable is set, you can use pm.environment.set("variable_key", "variable_value");;; or pm.globals.set("variable_key", "variable_value");;; According to the appropriate range to get variable. This method requires providing a variable name as a parameter to retrieve the stored value, as shown below:

 

Guess you like

Origin blog.csdn.net/LYX_WIN/article/details/108262154