Skip to content

Commit

Permalink
Release a new version with improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vdespa committed Sep 26, 2019
1 parent ddddd9d commit 0b26bf1
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
33 changes: 30 additions & 3 deletions docs/cheatsheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ Local variables are automatically removed once the tests have been executed.
Dynamic variables
-----------------

Can only be used in request builder. Only ONE value is generated per request.

All dynamic variables can be combined with strings, in order to generate dynamic / unique data.

Example JSON body:
Expand All @@ -192,8 +190,11 @@ Example JSON body:
{"name": "John Doe", "email": "john.doe.{{$timestamp}}@example.com"}
If you want to use dynamic variables in scripts, you can use the `replaceIn` starting with Postman v7.6.0. ::

pm.variables.replaceIn('{{$randomFirstName}}'); // returns a String

Please see the section dedicated to :doc:`Dynamic variables </dynamic-variables>`
For more details please see the section dedicated to :doc:`Dynamic variables </dynamic-variables>`

Logging / Debugging variables
-----------------------------
Expand Down Expand Up @@ -297,6 +298,32 @@ Convert XML body to JSON: ::

Note: see assertions for JSON responses.

Skipping tests
--------------

You can use `pm.test.skip` to skip a test. Skipped tests will be displayed in reports.

**Simple example** ::

pm.test.skip("Status code is 200", () => {
pm.response.to.have.status(200);
});

**Conditional skip** ::

const shouldBeSkipped = true; // some condition

(shouldBeSkipped ? pm.test.skip : pm.test)("Status code is 200", () => {
pm.response.to.have.status(200);
});

Failing tests
-------------

You can fail a test from the scripts without writing an assertion: ::

pm.expect.fail('This failed because ...');

Postman Sandbox
===============

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# The short X.Y version
version = u''
# The full version, including alpha/beta/rc tags
release = u'Version 1.2.0 - June 2019'
release = u'Version 1.3.0 - September 2019'


# -- General configuration ---------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion docs/dynamic-variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
Dynamic variables
*****************

Dynamic variables can only be used in the request builder. They do not work in scripts (pre-request/tests)!
Dynamic variables can be used in the request builder like this:

.. image:: _static/dynamic-variables.png
:scale: 50 %

If you want to use dynamic variables in scripts, you can use the `replaceIn` starting with Postman v7.6.0. ::

pm.variables.replaceIn('{{$randomFirstName}}');

pm.variables.replaceIn('{{$randomFirstName}} {{$randomLastName}}');

The `replaceIn` method will return a String with the resolved variables.

Before Postman 7.2, only the following dynamic variables were available:

+---------------+-----------------------------------------------------+--------------------------------------+
Expand Down
92 changes: 92 additions & 0 deletions docs/libraries.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
********************
JavaScript libraries
********************

Postman comes with a few built-in libraries. If you prefer to add additioal JavaScript libraries, please take a look at the Custom libraries section.

Built-in JavaScript libraries
-----------------------------

**cheerio**

Simple library for working with the DOM model. Useful if you are getting back HTML. ::

responseHTML = cheerio(pm.response.text());
console.log(responseHTML.find('[name="firstName"]').val());

Read more: https://github.com/cheeriojs/cheerio

**crypto-js**

Library which implements different crypto functions.

Hash string using SHA256 ::

CryptoJS.SHA256("some string").toString()

HMAC-SHA1 encryption ::

CryptoJS.HmacSHA1("Message", "Key").toString()

AES Encryption ::

const encryptedText = CryptoJS.AES.encrypt('message', 'secret').toString();

AES Decryption ::

const plainText = CryptoJS.AES.decrypt(encryptedText, 'secret').toString(CryptoJS.enc.Utf8);

Read more: https://www.npmjs.com/package/crypto-js

Node.js libraries
-----------------

NodeJS modules that are available inside Postman:

- path
- assert
- buffer
- util
- url
- punycode
- querystring
- string_decoder
- stream
- timers
- events


Custom libraries
----------------

There is no standard way of including 3rd party JavaScript libraries.

Currently the only way is to fetch (and optionally store) the content of the JavaScript library and to use the JavaScript `eval` function to execute the code.

Template: ::

pm.sendRequest("https://example.com/your-script.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
}
eval(response.text());
// YOUR CODE HERE
});

Example loading a library using unpkg as a CDN: ::

pm.sendRequest("https://unpkg.com/papaparse@5.1.0/papaparse.min.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
}
eval(response.text());

const csv = `id,name\n1,John`;
const data = this.Papa.parse(csv); // notice the this
console.log(data);
});

Notice: in order to load a library and use it in Postman, the JavaScript code needs to be "compiled" and ready for distribution. Usually the code will be available as a *.min.js file or within the dist or umd folder.
9 changes: 9 additions & 0 deletions docs/request-creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,12 @@ Possible options:

- start a local server to serve that file and to get it in Postman with a GET request.
- use Newman as a custom Node.js script and read the file using the filesystem.

How to add a delay between Postman requests?
--------------------------------------------

To add a delay after a request, add the following in your Tests: ::

setTimeout(() => {}, 10000);

The example above will add a delay of 10000 milliseconds or 10 seconds.

0 comments on commit 0b26bf1

Please sign in to comment.