Apidog 的腳本引擎使用 pm 物件來存取請求、回應和變數的相關資料。此方法與 Postman 相容。pm Object#
| Property | Data Type | Description |
|---|
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: 在目前請求中新增一個 key 為 headerName 的標頭。
pm.request.headers.remove(headerName:String):function: 在目前請求中刪除一個 key 為 headerName 的標頭。
pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):function: 在目前請求中更新插入一個 key 為 headerName 的標頭。如果該 key 已存在,則會進行修改。
以下 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 expects 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。