一、环境变量
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、判断返回的状态码是200 pm.test("Status code is 200", function () {pm.response.to.have.status(200)}); 将200改成500,则判断的是状态码为500 当然还有其他的直接判断状态码的     pm.response.to.be.info; //检查响应码是否为1xx     pm.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;  //检查响应码是否为429
  2、判断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数据使用TinyValidator 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; }); 
  四、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"); 

  

六、判断响应时间

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
1、如下,判断响应时间不超过200ms pm.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; //接口请求成功 

八、官方Tests文档
https://learning.getpostman.com/docs/postman/scripts/test_examples/
 
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄