logo logo

Tester’s Month Story – Tips And Tricks For Automation Using Postman

Tester's Month Story - Tips And Tricks For Automation Using Postman

Postman is one of the popular API testing tools in the market. In this article, I will share some tips and tricks for automation using Postman. If you’d like to learn the basics beforehand, consider reading this article on how to get started with postman for API testing 🔍

📌 Another great option is with TestProject‘s free test automation platform. Here’s a hands-on tutorial to help you get started: Automating End to End API Testing Flows Guide.

The tools/dependency you need to get started:

  1. Postman
  2. npm/Node
  3. Newman

How to run multiple collections sequentially at one run

Batch running multiple Newman/Postman test collections:

Create a batch file like this and run:

call newman run Collection1.postman_collection.json -e qa1.postman_environment.json -r htmlextra --reporters cli,junit,htmlextra --reporter-htmlextra-export TestReport1.html -d TestData1.csv
call newman run Collection2.postman_collection1.json -e qa2.postman_environment1.json -r htmlextra --reporters cli,junit,htmlextra --reporter-htmlextra-export TestReport2.html -d TestData2.csv

Side note: Read this article in case you would like to run it in parallel (P.S. I haven’t tried this yet).

How to set an environment variable based on another value

For example, for QA1, we need account number “123”, but for QA2, we need account number “345”, etc.

pm.test("Set environment variable_value based on Env type.", function () {
if (pm.environment.get('env') == 'qa1') {
pm.environment.set("variable_key", "variable_value");
} else if (pm.environment.get('env') == 'qa2') {
pm.environment.set("variable_key", "variable_value");
 else if (pm.environment.get('env') == 'qa3') {
pm.environment.set("variable_key", "variable_value");
}
});

How to set request workflows

When you start a collection run, all requests are run in the order you see them in Postman. So, all requests are executed first, by order of the folder, and then any requests in the root of the collection. However, you can override this behavior using a built-in function called postman.setNextRequest(“request_name”). This function allows you to specify which request runs next ⏭

Note: the name of the request should be exactly the same as you have written while naming the request, as it is case-sensitive. You can see more details here.

How to skip tests based on environment

There are many ways to achieve it but the easiest one would be setting the environment variable and use that in your script:

const skipTest= pm.environment.get('env') == 'Prod';
(skipTest ? pm.test.skip : pm.test)("Check Prod requests", function()
{
const response = pm.response.json();
pm.expect(response.key).to.eql(“XXXX-XXX”);
});

How to set delay while running a collection

newman run collection.json --delay 10000

How can we run specific folder(s) inside a collection

Use the argument: –folder <name>

Run requests within a particular folder or/and folders or specific requests in a collection. Multiple folders or requests can be specified by using –folder multiple times, like: –folder f1 –folder f2

Sample:

newman run "<<yourCollection.postman_collection.json>>" -e <<yourEnvironment.postman_environment.json>> --folder "<<folderName1>> --folder "<<folderName2>>" -r htmlextra --reporters cli,junit,htmlextra --reporter-htmlextra-export TestReport.html

Looking for an extensive report after your execution run?

Please check here. It comes with a nice dashboard style. It is a very precise and concise one – I love it 🤩

Conclusion

I hope these tips & tricks were helpful and demonstrate that by using Postman we can do so much in terms of API automation. So, give it a shot to get a stronghold on API automation 💫

Dheeraj Gambhir

About the author

Dheeraj Gambhir

An innovative IT professional with over 14.5 years of experience in the IT industry. Very passionate about sharing Software testing stories with a wider community. A quality champion who knows how to get even the most complex things done in a simpler way.

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Leave a Reply

popup image

API testing has never been easier

Explore this step-by-step guide to easily achieve flawless automated API testing flows, without any 3rd party tools and without a single line of code. A bunch of test examples are included!
Get Started
FacebookLinkedInTwitterEmail