Writing first test script and chaining outputs between scripts.

Till now , we have executed requests but have not written any testscripts. Meaning, there were no validation steps to verify whether the request passed or failed.

Now let us see who to do this. Let us write our first test script.!!!! 🙂

Automation tests with Postman:

There are two parts were we can write the scripts for our tests.

  1. Pre-request script
  2. Tests

Here pre-request script act as the test setup script, meaning, it will get executed before the tests. So if you have any pre-requisite like creating user variable or validating whether a user variable value exists, you can do it here.

Tests are were we write the actual tests, it get executed after we get the response.

Writing first Test script:

The syntax of writing tests are as follows:

pm.test("Test_case_description", function () {
    <Validation steps!> ( The steps are available as test snippet you just need to select it )
});

Note:

We can write tests at both the places ( Pre-request or Tests) but as tests are meant to be validating the user action, it should be written at the ‘Test’ tab so that it will be executed only after the action is completed.

Postman pm.test function expects different exceptions and passes or fails the test accordingly. You can try running just the below command without the ‘pm.test’ enclosed and observe that on sending the request an assertion is thrown.

(The below script is to validate whether the text ‘test’ as sub-string ‘b’ in it.)

pm.expect('test').to.have.string('b');

Now enclose the command in pm.test as below on both pre-request and Tests tab:

pm.test("Check string", function () {
   pm.expect('test').to.have.string('b');
});

Now send the request and you will observe that two tests were executed.

Run the same using collection runner (Make sure to click save before running runner):

Chaining the response together:

Get the data from response body and store it in an environmental variable:

pm.test("Get email", function () {
    var jsonData = pm.response.json();
    var email= jsonData.data[2].email;
    pm.environment.set("email", email);
});

Use this environmental variable in next test.

{
    "email": "{{email}}",
    "password": "cityslicka"
}

Till now , we have executed requests but have not written any testscripts. Meaning, there were no validation steps to verify whether the request passed or failed.

Now let us see who to do this. Let us write our first test script.!!!! 🙂

Automation tests with Postman:

There are two parts were we can write the scripts for our tests.

  1. Pre-request script
  2. Tests

Here pre-request script act as the test setup script, meaning, it will get executed before the tests. So if you have any pre-requisite like creating user variable or validating whether a user variable value exists, you can do it here.

Tests are were we write the actual tests, it get executed after we get the response.

Writing first Test script:

The syntax of writing tests are as follows:

pm.test("Test_case_description", function () {
    <Validation steps!> ( The steps are available as test snippet you just need to select it )
});

Note:

We can write tests at both the places ( Pre-request or Tests) but as tests are meant to be validating the user action, it should be written at the ‘Test’ tab so that it will be executed only after the action is completed.

Postman pm.test function expects different exceptions and passes or fails the test accordingly. You can try running just the below command without the ‘pm.test’ enclosed and observe that on sending the request an assertion is thrown.

(The below script is to validate whether the text ‘test’ as sub-string ‘b’ in it.)

pm.expect('test').to.have.string('b');

Now enclose the command in pm.test as below on both pre-request and Tests tab:

pm.test("Check string", function () {
   pm.expect('test').to.have.string('b');
});

Now send the request and you will observe that two tests were executed.

Run the same using collection runner (Make sure to click save before running runner):

Chaining the response together:

Get the data from response body and store it in an environmental variable:

pm.test("Get email", function () {
    var jsonData = pm.response.json();
    var email= jsonData.data[2].email;
    pm.environment.set("email", email);
});

Use this environmental variable in next test.

{
    "email": "{{email}}",
    "password": "cityslicka"
}

First steps with Postman…

Ok lets start working with postman!!!!

Install Postman:

https://www.getpostman.com/downloads/

Open Postman:

Create Workspace:

Work space allows you to organize your work. It creates a separate working space where you group all your works including tests,collections, environments etc ( will learn as you read on).

Click My work space and select create New workspace

Note: Workspace could be personal, private or Team. Personal will be available only to you, shared will be available to everyone else with the invitation link and private is enterprise version and allows team members who were invited by you to be part of the workspace.

Team workspace and My Workspace are default workspace and cannot be deleted. To delete other workspaces, goto All workspace (will be opening in browser) and select delete by click ing the meatball menu.

Create Collection:

Collection allows to group your requests together in a logical order. It could be seen as the test suite level and we will have individual requests as the tests.

You also create folder inside postman to group your tests more efficiently for example :

Collection name be “Android FaceAPP”

And Folder name be “Test_LoginFeature”

Click collections>New collections for creating new collection

Click meatball menu> Add folder, to create folder

Create your first request:

Create a new request by clicking New or by using meatball menu corresponding to the desired collection and folder.

Add the request details:

And click save ( don’t forget to click save else the request won’t be saved onto collection- folder hierarchy)

Then click send.

Note: Use https://reqres.in/ to get API for practice.

Running Collection Runner:

So you can create as many requests as you want, now imagine you have to execute all this requests in order.

You can do this by using collection runner.

Click on the collection you want to run and click play icon, and click run.

The below window opens up and now click run <collectinname>

This will execute all requests in the collection and shows the result.

Note: Here test results shows ‘0’ passed and failed as there were no validation scripts added. We will learn it in next sections.

Running collections from command line:

For running collections through command line we need to use newman tool

To install newman you need npm (npm is installed as part of nodejs installation , Install here) and then use the below command:

npm install -g newman

Note: make sure that nodejs modules are in path or else add it to path , to ensure newman command is recognized from cmd.

Now to run the tests from command line create a share link or export collection as json

Through Share link:

Click the collection’s meatball menu and click share and navigate to ‘Get link’ and generate a link.

copy the link address and run the command:

newman run <link_address>

Through json:

Click the meatball menu and select export, this creats a json file for the collection

now run

newman run <full_path>/TestApp.postman_collection.json

Note: This could be used for running postman in CI/CD pipeline

HTML Reporting

We have seen how to run postman through command line using newman ( just npm install newman and use command ‘newman run <collection>.json’ or url)

To get html reporet install newman-reporter-htmlextra

npm install -g newman-reporter-htmlextra

Now run the scripts as

newman run <collection_file>.json -r htmlextra