一、环境变量
1、获取环境变量中的值pm.environment.get("variable_key");
2、获取全局变量中的值pm.globals.get("variable_key");
3、Tests中设置环境变量中值pm.environment.set("variable_key", "variable_value");
4、Tests中设置全局变量中值pm.globals.set("variable_key", "variable_value");5、获取页面 token 并在环境变量中添加const $ = cheerio.load(pm.response.text());var token = $("input[name='__RequestVerificationToken']").prop('value');pm.environment.set("RequestVerificationToken", token);
6、Tests中清除环境变量中设置的值pm.environment.unset("variable_key");
7、Tests中清除全局变量中值pm.globals.unset("variable_key");
8、获取一个变量pm.variables.get("variable_key");
二、返回的状态码
1、判断返回的状态码是200pm.test("Status code is 200", function () {pm.response.to.have.status(200)});将200改成500,则判断的是状态码为500当然还有其他的直接判断状态码的pm.response.to.be.info; //检查响应码是否为1xxpm.response.to.be.success; //检查响应码是否为2xx
pm.response.to.be.redirection; //检查响应码是否为3xx
pm.response.to.be.clientError; //检查响应码是否为4xx
pm.response.to.be.serverError; //检查响应码是否为5xx
pm.response.to.be.error; //检查响应码是否为4xx或者5xx
pm.response.to.be.ok; //检查响应码是否为200
pm.response.to.be.accepted; //检查响应码是否为202
pm.response.to.be.badRequest; //检查响应码是否为400
pm.response.to.be.unauthorized; //检查响应码是否为401
pm.response.to.be.forbidden; //检查响应码是否为403
pm.response.to.be.notFound; //检查响应码是否为404
pm.response.to.be.rateLimited; //检查响应码是否为4292、判断post请求返回的状态正确pm.test("Successful POST request", function () {pm.expect(pm.response.code).to.be.oneOf([201,202]);});
3、代码名称包含一个字符串pm.test("Status code name has string", function () { pm.response.to.have.status("Created");});
三、body 中内容判断
1、判断返回的body中包含某些内容pm.test("Body matches string", function () {pm.expect(pm.response.text()).to.include("string_you_want_to_search");});
2、判断响应主体是否等于一个字符串pm.test("Body is correct", function () {pm.response.to.have.body("response_body_string");});
3、将XML正文转换为Json对象var jsonObject = xml2Json(responseBody);
4、对于json数据使用TinyValidatorvar 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;});
四、json 文件
Postman中获取返回的json内容:var jsonData = pm.response.json();
var JsonData = {"employees": [{ "firstName":"Bill" , "lastName":"Gates" },{ "firstName":"George" , "lastName":"Bush" },{ "firstName":"Thomas" , "lastName":"Carter" }]}
1 、打印json内容console.log(JsonData);
打开View-->>Show Postman Console,查看打印内容2、 获取字段的值获取某个Json 字段的值:var firstName1 = JsonData.employees[0].firstName;判断某个Json 字段的值与预期一致:pm.test("firstName1 is OK", function () { pm.expect(firstName1).to.eql("Bill");});3、获取json子项/数组的个数/长度获取json子项/数组的个数/长度:var dataLength = JsonData.employees.length;判断json子项/数组的个数/长度与预期一致:pm.expect(JsonData.employees).to.have.lengthOf(3);
4、判断返回字段值的类型tests["firstName1 is str type"] = typeof(JsonData.employees[0].firstName) === "string";常见的类型有:number 、string 、object 、array 、boolean 、undefind
五、headers中内容判断
1、内容类型存在的判断大小写不敏感:pm.test("Content-Type is present", function () {pm.response.to.have.header("Content-Type");});大小写敏感:tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
六、判断响应时间
1、如下,判断响应时间不超过200mspm.test("Response time is less than 200ms", function () {pm.expect(pm.response.responseTime).to.be.below(200);});
七、接口请求
1、发送请求pm.sendRequest("https://postman-echo.com/get", function (err, response) { console.log(response.json());});
2、接口请求失败tests["Request Failed"] = false;tests["Request sucess"] = true; //接口请求成功