DEV Community

Cover image for Postman API Automation Script and integrate with Jenkins Documentation
Khairun Nahar Nowrin
Khairun Nahar Nowrin

Posted on • Edited on

Postman API Automation Script and integrate with Jenkins Documentation

Step 01: First create a API collection in Postman.
Step 02: Create Environment to store base URL, Auth token and other necessary key value.

Image description

After that select environment before check the API response.

Image description

Step 03: Instant of create an Environment can also store Base_URL in API collection variable or as a global variable.

Image description

Image description

Image description

Image description

Click on "Add new variable" and then set Scope.Thats all

Step 04: Store any kind of Dynamic data like - Auth token or Bearer token add this two lines of code on API collection Test scripts.

Image description



var response = pm.response.json();
pm.environment.set('access_token',response.data.access_token);


Enter fullscreen mode Exit fullscreen mode

And store token in collection variable section or global variable section (Follow step - 03)

Image description

For store any array value Simply reference an individual element of an array using the array name and the index inside parentheses, then use the assignment operator (=) followed by a value.



var response = pm.response.json();
pm.environment.set('access_token',response.data.access_token);
pm.environment.set('user_key',response.data.user[0].user_key);


Enter fullscreen mode Exit fullscreen mode

Step 05: After that i prefer write API test cases. For Test case i used - Qase.
Mainly API test case focus on BUSINESS LOGIC and we handle For Test case i follow this documentation -
Test case and requirment

Step 06: When I test a API Manually i focus on those check points.

  1. Keys verification. If we have JSON or XML APIs we should verify that all the keys are coming. For example we focus on the UI screen.
  2. Have a test case to do XML, and JSON Schema validation.
  3. Verify the Parse the Response data
  4. Verify the JSON Schema validation, Verify the Field Type, Verify the Mandatory Fields
  5. Valid Response headers & Negative Test Cases response
  6. Verify how the APIs error codes are handled.
  7. Verify the response HTTP status code.
  8. Valid Response payload
  9. Chaining Request verification.
  10. Verification of APIs with Data parameters.
  11. End to End CRUD flows
  12. Database Integrity Test Cases
  13. File Upload Test Cases

Step 07: For postman scripting i mention some common test scenario script.

For GET API -> Check 200 response and 404 response

Script 01 -



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


Enter fullscreen mode Exit fullscreen mode

Script 02 -



pm.test("Status code is 200 ", function () {
    pm.sendRequest('https://dummyjson.com/products/1', function (err, res) {
        pm.expect(err).to.not.be.ok;
        pm.expect(res).to.have.property('code', 200);
        console.log(res.json())
    });
});


Enter fullscreen mode Exit fullscreen mode

Script 03 -



pm.test("Status code is 404", function () {
    pm.sendRequest('https://dummyjson.com/products/one', function (err, res) {
        pm.expect(err).to.not.be.ok;
        pm.expect(res).to.have.property('code', 404);
        pm.expect(res.json().message).to.eql("Product with id 'one' not found");
        console.log(res.json())
    });
});


Enter fullscreen mode Exit fullscreen mode

Image description

For POST API -> Check 200 response and 404 response

Script 01



//Successful Add Product
const postResponse = {
  url: 'https://dummyjson.com/products/add',
  method: 'POST',
  header: {
    'Accept': '*/*',
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: {
    mode: 'urlencoded',
    urlencoded : [
      { key: 'title', value: 'Apple Magic Mouse'},
    ]
  }
};

pm.test("Test-01: User successfully add Product", function () {
    pm.sendRequest(postResponse, (error, response) => {
        pm.response.to.have.status(200);
        console.log(error ? error : response.json());
        });
});


Enter fullscreen mode Exit fullscreen mode

Script 02



/Failed to Add Product
const addProductFailResponse = {
  url: 'https://dummyjson.com/products/add_product',
  method: 'POST',
  header: {
    'Accept': '*/*',
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: {
    mode: 'urlencoded',
    urlencoded : [
      { key: 'title', value: 'Apple Magic Mouse'},
    ]
  }
};

pm.test("Test-02: User Failed add Product", function () {
    pm.sendRequest(addProductFailResponse, (error, response) => {
        pm.expect(response).to.have.property('code', 404);
        pm.expect(response).to.have.property('status', 'Not Found');
        console.log(error ? error : response);
        });
});



Enter fullscreen mode Exit fullscreen mode

Image description

For PUT API -> Check 200 response and 404 response

Script 01:



//Successfully Update Product
const postResponse = {
  url: 'https://dummyjson.com/products/two',
  method: 'PUT',
  header: {
    'Accept': '*/*',
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: {
    mode: 'urlencoded',
    urlencoded : [
      { key: 'title', value: 'Apple Magic Mouse'},
    ]
  }
};

pm.test("Test-01: User successfully update Product", function () {
    pm.sendRequest(postResponse, (error, response) => {
        pm.response.to.have.status(200);
        console.log(error ? error : response.json());
        });
});


Enter fullscreen mode Exit fullscreen mode

Script 02:



//Failed to Update Product
const updateProductFailResponse = {
  url: 'https://dummyjson.com/products/two',
  method: 'PUT',
  header: {
    'Accept': '*/*',
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: {
    mode: 'urlencoded',
    urlencoded : [
      { key: 'title', value: 'Apple Magic Mouse'},
    ]
  }
};

pm.test("Test-02: User Failed update Product", function () {
    pm.sendRequest(updateProductFailResponse, (error, response) => {
        pm.expect(response).to.have.property('code', 404);
        console.log(error ? error : response);
        });
});



Enter fullscreen mode Exit fullscreen mode

Image description

For DELETE API -> Check 200 response and 404 response

Script 01:



//Successfully delete Product
const postResponse = {
  url: 'https://dummyjson.com/products/two',
  method: 'DELETE',
  header: {
    'Accept': '*/*',
    'Content-Type': 'application/x-www-form-urlencoded',
  }
};

pm.test("Test-01: User successfully Delete Product", function () {
    pm.sendRequest(postResponse, (error, response) => {
        pm.response.to.have.status(200);
        console.log(error ? error : response.json());
        });
});


Enter fullscreen mode Exit fullscreen mode

Script 02:



//Failed delete Product
const deleteProductFailResponse = {
  url: 'https://dummyjson.com/products/90909',
  method: 'DELETE',
  header: {
    'Accept': '*/*',
    'Content-Type': 'application/x-www-form-urlencoded',
  }
};

pm.test("Test-02: User failed to Delete Product", function () {
    pm.sendRequest(deleteProductFailResponse, (error, response) => {
        pm.expect(response).to.have.property('code', 404);
        console.log(error ? error : response);
        });
});


Enter fullscreen mode Exit fullscreen mode

Image description

I mention some basic test scripts. To follow this script we can check any test scenarios without changing or run the API multiple request.

For more Script can follow this Postman Documentations

  1. https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/
  2. https://learning.postman.com/docs/writing-scripts/script-references/test-examples/#asserting-a-value-type
  3. https://community.postman.com/t/skip-request-based-on-condition-collection/17976/4

Step 8: Now our both manual testing and scripting is done so its time to run our test case in local machine and generate report. First run the collection on Postman. Click on Runner and drag and drop the API collection Run the collection.

Step - 1
Image description

Step - 2
Image description

Step - 3
Image description

Step - 4
Image description

Now we run the postman collection using CLI. The most important tool when try to automated a collection run is Newman and Newman is a CLI tool that can take a postman collection run all the test and at the end generate the report. If we use Newman we can run a postman collection with API tests on any professional server that deals with building nd testing software, like for example - Jenkins. For that we have to install newman. In order to use Newman locally on computer need to have nodejs install. If you already have newman then just check the version. Or if you do not have the newman then install it. For install newman follow the documentation - https://www.npmjs.com/package/newman

Image description



newman --version


Enter fullscreen mode Exit fullscreen mode

Run postman collection in cli we need to copy API collection JSON link
Image description
Then run the command



newman run https://www.getpostman.com/collections/da7c81fa859beeaf3d64


Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Now you can Notice that the entire execuion has been successful. All the test have been generated properly.

STEP - 09: HTML REPORT WITH NEWMAN
When working with newman is the possibility of generating reports and one of the report everyone use in the postman is htmlextra to generate a HTML report. This report will contain all log, full request, and full response. To install it simple go https://www.npmjs.com/package/newman-reporter-htmlextra and follow the installation step.
To create a report run this command



newman run https://www.getpostman.com/collections/da7c81fa859beeaf3d64 --reporters cli,htmlextra


Enter fullscreen mode Exit fullscreen mode

Image description

And here is my report. For mac the report was generated in file:///Users/your_user_name/newman/ -> folder. Can check request information in request section, Can investigate what happened with each request. Also have Failed test and Skip test section.

For More follow the tutorial - https://www.youtube.com/watch?v=VywxIQ2ZXw4

STEP - 10: Postman Collection in Jenkins
I integrate the test system with Jenkins so for that we need to install Jenkins. To install Jenkins we need Homebrew. Run this command to ensure that you have brew in your local machine.



brew --version
Homebrew 3.6.6
Homebrew/homebrew-core (git revision 2ce0c43d33a; last commit 2022-10-23)
Homebrew/homebrew-cask (git revision 38d1bb4d21; last commit 2022-10-23)


Enter fullscreen mode Exit fullscreen mode

Install Jenkins run this command
brew install jenkins-lts
After successfully install Jenkins, Lets start Jenkins to run command



brew services start jenkins-lts


Enter fullscreen mode Exit fullscreen mode

To Unlock Jenkins and generate Administrator password, just copy the file and run command



cat /Users/appnapws54/.jenkins/secrets/initialAdminPassword


Enter fullscreen mode Exit fullscreen mode

Copy the password and paste it.
Image description

Image description

For Jenkins Installation and Setup follow this Documentation and Video

  1. https://www.youtube.com/watch?v=ANd6Qzkwjus
  2. https://www.jenkins.io/download/lts/macos/

Set up Jenkins

  1. With Jenkins running, go to http://localhost:8080 and sign in.

  2. On the Dashboard page, select New Item on the left sidebar to create a new job.

  3. Select a Freestyle project from the options. Name your project, and select OK.

  4. In General > Build, add a build step in the project, and choose Execute Shell. The build step executes a shell command. Enter a shell command to run, such as newman run ~/Desktop/jenkins_demo_postman_collection.json.

  5. In Build Environment > Build Environment, select Provide Node & npm bin/ folder to PATH and choose the NodeJS install you configured with Newman.

  6. Select Save to finish creating the project.

Troubleshooting

  1. Run this build test manually by selecting Build Now in the sidebar.

  2. run build

Jenkins indicates that the build has failed with a red cross next to the build in the build history. This is because of the intentionally failed tests in the collection.

Select the build in the history list, then select Console Output to inspect what Newman returned.

Fix these tests inside your collection. Export it and then run the build again.

Jenkins indicates that the build succeeded with a green checkmark.

Configure frequency of runs
To set the frequency with which Jenkins runs Newman:

  1. Open the build window.

  2. Select Configure.

  3. Go to Build Triggers and select Build periodically.

  4. Enter a schedule. The syntax for setting the frequency to every 30 minutes is H/(30) * * * *.

  5. Select the help icon next to Schedule to learn how to specify the build frequency.

  6. Select Save.

Jenkins will now run Newman at your desired frequency and will tell you if the build failed or succeeded.

Postman Integrating with Jenkins

  1. https://learning.postman.com/docs/running-collections/using-newman-cli/integration-with-jenkins/

*Jenkins Report generate and collect with github *
1.https://www.youtube.com/watch?v=3_SCjA9616c
2.https://blog.logrocket.com/how-automate-api-tests-postman/
https://www.youtube.com/watch?v=tKZsjZ0bGlU

Top comments (0)