Postman Sandbox

Postman Sandbox是一个 JavaScript 执行环境,在编写预请求脚本和测试脚本(在 Postman 和 Newman 中)时可用。在预请求/测试脚本部分中编写的代码都在 Sandbox 中执行。

常用的库和实用程序

  • Lodash: JS 实用程序库
  • cheerio: 快速,精简的核心 jQuery API 实现(版本4.6.0及更高版本)
  • BackboneJS 已弃用: 提供简单的模型,视图和集合。这将在以后版本的沙箱中删除。
  • SugarJS 已弃用: 使用有用的方法扩展本机JS对象。这将在以后版本的沙箱中删除。
  • tv4 JSON schema validator: 根据json-schema草案的v4验证JSON对象
  • CryptoJS: 标准和安全的加密算法。支持的算法:AES,DES,EvpKDF,HMAC-MD5,HMAC-SHA1 / 3/256/512,MD5,PBKDF2,Rabbit,SHA1 / 3/224/256/512,TripleDES
  • xml2Json(xmlString): 这个函数在 Newman 和 Postman 中是一样的
  • xmlToJson(xmlString) 已弃用: 此功能在Newman和Postman中的行为不相同
  • postman.getResponseHeader(headerName) Test-only:返回名称为“headerName”的响应头(如果存在)。如果没有这样的头存在,则返回null。 注意:根据W3C规范,头名不区分大小写。这个方法会照顾这个。postman.getResponseHeader("Content-type")postman.getResponseHeader("content-Type")将返回相同的值。

注意:自4.6.0版起,jQuery支持已经停用,支持cheerio

环境和全局变量

  • postman.setEnvironmentVariable(variableName, variableValue): Sets an environment variable “variableName”, and assigns the string “variableValue” to it. You must have an environment selected for this method to work. Note: Only strings can be stored. Storing other types of data will result in unexpected behavior.
  • postman.getEnvironmentVariable(variableName): Returns the value of an environment variable “variableName”, for use in pre-request & test scripts. You must have an environment selected for this method to work.
  • postman.setGlobalVariable(variableName, variableValue): Sets a global variable “variableName”, and assigns the string “variableValue” to it. Note: Only strings can be stored. Storing other types of data will result in unexpected behavior.
  • postman.getGlobalVariable(variableName): Returns the value of a global variable “variableName”, for use in pre-request & test scripts.
  • postman.clearEnvironmentVariable(variableName): Clears the environment variable named “variableName”. You must have an environment selected for this method to work.
  • postman.clearGlobalVariable(variableName): Clears the global variable named “variableName”.
  • postman.clearEnvironmentVariables(): Clears all environment variables. You must have an environment selected for this method to work.
  • postman.clearGlobalVariables(): Clears all global variables.
  • environment: A dictionary of variables in the current environment. Use environment["foo"] to access the value of the “foo” environment variable. Note: This can only be used to read the variable. Use setEnvironmentVariable() to set a value.
  • globals: A dictionary of global variables. Use globals["bar"] to access the value of the “bar” global variable. Note: This can only be used to read the variable. Use setGlobalVariable() to set a value

Dynamic variables

Postman also has a few dynamic variables which you can use in your requests. This is primarily an experiment right now. More functions would be added soon. Note that dynamic variables cannot be used in the Sandbox. You can only use them in the {{..}} format in the request URL / headers / body.

  • : Adds a v4 style guid
  • : Adds the current timestamp.
  • : Adds a random integer between 0 and 1000

Cookies

  • responseCookies {array} Postman-only: Gets all cookies set for the domain. You will need to enable the Interceptor for this to work.
  • postman.getResponseCookie(cookieName) Postman-only: Gets the response cookie with the given name. You will need to enable the interceptor for this to work. Check out the blog post.
  • request {object}: Postman makes the request object available to you while writing scripts. This object is read-only. Changing properties of this object will have no effect. Note: Variables will NOT be resolved in the request object. The request object is composed of the following:

    • data {object} - this is a dictionary of form data for the request. (request.data[“key”]==”value”)
    • headers {object} - this is a dictionary of headers for the request (request.headers[“key”]==”value”)
    • method {string} - GET/POST/PUT etc.
    • url {string} - the url for the request.
  • responseHeaders {object} Deprecated, Test-only: This is a map of the response headers. This is case-sensitive, and should not be used. Check the postman.getResponseHeader() method listed above.

  • responseBody {string} Test-only: A string containing the raw response body text. You can use this as an input to JSON.parse, or xml2Json.
  • responseTime {number} Test-only: The response time in milliseconds
  • responseCode {object} Test-only: Contains three properties:

    • code {number}: The response code (200 for OK, 404 for Not Found etc)
    • name {string}: The status code text
    • detail {string}: An explanation of the response code
  • tests {object} Test-only: This object is for you to populate. Postman will treat each property of this object as a boolean test.

  • iteration {number}: Only available in the Collection Runner and Newman. Represents the current test run index. Starts from 0.

Test-only: This object is only available in the test script section. Using this in a pre-request script will throw an error.

Data files

If you’re using data files in the Collection Runner or in Newman, you’ll have access to a data object, which is a dictionary of data values in the current test run.

pm.* APIs

Review Postman Sandbox API Reference.

results matching ""

    No results matching ""