环境
设置一个环境变量
pm.environment.set("variable_key", "variable_value");
设置一个嵌套对象作为环境变量
var array = [1, 2, 3, 4];
pm.environment.set("array", JSON.stringify(array, null, 2));
var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
pm.environment.set("obj", JSON.stringify(obj));
获得一个环境变量
var value = pm.environment.get("variable_key");
如果是json
var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));
清除一个环境变量
pm.environment.unset("variable_key");
全局
设置一个全局变量
pm.globals.set("variable_key", "variable_value");
清除一个全局变量
pm.globals.unset("variable_key");
变量
var value = pm.variables.get("variable_key");
响应处理
检查如果响应包含一个字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
检查如果响应等于一个字符串
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
检查一个json值
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
内容类型header是否出现
pm.test("Content-Type header is present", function () {
pm.response.to.have.header("Content-Type");
});
响应时间小于200毫秒
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
状态码是200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
状态码名称包含一个字符串
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
post请求状态码成功
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
验证请求结构
使用tv4验证json schema
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
pm.test('Schema is valid', function() {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});
使用ajv验证json schema
var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
"properties": {
"alpha": {
"type": "boolean"
}
}
};
pm.test('Schema is valid', function() {
pm.expect(ajv.validate(schema, {alpha: true})).to.be.true;
pm.expect(ajv.validate(schema, {alpha: 123})).to.be.false;
});
编解码
解码base64数据
// Assume `base64Content` has a base64 encoded value
var rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
// CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
var intermediate = CryptoJS.enc.Base64.parse(base64content);
pm.test('Contents are valid', function() {
pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
});
转换xml为json
var jsonObject = xml2Json(responseBody);
发送异步请求
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
断言库
判断子字符串在目标中
pm.test("Check if pattern is in target string",function () {
pm.expect('foobar').to.have.string('bar');
});
严格比较
const TEN = 10;
pm.test('Check if number is equal to 10', function () {
pm.expect(TEN).to.equal(10);
});
宽松比较
pm.test("Our JSON is loosely equal to the provided JSON", function () {
pm.expect(data1).to.deep.equal(data2);
});
断言响应值
pm.test("Check response value", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
断言当前环境
pm.test("Check if environment is production", function () {
pm.expect(pm.environment.get('env')).to.equal('production');
});
断言目标类型等于字符串类型
pm.test("Check if target is string", function () {
pm.expect('Postman').to.be.a('string');
});
pm.test("Check if target is an object", function () {
pm.expect({a: 1}).to.be.an('object');
});
pm.test("Check if target is undefined", function () {
pm.expect(undefined).to.be.an('undefined');
});
断言如果目标是空
pm.test("Check if array is empty", function () {
expect([]).to.be.empty;
});
pm.test("Check if string is empty", function () {
pm.expect('').to.be.empty;
});
pm.test("Check if array is empty", function () {
pm.expect([]).to.be.an('array').that.is.empty;
});
断言目标包含已传递的key
pm.test("Check if object contains all provided keys", function () {
pm.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
});
pm.test("Checking if object contains any ONE of the keys", function () {
pm.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
});
pm.test("Check if object contains any NONE of the provided keys", function () {
pm.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
});
断言目标包含所述属性
pm.test("Check if object contains the property", function () {
pm.expect({a: 1}).to.have.property('a');
});
确认目标长度
pm.test("Check the length of the target", function () {
pm.expect('foo').to.have.lengthOf(3);
});
pm.test("Check the size of the target", function () {
pm.expect([1, 2, 3]).to.have.lengthOf(2);
});
断言目标数组具有与给定数组集相同的成员
pm.test("Check if the target has same members as the array set", function () {
pm.expect([1, 2, 3]).to.have.members([2, 1, 3]);
});
断言目标包含所提供的项目
pm.test("Check if the target array includes the number provided", function () {
pm.expect([1, 2, 3]).to.include(2);
});
pm.test("Check if the target object includes the properties provided", function () {
pm.expect({a: 1, b: 2, c: 3}).to.include({a: 1, b: 2});
});
pm.test("Check if the target is an array that includes the number specified", function () {
pm.expect([1, 2, 3]).to.be.an('array').that.includes(2);
});