Skip to content

Instantly share code, notes, and snippets.

@atikahfd
Forked from JamesMessinger/reuse-code.js
Created June 16, 2022 08:06
Show Gist options
  • Select an option

  • Save atikahfd/b284543a71c4cb6f1a6bcc587aef9b99 to your computer and use it in GitHub Desktop.

Select an option

Save atikahfd/b284543a71c4cb6f1a6bcc587aef9b99 to your computer and use it in GitHub Desktop.
Saving JavaScript code to a variable in Postman
// Save common tests in a global variable
postman.setGlobalVariable("commonTests", () => {
// The Content-Type must be JSON
tests["Content-Type header is set"] = postman.getResponseHeader("Content-Type") === "application/json";
// The response time must be less than 500 milliseconds
tests["Response time is acceptable"] = responseTime < 500;
// The response body must include an "id" property
var data = JSON.parse(responseBody);
tests["Response has an ID"] = data.id !== undefined;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment