postman Test Script
2017-10-23 本文已影响32人
足迹人生2017
#### pm.response
//获取当前请求返回的状态码如200,404,500等
pm.response.code
//当前请求成功返回OK
pm.response.reason()
//以数组的形式返回当前请求成功后的response的headers
pm.response.headers:
//获取执行此次请求的时间单位为ms
pm.response.responseTime
//以文本的方式获取响应里面的body的内容
pm.response.text()
//将body里面的内容解析为一个json对象
pm.response.json():Function → Object
#### pm.cookies
//检查请求的url站点是否存在cookie
pm.cookies.has(cookieName:String):Function → Boolean
//获取存在的cookie的值
pm.cookies.get(cookieName:String):Function → String
//将请求的url站点中获取到的所有存在的cookie输出到一个对象中去
pm.cookies.toObject:Function → Object
//测试方法,第二个方法中填写测试内容
pm.test(testName:String, specFunction:Function)
#example
pm.test("response should be okay to process", function () {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody('');
pm.response.to.not.have.jsonBody('error');
});
//该方法需要结合http://chaijs.com/api/bdd/使用
pm.expect(assertion:*)
#example
pm.test('environment to be production', function () {
pm.expect(pm.environment.get('env')).to.equal('production');
});
#### pm.response.to
//根据状态码判断响应是否成功
pm.response.to.have.status(code:Number)
//根据返回的状态值判断响应是否成功
pm.response.to.have.status(reason:String)
//根据response中的headers的key判断响应是否成功
pm.response.to.have.header(key:String)
//对response中的header中的key和value校验,判断响应是否成功
pm.response.to.have.header(key:String, optionalValue:String)
//获取响应返回的资源
pm.response.to.have.body()
//对响应返回的body内容校验,判断响应是否成功
pm.response.to.have.body(optionalValue:String)
//对响应返回的body进行正则校验,判断响应是否成功
pm.response.to.have.body(optionalValue:RegExp)
//判断响应返回的body是否是json,判断响应是否成功
pm.response.to.have.jsonBody()
pm.response.to.have.jsonBody(optionalExpectEqual:Object)
pm.response.to.have.jsonBody(optionalExpectPath:String)
pm.response.to.have.jsonBody(optionalExpectPath:String, optionalValue:*)
#### pm.response.to.be.*
判断当前请求响应的状态值
//Checks 1XX status code
pm.response.to.be.info
//Checks 2XX status code
pm.response.to.be.success
//Checks 3XX status code
pm.response.to.be.redirection
//Checks 4XX status code
pm.response.to.be.clientError
//Checks 5XX
pm.response.to.be.serverError
//Checks 4XX or 5XX
pm.response.to.be.error
//Status code must be 200
pm.response.to.be.ok
//Status code must be 202
pm.response.to.be.accepted
//Status code must be 400
pm.response.to.be.badRequest
//Status code must be 401
pm.response.to.be.unauthorised
//Status code 403
pm.response.to.be.forbidden
//Status code of response is checked to be 404
pm.response.to.be.notFound
//Checks whether response status code is 429
pm.response.to.be.rateLimited