Apifox 兼容了 Postman 的脚本,所以你可以使用 Postman 的脚本 API 来编写脚本。pm
对象包含了接口(或测试集)运行的相关信息,并且可以通过它访问需要发送的请求信息和发送后返回的响应信息。另外还可以通过它获取 (get) 或设置 (set) 环境变量和全局变量。pm.info
对象包含了接口(或测试集)运行的相关信息。当前执行是什么类型的脚本:前置脚本(prerequest),或后置脚本(test)。
当前执行第几轮循环(iteration),仅集合测试有效。
pm.info.iterationCount:Number
pm.info.requestName:String
pm.sendRequest#
pm.sendRequest
用途为在脚本内异步发送 HTTP/HTTPS 请求。该方法接受一个 collection SDK 兼容的 request 参数和一个 callback 函数参数。 callback 有 2 个参数,第一个是 error ,第二个是 collection SDK 兼容的 response。更多信息请查阅 Collection SDK 文档 。 pm.variables#
临时变量。不同类型的变量,有不同的优先级,不同类型变量的优先级顺序为: 临时变量
> 环境变量
> 项目全 局变量
> 团队全局变量
。pm.variables.has(variableName:String):function → Boolean
: 检查是否存在某个临时变量。
pm.variables.get(variableName:String):function → *
: get 单个临时变量。
pm.variables.set(variableName:String, variableValue:String):function → void
: set 单个临时变量。
pm.variables.replaceIn(variableName:String):function
: 以真实的值替换字符串里包含的“动态变量”,如{{variable_name}}
。示例:pm.variables.replaceInAsync(variableName:String):function
: 以真实的值替换字符串里包含的“动态值表达式”,如{{$person.fullName}}
。返回 Promise,调用时需加 await
。示例:pm.variables.toObject():function → Object
: 以对象形式获取所有临时变量。
pm.iterationData#
测试数据变量,因为测试数据是单独管理的,暂不支持在脚本中直接设置测试数据变量,但是你可以在脚本中访问测试数据变量,如下。pm.iterationData.has(variableName:String):function → Boolean
: 检查是否存在某个测试数据变量。
pm.iterationData.get(variableName:String):function → *
: get 单个测试数据变量。
pm.iterationData.replaceIn(variableName:String):function
: 以真实的值替换字符串里包含的“动态变量”,如{{variable_name}}
。
pm.iterationData.toObject():function → Object
: 以对象形式获取所有测试数据变量。
pm.environment#
pm.environment.name:String
: 环境名。
pm.environment.has(variableName:String):function → Boolean
:检查是否存在某个环境变量。
pm.environment.get(variableName:String):function → *
:get 单个环境变量。
pm.environment.set(variableName:String, variableValue:String):function
:set 单个环境变量。
pm.environment.replaceIn(variableName:String):function
:以真实的值替换字符串里的包含的动态变量,如{{variable_name}}
。
pm.environment.toObject():function → Object
:以对象形式获取 当前环境的所有变量。
pm.environment.unset(variableName:String):function
: unset 单个环境变量。
pm.environment.clear():function
:清空当前环境的所有变量。
pm.globals#
pm.globals.has(variableName:String):function → Boolean
:检查是否存在某个全局变量。
pm.globals.get(variableName:String,variableScope:String):function → *
:get 单个全局变量。 可以使用 'PROJECT' (默认)或 'TEAM' 来选择全局变量范围。
pm.globals.set(variableName:String,variableValue:String, variableScope:String):function
:set 单个全局变量。可以使用 'PROJECT' (默认)或 'TEAM' 来选择全局变量范围。
pm.globals.replaceIn(variableName:String):function
:以真实的值替换字符串里的包含的动态 变量,如{{variable_name}}
。如前置脚本,获取请求参数的值如果包含变量,则需要使用 pm.globals.replaceIn
才能将变量替换会真正的值。
pm.globals.toObject():function → Object
:以对象形式获取所有全局变量。
pm.globals.unset(variableName:String):function
: unset 单个全局变量。
pm.globals.unset(variableName:String,variableScope:String):function
: unset 单个全局变量。可以使用 'PROJECT' (默认)或 'TEAM' 来选择全局变量范围。
pm.globals.clear():function
:清空当前环境的全局变量。
2.
当使用 set 并带上 'TEAM' 变量范围时,只会更改已有同名团队变量的本地值
。如果当前不存在此名称的团队变量,则不会新增一个团队变量,而是把本次 set 的变量当做临时变量来使用。
pm.request#
request
是接口请求对象。在前置脚本中表示将要发送的请求
,在后置脚本中表示已经发送了的请求
。pm.request.url
:Url
: 当前请求的 URL。pm.request.getBaseUrl()
:获取当前运行环境选择的的 前置 URL
,在 2.1.39 版本之后支持。
pm.request.method
:String
当前请求的方法,如GET
、POST
等。
pm.request.headers.add({ key: headerName:String, value: headerValue:String})
:function
: 给当前请求添加一个 key 为headerName
的 header。
pm.request.headers.remove(headerName:String)
:function
: 删除当前请求里 key 为headerName
的 header
pm.request.headers.get(headerName:String)
:function
: 查询当前请求里的 headerName
。
pm.request.headers.upsert({ key: headerName:String, value: headerValue:String})
:function
: upsert key 为headerName
的 header(如不存在则新增,如已存在则修改)。
pm.request.auth
: 当前请求的身份验证信息
在后置操作中填入自定义脚本,参考下图示例,选择所需要的提取对象,编写对应的函数。例如提取请求中的 headers 中的 Accept
值并打印到控制台。pm.response#
在后置脚本中 pm.response
接口请求完成后返回的 response 信息。pm.response.status:String
pm.response.responseTime:Number
pm.response.responseSize:Number
pm.response.text():Function → String
pm.response.json():Function → Object
在后置脚本中使用 pm.response.headers.get
命令可以提取返回响应中的 headers
中的值。例如想要在控制台中显示 Header 中的 Date 值,那么可以在后置操作中填写如下自定义脚本:pm.cookies#
cookies
为当前请求对应域名下的 cookie 列表。pm.cookies.has(cookieName:String):Function → Boolean
检查是否存在名为cookieName
的 cookie 值
pm.cookies.get(cookieName:String):Function → String
get 名为cookieName
的 cookie 值
pm.cookies.toObject:Function → Object
pm.cookies.jar().clear(pm.request.getBaseUrl())
pm.cookies 为接口请求后返回的 cookie,而不是接口请求发出去的 cookie。
pm.test#
pm.test(testName:String, specFunction:Function):Function
通过 callback 的可选参数 done
,还可用来测试异步方法:pm.test.index():Function → Number
Get the total number tests from a specific location.
pm.expect#
pm.expect(assertion:*):Function → Assertion
Response 对象可用的断言 API 列表#
pm.response.to.have.status(code:Number)
pm.response.to.have.status(reason:String)
pm.response.to.have.header(key:String)
pm.response.to.have.header(key:String, optionalValue:String)
pm.response.to.have.body()
pm.response.to.have.body(optionalValue:String)
pm.response.to.have.body(optionalValue:RegExp)
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.have.jsonSchema(schema:Object)
pm.response.to.have.jsonSchema(schema:Object, ajvOptions:Object)
pm.response.to.be#
pm.response.to.be
是用来快速断言的一系列内置规则。pm.response.to.be.success
pm.response.to.be.redirection
pm.response.to.be.clientError
pm.response.to.be.serverError
pm.response.to.be.accepted
pm.response.to.be.badRequest
pm.response.to.be.unauthorized
pm.response.to.be.forbidden
pm.response.to.be.notFound
pm.response.to.be.rateLimited