Apifox 兼容了 Postman 的脚本 API,你可以在 前置脚本 或 后置脚本 中使用这些 API 来获取请求和响应信息、读写变量、发送请求、做断言等。脚本的核心对象,包含了接口(或测试集)运行的相关信息,可以通过它访问需要发送的请求信息和发送后返回的响应信息,还可以通过它获取(get)或设置(set)环境变量和全局变量。pm.info#
用于获取运行时上下文信息(迭代次数、请求名称/ID 、脚本类型等)。当前执行是什么类型的脚本:前置脚本(prerequest),或后置脚本(test)。
当前执行第几轮循环(iteration),仅集合测试有效。
pm.info.iterationCount:Number
pm.info.requestName:String
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 → *
pm.variables.set(variableName:String, variableValue:String):function → void
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 → *
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 → *
pm.environment.set(variableName:String, variableValue:String):function
pm.environment.replaceIn(variableName:String):function
以真实的值替换字符串里的包含的动态变量,如{{variable_name}}
。
pm.environment.toObject():function → Object
pm.environment.unset(variableName:String):function
pm.environment.clear():function
pm.moduleVariables#
pm.moduleVariables:Object
pm.moduleVariables.has(variableName:String):function → Boolean
pm.moduleVariables.get(variableName:String):function → *
pm.moduleVariables.set(variableName:String, variableValue:String):function
pm.moduleVariables.replaceIn(variableName:String):function
将字符串中包含的 {{variable}} 占位符替换为真实值。
pm.moduleVariables.toObject():function → Object
pm.moduleVariables.unset(variableName:String):function
pm.moduleVariables.clear():function
兼容写法:pm.collectionVariables
与 pm.moduleVariables
效果相同,可以互换使用。
pm.globals#
pm.globals.has(variableName:String):function → Boolean
pm.globals.get(variableName:String,variableScope:String):function → *
获取单个全局变量。 可以使用 'PROJECT' (默认)或 'TEAM' 来选择项目全局变量或团队全局变量。
pm.globals.set(variableName:String,variableValue:String, variableScope:String):function
设置单个全局变量。可以使用 'PROJECT' (默认)或 'TEAM' 来选择项目全局变量或团队全局变量。
pm.globals.replaceIn(variableName:String):function
以真实的值替换字符串里的包含的动态变量,如{{variable_name}}
。
pm.globals.toObject():function → Object
pm.globals.unset(variableName:String):function
pm.globals.unset(variableName:String,variableScope:String):function
删除单个全局变量。可以使用 'PROJECT' (默认)或 'TEAM' 来选择项目全局变量或团队全局变量。
pm.globals.clear():function
2.
当使用 set 并带上 'TEAM' 变量范围时,只会更改已有同名团队变量的本地值
。如果当前不存在此名称的团队变量,则不会新增一个团队变量,而是把本次 set 的变量当做临时变量来使用。
pm.request#
用于访问当前请求对象。在前置脚本中表示将要发送的请求
,在后置脚本中表示已经发送了的请求
。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
pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):function
新增或更新 key 为headerName
的 header(如不存在则新增,如已存在则修改)。
pm.response#
表示当前请求的响应。仅在后置脚本可用,包含状态码、headers、响应体等。pm.response.status:String
pm.response.responseTime:Number
pm.response.responseSize:Number
pm.response.text():Function → String
pm.response.json():Function → Object
pm.cookies#
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
用于编写断言,验证响应或变量是否符合预期,支持同步和异步测试。pm.test(testName:String, specFunction:Function):Function
pm.test.index():Function
→ Number :获取测试索引。
通过 callback 的可选参数 done
,还可用来测试异步方法: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.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