Postman编程

Postman常用Api

Postman像jmeter一样提供前置处理脚本和后置处理脚本。脚本主要使用JavaScript语法,并内置提供了一些js代码库,提供了一些内置对象和方法。
参考:https://www.getpostman.com/docs/testing_examples

可通过这个链接作为例子进行学习:http://www.getpostman.com/samples/test_data_file.json

  1. 设置环境变量
postman.setEnvironmentVariable("key", "value");
  1. 查询环境变量
postman.getEnvironmentVariable("key");
  1. 设置全局变量
postman.setGlobalVariable("key", "value");
  1. 查询全局变量
postman.getGlobalVariable("key"); 
  1. 检查Http响应内容是否包含一个字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
  1. 将XML内容转成JSON对象,方便对XML内容进行处理
var jsonObject = xml2Json(responseBody);
  1. 检查Http响应内容是否等于一个字符串
tests["Body is correct"] = responseBody === "response_body_string";
  1. 检查一个JSON值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
  1. 检查一个Http response header是否存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
//这种方式不区分大小写
  1. 检查一个Http response header是否存在(区分大小写)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
  1. 对响应时间做断言,如响应时间是否小于200ms
tests["Response time is less than 200ms"] = responseTime < 200;
  1. 对status code进行断言
tests["Status code is 200"] = responseCode.code === 200;
  1. 断言Status messsage是否包含某个字符串
tests["Status code name has string"] = responseCode.name.has("Created");
  1. 断言Status code是否成功
tests["Successful POST request"] = responseCode.code === 200;
  1. 使用TinyValidator框架对
var schema = {
 "items": {
 "type": "boolean"
 }
};
var data1 = [true, false];
var data2 = [true, 123];

tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
console.log("Validation failed: ", tv4.error);

--
来源我的文章:http://ask.testfan.cn/article/910

转载于:https://www.cnblogs.com/yanluu/p/6337476.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值