How to schedule Collection run in Postman?

HOME

How to Create an Environment in Postman?

HOME

{
    "email": "eve.holt@reqres.in",
    "password": "pistol"
}

Integration of Postman with Jenkins

Last Updated On

HOME

Postman contains a full-featured testing sandbox that enables you to write and execute JavaScript-based tests for your API. You can then integrate Postman with your CI/CD build system using Newman, the command-line collection runner for Postman. In this tutorial, we are going to learn how we can integrate Postman with Jenkins.

Table of Contents

Prerequisite:

  • Jenkins installed on the machine
  • NodeJS installed on the machine

Implementation Steps

Step 1: Download the NodeJS Plugin

To generate a Performance Report in Jenkins, we need to download NodeJS Plugin. Please refer to this tutorial to install the plugin – How to install Plugins in Jenkins.

Go to Manage Jenkins > Manage Plugins and install the NodeJS plugin.

Step 3: Create a new FreeStyle project

  1. Give the Name of the projectPostman_Demo
  2. Click on the FreeStyle project. 
  3. Click on the OK button.

In the General section, enter the project description in the Description box.

Step 4: Source Code Management

Select Source Code Management as None if the project is locally present on the machine.

Step 6: Select Execute Windows batch command

In the Build Steps section, select Execute Windows batch command.

Use the below command to go to the path where the JMeter is placed in your system.

cd C:\Users\Vibha\Desktop\Postman
newman run --disable-unicode API_Tests.json

We have created a new project Postman_Demo” with the configuration to run the Postman scripts.

Step 7: Execute the tests

Let’s execute it now by clicking on the “Build Now” button. 

Right-click on Build Number (here in my case it is #1).

Click on Console Output to see the result.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Additional Tutorials

What is Collection in Postman?
Data Driven Testing in Postman
 How to import Collection into Postman?
 How to run Postman requests from the command line
 How to generate Newman HTML Report?

How to generate Newman HTML Report?

HOME

npm install -g newman-reporter-htmlextra

newman run API_Newman_Tests.json -r htmlextra

newman run API_Newman_Tests.json -r htmlextra --reporter-htmlextra-export ./results/report.html

How to import Collection into Postman?

HOME

How to run Postman requests from the command line

HOME

In this tutorial, we will run the Postman requests from the command line. Newman is a command line collection runner for Postman.

node -v

npm -v

npm install -g newman

On cmd go to that location of collection json file

cd C:\Users\Vibha\Desktop\Automation\Postman

newman run API_Testing.json

How to Export Postman Collections?

HOME

How to create Tests in Postman

HOME

In this tutorial, we will see how we can create Tests in the Postman.

Consider a sample JSON which has 4 sets of data. This data we will be passing to a POST request.

Create a request in Postman. Below is the URL of the request.

https://reqres.in/api/users

Paste the body of the request in the Body part of the Postman.

Create Tests for the request

Click on the “Tests”. Write the tests as shown below:

pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

pm.test("The endpoint does not return unexpected status code", () => {
 
  // change 404 to the response code you do not expect
  pm.response.to.not.have.status(404);
});

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

pm.test("API response contians the expected header", () => {
  pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("Postman");
});

The output of the above test is

pm.test("Value of name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.name).to.eql("Postman");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the name field is Postman.
  pm.expect(response).to.have.property("name", "Postman");

  // the line below checks value of the job field is Test (string).
  pm.expect(response).to.have.property("job", "Test");
});

The output of the above test is

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

The output of the above test is

const responseJson = pm.response.json();
pm.test("Test Data Type of the response", () => {
    
    pm.expect(responseJson.job).to.be.a("string");
    pm.expect(parseInt(responseJson.id)).to.be.a("number");
});

pm.test("The response has all properties", () => {
    //parse the response JSON and test three properties
    const responseJson = pm.response.json();
    pm.expect(responseJson.name).to.eql("Postman");
    pm.expect(responseJson.job).to.be.a('string')
    pm.expect(responseJson.id).to.have.lengthOf(3);
});

Sample Response

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the id field is 2 (number).
  pm.expect(response).to.have.property("id", 2);

  // the line below checks value of the name field is Morty Smith (string).
  pm.expect(response).to.have.property("name", "Morty Smith");

  // the line below checks value of the origin.name field is Earth (C-137) (string).
  pm.expect(response).to.have.nested.property("location.name", "Citadel of Ricks");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  pm.expect(response).to.have.nested.property("episode.3", "https://rickandmortyapi.com/api/episode/4");
});

Data Driven Testing in Postman

HOME

In this tutorial, we will see how we can use data files to feed test data to the Postman requests.

Postman provides two ways to pass sets of values to use in a collection run.

By selecting a JSON or CSV data file in the Collection Runner, you can test your requests with multiple different values as part of a single run.

1. Passing data using JSON File

Consider a sample JSON which has 4 sets of data. This data we will be passing to a POST request.

[
   {
      "name":"Test1",
      "job":"Developer"
   },
   {
      "name":"Test2",
      "job":"Scrum Master"
   },
   {
      "name":"Test3",
      "job":"Quality Analyst"
   },
   {
      "name":"Test4",
      "job":"Business Analyst"
   }
]

Create a request in Postman. Below is the URL of the request.

https://reqres.in/api/users

Paste the body of the request in the Body part of the Postman.

Here, name and job are the variables that we will be passing to the POST request. So, we have parameterized these 2 variables.

Create Tests for the request

Click on the “Tests”. Write the tests as shown below:

If you need help in creating the test, you can refer to the snippets present on the right side of the console.

Here, data.json is the name of the json we are using in this example.

To read the JSON data we need to upload a JSON file from the collection runner.

Select the more actions icon More actions icon next to the collection name. Click on the “Run Collection” option.

This shows all the requests present in this collection. Here, I want to run only CreateUser Request. So, I will uncheck the “Dummy User” option.

On the Functional tab, select Run manually.

Click on the Select File and upload the data file. The Iterations field shows the number of data sets present in the data file. Here, we have 4 sets of data, so used Iterations as 4. The Delay field defines the time in ms between two iterations.

Click on the preview to see the data that is uploaded.

Click on the “RunDataDrivenTesting” button to run the tests available in the Collection.

We can see that our API has run four times. Each time, a different set of data is picked up.

2. Passing data using CSV File

All the steps remain the same here, except instead of a JSON, we will use a .csv file.

Click on the “Preview” button to view the data present in the .csv file.

Below are the sample tests.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(201);
});

var expectedName = pm.variables.get("name");
var expectedRole = pm.variables.get("role");


pm.test("Verify name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(expectedName);
});

pm.test("Verify role", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(expectedRole);
});


Run the collection again. Below is the image of the test execution in Postman.

What is Collection in Postman?

HOME

What is Collection?

In Postman, a collection is a group of API requests that are saved in Postman and can be organized into folders. Within a collection, any number of folders can be formed. All API requests can be saved and stored in a collection, which can be shared around the team in the Postman workspace.
Organizing comparable requests into folders and collections enables the customer to better organize and document their demands.

For example, if you are testing or validating a restful API that has 10 different endpoints. Then, it makes sense to organize them in a collection that would make things like applying collection variables, import/export easier and could be run as part of a single collection.

How to create a Collection?

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “Demo”.

How to create a request in Collection?