Postman常用Api
Postman像jmeter一样提供前置处理脚本和后置处理脚本。脚本主要使用JavaScript语法,并内置提供了一些js代码库,提供了一些内置对象和方法。
参考:https://www.getpostman.com/docs/testing_examples
可通过这个链接作为例子进行学习:http://www.getpostman.com/samples/test_data_file.json
- 设置环境变量
postman.setEnvironmentVariable("key", "value");
- 查询环境变量
postman.getEnvironmentVariable("key");
- 设置全局变量
postman.setGlobalVariable("key", "value");
- 查询全局变量
postman.getGlobalVariable("key");
- 检查Http响应内容是否包含一个字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
- 将XML内容转成JSON对象,方便对XML内容进行处理
var jsonObject = xml2Json(responseBody);
- 检查Http响应内容是否等于一个字符串
tests["Body is correct"] = responseBody === "response_body_string";
- 检查一个JSON值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
- 检查一个Http response header是否存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
//这种方式不区分大小写
- 检查一个Http response header是否存在(区分大小写)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
- 对响应时间做断言,如响应时间是否小于200ms
tests["Response time is less than 200ms"] = responseTime < 200;
- 对status code进行断言
tests["Status code is 200"] = responseCode.code === 200;
- 断言Status messsage是否包含某个字符串
tests["Status code name has string"] = responseCode.name.has("Created");
- 断言Status code是否成功
tests["Successful POST request"] = responseCode.code === 200;
- 使用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