当前位置:网站首页>Postman tutorial - scripting

Postman tutorial - scripting

2022-07-07 10:06:00 Splendid Qianyang 813

Catalog

1. If it is body The return is html, Want to put html Set the value of to the environment variable ;

2. If body The return is json, You want to set the returned value as an environment variable ;

3. If you want to token Set the value of to the environment variable ;

4. If you want to  params Set the value of to the environment variable :

5. If you want to responseBody Medium headers Set the value of to the environment variable ;

6. If you want to put the body Medium text、html、XML、json、JS Set to environment variable ;

7. If you want to responseBody Medium text、XML、json、JS Set to environment variable ;

8. take xml Format response convert to son Format

9. Clear a global variable

10. Clear an environment variable

11.response Include content

12. take xml Format response convert to son Format

13.response Equal to the expected content

14.json analysis key Check the value of

15. Check response Of header Whether the information has the tested field

16. Response time judgment

17. Set global variables

18. Set the environment variable

19. Judge status code


1. If it is body The return is html, Want to put html Set the value of to the environment variable ;


const $ = cheerio.load(responseBody);

pm.environment.set("yuyuedanbianhao", $('span').text());

2. If body The return is json, You want to set the returned value as an environment variable ;


var jsonData =JSON.parse(responseBody);// obtain body All parameters returned in

postman.setEnvironmentVariable("orderid-value",jsonData.result.orderId)

3. If you want to token Set the value of to the environment variable ;


var jsonData =JSON.parse(responseBody);// obtain body All parameters returned in

postman.setEnvironmentVariable("token",jsonData.result.token)

4. If you want to  params Set the value of to the environment variable :


5. If you want to responseBody Medium headers Set the value of to the environment variable ;


var data =postman.getResponseHeaders("token");

postman.setEnvironmentVariable("token",data)

6. If you want to put the body Medium text、html、XML、json、JS Set to environment variable ;


7. If you want to responseBody Medium text、XML、json、JS Set to environment variable ;


8. take xml Format response convert to son Format


Response body:Convert XML body to a JSON Object

The corresponding script :

var jsonObject = xml2Json(responseBody);

Parameters :( By default, no parameters need to be set , For interface response) transformable xml

9. Clear a global variable


Clear a global variable

The corresponding script :

postman.clearGlobalVariable("variable_key");

Parameters : Of variables to be cleared key

10. Clear an environment variable


Clear an environment variable

The corresponding script :

postman.clearEnvironmentVariable("variable_key");

Parameters : Of environment variables to be cleared key

11.response Include content


Response body:Contains string

The corresponding script :

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

Parameters : Expected content

12. take xml Format response convert to son Format


Response body:Convert XML body to a JSON Object

The corresponding script :

var jsonObject = xml2Json(responseBody);

Parameters :( By default, no parameters need to be set , For interface response) transformable xml

13.response Equal to the expected content


Response body:Is equal to a string

The corresponding script :

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

Parameters : expect response

14.json analysis key Check the value of


Response body:JSON value check

The corresponding script :

tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args

Parameters :test Replace the measured value ,args Replace the tested key

15. Check response Of header Whether the information has the tested field


Response headers:Content-Type header check

The corresponding script :

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

Parameters : expect header

16. Response time judgment


Response time is less than 200ms

The corresponding script :

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

Parameters : response time

17. Set global variables


Set an global variable

The corresponding script :

postman.setGlobalVariable("variable_key", "variable_value");

Parameters : The key value of the global variable

18. Set the environment variable


Set an environment variable

The corresponding script :

postman.setEnvironmentVariable("variable_key", "variable_value");

Parameters : Key value of environment variable

19. Judge status code


Status code:Code is 200

The corresponding script :

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

Parameters : Status code

 

 

 

 

 

 

 

原网站

版权声明
本文为[Splendid Qianyang 813]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130623333042.html