Apidog 的脚本引擎使用 pm 对象来访问有关请求、响应和变量的数据。此方法与 Postman 兼容。pm 对象#
| 属性 | 数据类型 | 描述 |
|---|
pm.info.eventName | String | 当前正在运行的脚本类型 (预处理器脚本或后处理器脚本)。 |
pm.info.iteration | Number | 当前迭代的次数(仅在测试集合中有效)。 |
pm.info.iterationCount | Number | 总迭代次数(仅在测试集合中有效)。 |
pm.info.requestName | String | 当前正在运行的 API 名称。 |
pm.info.requestId | String | 当前正在运行的 API ID。 |
发送请求(pm.sendRequest)#
pm.sendRequest 用于在脚本中发送异步 HTTP/HTTPS 请求。pm.variables#
Local Variables > Environment Variables > Global Variables Shared within Project > Global Variables Shared within Team。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}})替换为实际值。示例:
// Define a string containing a dynamic variable
let stringWithVariable = "Hello, {{username}}";
// Use the replaceIn method to replace the {{username}} placeholder
let realValueString = pm.variables.replaceIn(stringWithVariable);
// Output the replaced value
console.log(realValueString); // Output: "Hello, john_doe"
pm.variables.replaceInAsync(variableName:String):function:将字符串中的“动态值表达式”(例如 {{$person.fullName}})替换为实际值。此方法返回一个 Promise,因此调用时需要使用 await。示例:
// Define a string containing a dynamic value expression
let stringWithVariable = "Hello, {{$person.fullName}}";
// Use the replaceInAsync method to replace the {{$person.fullName}}
let realValueString = await pm.variables.replaceInAsync(stringWithVariable);
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用于管理模块级变量的 Current Value。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.replaceIn 将变量替换为真实值。
pm.globals.toObject():function → Object:以对象形式获取所有全局变量。
pm.globals.unset(variableName:String,variableScope:String):function:取消设置全局变量。使用 'PROJECT'(默认)或 'TEAM' 指定变量的作用域。
pm.globals.clear():function:清除当前环境下的所有全局变量。
1.
以上所有操作仅影响current values,不影响initial values。
2.
使用 'TEAM' 作用域进行 set 时,只会更新现有团队变量的当前值。如果团队变量不存在,则不会创建它。相反,该变量会被视为局部变量。
pm.request#
request 是 API 请求对象。在预处理器脚本中,它是将要发送的请求。在后处理器脚本中,它是已经发送的请求。pm.request.url:Url:当前请求的 URL。pm.request.getBaseUrl():获取当前运行时环境所选的 BASE URL。此功能在 2.1.39 版本之后支持。
pm.request.method:String:当前请求的方法,例如 GET、POST 等。
pm.request.headers.add({ key: headerName:String, value: headerValue:String}):function:在当前请求中添加一个以 headerName 为键的头部。
pm.request.headers.remove(headerName:String):function:在当前请求中删除一个以 headerName 为键的头部。
pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):function:在当前请求中更新或插入一个以 headerName 为键的头部。如果该键已存在,则会被修改。
以下 API 只能在postprocessor scripts中使用。
pm.response#
在后处理器脚本中使用 pm.response 访问返回的响应信息。pm.response.status:String
pm.response.responseTime:Number
pm.response.responseSize:Number
pm.response.text():Function → String
pm.response.json():Function → Object
pm.cookies#
Cookies 是当前请求域名下的 cookie 列表。pm.cookies.has(cookieName:String):Function → Boolean
检查 cookieName 的 cookie 值是否存在。
pm.cookies.get(cookieName:String):Function → String
从 cookieName 获取 cookie 值。
pm.cookies.toObject:Function → Object
以对象形式获取当前域名下的所有 cookie。
pm.cookies.jar().clear(pm.request.url)
清除所有 cookie。
pm.cookies 是 API 请求后返回的 cookie,而不是 API 请求发送的 cookie。
pm.test#
你可以在回调函数中使用 done(可选参数)来运行异步测试。pm.test.index():Function → Number
获取特定位置的测试总数。
pm.expect#
pm.expect 是一种断言方法。在此查看 ChaiJS expect BDD 库文档。pm.response.to.have.*#
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.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。