postman——集合——执行集合——测试脚本——pm对象简单示例01

console.log(pm.info.requestName);                           返回请求名
console.log(pm.info.requestId);                                 返回请求id
console.log("++++++++++++++++++++++++++");
console.log(pm.response.code);                             返回状态码
console.log(pm.response.responseTime);           返回响应时间
console.log(pm.response.headers);                   返回响应头信息,返回一个列表

 

===========================================================================================================================

//返回状态码
console.log(pm.response.code);
//
console.log(pm.response.reason());
//返回 Header头信息
console.log(pm.response.headers);
//返回响应时间
console.log(pm.response.responseTime);
//以文本格式返回body响应b内容
console.log(pm.response.text());
//以json格式返回body响应b内容
console.log(pm.response.json());

--------------------------------------------------------------------------------

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,是则真,否则假
-----------------------------------------------------------------------------------------------------------------

实践断言响应代码

以断言响应状态是否是200为例

查看结果,相应状态为200.

=============================================================

网址:https://www.jianshu.com/p/61047ceb048c

pm 对象与请求相关的功能有以下几个:

  • pm.info info 对象包含与正在执行的脚本有关的信息。例如请求名称,请求ID和迭代计数;
  • pm.variables/pm.environment/pm.globals,变量管理;
  • pm.request,pm 内的 request 对象表示当前脚本所在的请求;
  • pm.response,response 对象表示当前脚本所在请求的响应结果;
  • pm.cookies,cookies 对象包含与请求域相关联的 cookie 列表;

pm.info

pm.info.eventName:String

获取当前环境名称。

console.log(pm.info.eventName);
/// 测试环境

pm.info.iteration:Number

返回当前是第几次运行迭代,必须要在 Collection 运行才会生效;

console.log(pm.info.iteration);
/// 0

pm.info.iterationCount:Number

计划运行的迭代总数。

console.log(pm.info.iterationCount);
/// 4

pm.info.requestName:String

返回当前请求名称,前提是请求已被保存到某个 Collection。

console.log(pm.info.requestName);
/// test login

pm.info.requestId:String

获取标识正在运行的请求的唯一 GUID。

console.log(pm.info.requestName);
/// c78dee07-399a-4885-bfcc-ca524e6114f6

pm.request

request 对象表示脚本所在的当前请求。对于 Pre-request Scripts,相当于即将要发送的请求,在 Tests 中时,相当于是已发送的请求。

请求对象包含以下面结构存储的信息:

pm.request.url:请求的 url

console.log(pm.request.url);
/// {host: [4], path: [3], protocol: "http"…}

pm.request.headers:请求 headers 列表,与 responseHeaders 不同的是返回的是数组且结构也有区别

console.log(pm.request.headers);
/// [{…}, {…}, {…}, {…}, {…}, …]

pm.request.headers.add(headerName:String):为当前请求添加指定的头部字段,当然没有添值。

pm.request.headers.add('xxx');
console.log(pm.request.headers);
/// [...{key: "xxx" value: ""}]

pm.request.headers.delete(headerName:String):删除指定的头部字段。

pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):以对象类型传入键值对的形式添加头部字段。但是要注意的是头部字段如果存在,不会再次添加只会更新其值。

pm.request.headers.upsert({'content-type': 'application/json'});

pm.response

The response details are stored in the following format:

pm.response.code:Number:返回状态码

console.log(pm.response.code);
/// 200

pm.response.reason():Function → String:状态码描述

console.log(pm.response.reason());
/// OK

pm.response.headers:HeaderList:响应头列表

console.log(pm.response.reason());
/// [{…}, {…}, {…}, {…}, {…}, …]

pm.response.responseTime:Number:响应时间

console.log(pm.response.responseTime);
/// 54

pm.response.text():Function → String:以文本形式返回响应消息体 body 内容

console.log(pm.response.text());

pm.response.json():Function → Object:以对象形式返回响应消息体 body 内容,相当于对文本形式的内容做了 JSON.parse,但是要注意只针对 json 格式的响应结果。

console.log(pm.response.json());

pm.cookies

pm.cookies: 读取与当前请求域相关的所有 cookie 的列表

console.log(pm.cookies);
/// [{…}, {…}]

pm.cookies.has(cookieName:String):Function → Boolean:判断指定的 cookie 是否存在

onsole.log(pm.cookies.has('cookie_token'));
/// true

pm.cookies.get(cookieName:String):Function → String:获取指定的 cookie 的值

onsole.log(pm.cookies.get('cookie_token'));
/// 4c2d02e1...a21ae1f635d88623161

pm.cookies.toObject():Function → Object:以对象的形式获取所有 cookie 及其值(不包括 host, domain 等信息)。返回的 cookie 是与请求的域 domain 和 路径 path 一致的 cookie。

console.log(pm.cookies.toObject());
{{cookie_token: "aff62d5e...fcea38", phpsessid: "a9sun4...fek7"}


作者:猫与测试
链接:https://www.jianshu.com/p/61047ceb048c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12198064.html