Postman常用方法集合

1、设置环境变量

  1. postman.setEnvironmentVariable("key", "value");

  2. pm.environment.set("key", "value");

实操:

可以用pm.environment.get("test”);查看值

查看:

 

2、设置全局变量

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

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

实操:

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

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

查看:

3、检查resonse body中是否包含某个string

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

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

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

  7.  
  8. });

实操:

4、检测JSON中的某个值是否等于预期的值

 
  1. var jd= JSON.parse(responseBody);

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

 

JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。 

5、转换XML body为JSON对象

 
  1. var jsonObject = xml2Json(responseBody);

  2.  
  3. tests["Body is correct"] = responseBody === "response_body_string";

6、测试response Headers中的某个元素是否存在(如:Content-Type)

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

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

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

上面的方法,不区分大小写。下面的方法,要区分大小写。 

7、验证Status code的值

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

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

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

  7.  
  8. });

 

8、验证Response time是否小于某个值

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

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

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

  7.  
  8. });

9、name是否包含某个值

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

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

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

  7.  
  8. });

 

10、POST 请求的状态响应码是否是某个值

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

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

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

  7.  
  8. });

11、很小的JSON数据验证器

 
  1. var schema = {

  2.  
  3. "items": {

  4.  
  5. "type": "boolean"

  6.  
  7. }

  8.  
  9. };

  10.  
  11. var data1 = [true, false];

  12.  
  13. var data2 = [true, 123];

  14.  
  15. console.log(tv4.error);

  16.  
  17. tests["Valid Data1"] = tv4.validate(data1, schema);

  18.  
  19. tests["Valid Data2"] = tv4.validate(data2, schema);

12、获取request.data值

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

  2.  
  3. data {object}:

  4. this is a dictionary of form data for the request. (request.data["key"]=="value")

  5. headers {object}:

  6. this is a dictionary of headers for the request (request.headers["key"]=="value")

  7. method {string}:

  8. GET/POST/PUT etc.

  9. url {string}:

  10. the url for the request.

  11. 假设requestBody中有"loginName: "10001";这个值,如果想获取到loginName的value值,代码如下

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

  14.  
  15. console.log(jd);

  16.  
  17. var loginName = jd.loginName;

  18.  
  19. console.log(loginName);

实操:

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

JSON.parse()【从一个字符串中解析出json对象】--把string转对象

JSON.stringify()【从一个对象中解析出字符串,主要针对[object object]类型数据的转换】--把对象转String

实操:

14、判断字段是否为空

 
  1. var jd = JSON.parse(responseBody);

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

15、在pre-request和tests 中获取变量的方法:

变量可以被使用在pre-request和test script中。因为这些部分是通过JavaScript来写的

你可以以不同的方式初始化和检索这些变量。可以在脚本中初始化变量,并将它们放在特定的范围内

1.定义一个变量在脚本中 

在脚本中设置一个变量可以根据变量预定的范围通过pm.environment.set("variable_key", "variable_value");方法或者pm.globals.set("variable_key", "variable_value");方法,这方法要求提供变量的key和value去设置变量。当你发送请求的时候,这脚本将会执行,值将会保存在变量中,如下图:

2.取一个预定义的变量

一旦一个变量被设置,你可以使用pm.environment.set("variable_key", "variable_value");;;或者pm.globals.set("variable_key", "variable_value");;; 根据适合的范围去获取变量值。这方法要求提供一个变量名作为参数去检索储存的值,如下图:

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值