5 ways to improve your automated POSTMAN tests.
Published: 2020-11-16 17:02:14

---
title: 5 ways to improve your automated POSTMAN tests.
published: false
date: 2020-05-23 00:00:00 UTC
tags: 
canonical_url: https://hvitis.dev/5-ways-you-could-improve-your-automated-postman-tests
published: true
---

## Improve your POSTMAN tests.

The methods I am sharing in this article allow me to prepare more robust tests. Some example use cases would be:

- Wondering how to create random number or random name for a variable? Need to use some jQuery for parsing HTML? [Try using 3rd party libraries in POSTMAN sandbox](#using-3rd-party-libraries-in-pre-scripts).
- Executing POST with body that needs to be changed? No problem! Try to make some [changable body conent](#passing-prerequest-to-body) by preparing it in pre-requests.
- Your requests just return a rendered page HTML? Try [parsing it and scrapping](#parsing-data-from-HTMLs-forms) for the data you need ( **tokens** for example ).
- Your request response makes the next request redundant? You want to execute previous request? [Queue execution of specific request](#setNextRequest).
- You are recieving big objects of important data? Verify just its format by [validating with schema](#validating-schema).

* * *

## Using 3rd party libraries in pre scripts

You want to create random numbers? use lodash library? jQuery? Chai assertions?Maybe you need some high level time operations for your scripts? MomentJS? No problem.

- Passing, parsing URLs
- Validating Schemas
- Decoding, encoding keys, tokens with SHA256 (atob, btoa)

No problem. POSTMAN allows you to use [3rd party libraries](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/) in its sandbox. Next tips will be tightly connected with this information, so have as look before proceeding!

## Passing prerequest to body

Let’s say SWAGGER tells you that in body you need to pass string as title:

[![swagger_postman](https://hvitis.dev/static/10da27252b913a8ec3d2e522b0f52526/4d236/ss-swagger-postman-docs.png "Swagger Endpoint Mapping")](/static/10da27252b913a8ec3d2e522b0f52526/741eb/ss-swagger-postman-docs.png)

You can programaticaly change your body content in pre-request scripts so that e.g. each time you make a request you have different Job title.

First open your pre-request scripts tab, prepare your body and **stringify it**.

[![postman-pre-script](https://hvitis.dev/static/676db402caea38d6ca8c9afff76e8957/4d236/ss-postman-prerequest-trick-script-add-on-extension.png "Sending request with prerequest scripts")](/static/676db402caea38d6ca8c9afff76e8957/fe3aa/ss-postman-prerequest-trick-script-add-on-extension.png)

(code version in case you need to copy)

```
const title = "Boss' Assistant nr " + _.random(0, 10)

const preparedBody = {
    "title" : title
}

pm.environment.set("job_object", JSON.stringify(preparedBody))
```

You have to set this body object as an environment, global or collection variable. Once you do it, before executing the script you just need to use it in body like that:

[![postman-pre-script-use](https://hvitis.dev/static/4edc23247710f1795fd3b2aaf55cf8ea/4d236/ss-postman-body-request.png "Sending request with prerequest scripts")](/static/4edc23247710f1795fd3b2aaf55cf8ea/9f555/ss-postman-body-request.png)

Now each time you execute this request you will get new Job title:

```
"title": "Boss' Assistant nr 5"
...
"title": "Boss' Assistant nr 2"
...
"title": "Boss' Assistant nr 6"
```

## Parsing data from HTMLs forms

General use case: There is some visible or hidden data within the HTML tags on the rendered page that you need to pass / verify / check.

Specific use cases: You have a page that has a login fields with generated token that you need for next request.

Let’s say that you make a request to some API and it redirects you to a page with input fields that have tokens assigned. That’s a common use case with SAML and SSO. You can practice [here](https://app.onelogin.com/login) on a real use case. This login field has a token.

[![input-token](https://hvitis.dev/static/534b6424a166b90727b77bb9e06a4e15/fdd36/ss-input-field-token.png "Sending request with prerequest scripts")](/static/534b6424a166b90727b77bb9e06a4e15/fdd36/ss-input-field-token.png)

On requesting the URL of this page we will get just normal HTML:

[![postman-html-request](https://hvitis.dev/static/9f11cc271cc9eaaf05b1b08d3bf11fd6/4d236/ss-make-html-request-postman.png "HTML body in POSTMAN request")](/static/9f11cc271cc9eaaf05b1b08d3bf11fd6/f7a66/ss-make-html-request-postman.png)

As you’ve read in a previous tip, we can use libraries provided in POSTMAN SandBox. jQuery is [replaced by CheerioJS](https://blog.postman.com/jquery-replaced-by-cheeriojs-in-postman-sandbox/) which emulates jQuery core API.

Let’s load the library and see if it’s working:

```
const $ = cheerio.load(pm.response.text())
console.log('Title of the page: ', $('title').text())
```

Now you just need to use your selectors magic knowledge. Let’s query for the TOKEN we need:

```
console.log($('input[name=authenticity_token]').attr('value'))
```

( **TIP 1:** If you are not good with selectors, click right mouse button on element you are looking for and then **copy selector** )

( **TIP 2:** If you need other information from HTML elements, check the [Cheerio documentation](https://github.com/cheeriojs/cheerio) for more. )

And finally let’s write the test and export the TOKEN:

```
pm.test('input field it should have a token', () => {
  let TOKEN = $('input[name=authenticity_token]').attr('value')
  pm.expect(TOKEN).to.not.be.empty
  pm.environment.set('TOKEN', TOKEN)
})
```

Now you can use the TOKEN in other requests.

## SetNextRequest

I’m assuming you know what is a postman collection. It’s a set of requests or folders with requests, depending how you order your work. The basic workflow that POSTMAN uses is checking request one after the other. It goes from the top to bottom basically. Most of this is well [described already](https://learning.postman.com/docs/postman/collection-runs/building-workflows/#getting-started) but I will just mention that with

```
postman.setNextRequest('REQUEST_NAME_HERE')
```

You can go back to **previous requests** or **skip some requests** or **loop through requests** [7](https://learning.postman.com/docs/postman/scripts/branching-and-looping/). It’s IMPORTANT to remember that naming convention in your requests that follows e.g. CamelCase and has consistent information **helps tremendously** because it makes your code cleaner.

Instead of using default naming:

[![postman-request-naming](https://hvitis.dev/static/0f0d231f46ec04eb0377b19e172df287/efa85/ss-naming-postman-requests.png "Naming POSTMAN request")](/static/0f0d231f46ec04eb0377b19e172df287/efa85/ss-naming-postman-requests.png)

Try naming your request in a way that will be easier to write them in the scripts when passing their names into your **setNextRequest()** e.g. using request method and no spaces:

[![good-postman-request-namingt](https://hvitis.dev/static/62b6cf9eb54336bacf5d4331e39ea4eb/41bf6/ss-naming-postman-requests-good.png "Naming POSTMAN request good")](/static/62b6cf9eb54336bacf5d4331e39ea4eb/41bf6/ss-naming-postman-requests-good.png)

## Validating schema

Imagine you make a test for checking if your API returns a Bank Account number.

```
const jsonData = pm.response.json()
pm.test('returns IBAN as a number', () => {
  const IBAN = jsonData.IBAN
  pm.expect(IBAN)
    .to.be.an('number')
    .that.does.not.include('ES')
})
```

You [assert](https://www.chaijs.com/api/bdd/) that it should be a number and it works fine but what if you would need to be sure as per data types of an entire response of object with 50 keys? [Using assertions](https://postman-quick-reference-guide.readthedocs.io/en/latest/assertions.html) would be cumbersome.

That’s a use case for [schema validation](https://json-schema.org/understanding-json-schema/about.html). POSTMAN allows us to use

- tv4 - Tiny Validator (for v4 JSON Schema)  
and / or
- Ajv - Another JSON Schema Validator

The [documentation](https://geraintluff.github.io/tv4/) for both libraries shows all the API you could use. I will just show you the basic idea behind it:

```
// We create a schema for our response
var schema = {
  IBAN: {
    type: 'number',
  },
}
```

and write a test for it:

```
const jsonData = pm.response.json()
pm.test('Schema is valid', () => {
  pm.expect(tv4.validate(jsonData, schema)).to.be.true
})
```

We could verify properties, whether field is required, nested objects, referencing and many more. Always remember to [**read documentation**](https://www.npmjs.com/package/ajv) before starting to know the full potential of your tools!

* * *

* * *

_Did you make any mistakes when using POSTMAN or you’ve seen one here? Tell me about your insights. Leave a comment with YOUR opinion._