Skip to main content

Tips and tricks for automation using Postman/Newman

  

1. How do we run multiple newman/postman collections sequentially (back to back) 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: Give a shot to https://medium.com/@mnu/run-multiple-postman-collection-in-parallel-stress-ee20801922ed in case you would like to run it in parallel (P.S. haven’t tried this yet)

 

2. How can we set environment variable based on another environment variable value? Like for QA1, we need accountNo say "123" but for QA2, we need accountNo say "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");

    }

});

 

3. 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, as the name suggests, allows you to specify which request runs next.

NOTE: Name of the request should be exactly same as you have written while naming the request as it is case sensitive.

More details: https://learning.postman.com/docs/running-collections/building-workflows/

 

4. How to skip test based on environment?

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

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”);

});

 

5. How to set delay while running a collection?

newman run collection.json --delay 10000

 

6. 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

 

7. Looking for an extensive report after your execution run?

Please check here: https://github.com/DannyDainton/newman-reporter-htmlextra

It comes with a nice dashboard style. It is very precise and concise one- I love it.


Comments

  1. Nice. Do you have an example of #1 in javascript, not shell?

    ReplyDelete
    Replies
    1. Give a shot to this https://community.postman.com/t/required-to-run-multiple-collection-through-newman/10792/6

      Delete

Post a Comment

Popular posts from this blog

How to Unzip files in Selenium (Java)?

1) Using Java (Lengthy way) : Create a utility and use it:>> import java.io.BufferedOutputStream; import org.openqa.selenium.io.Zip; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream;   public class UnzipUtil {     private static final int BUFFER_SIZE = 4096;     public void unzip (String zipFilePath, String destDirectory) throws IOException {         File destDir = new File(destDirectory);         if (!destDir.exists()) {             destDir.mkdir();         }         ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));         ZipEntry entry = zipIn.getNextEntry();         // to iterates over entries in the zip folder         while (entry != null) {             String filePath = destDirectory + File.separator + entry.getName();             if (!entry.isDirectory()) {                 extractFile (zipIn, filePath);            

The use of Verbose attribute in testNG or POM.xml (maven-surefire-plugin)

At times, we see some weird behavior in your testNG execution and feel that the information displayed is insufficient and would like to see more details. At other times, the output on the console is too verbose and we may want to only see the errors. This is where a verbose attribute can help you- it is used to define the amount of logging to be performed on the console. The verbosity level is 0 to 10, where 10 is most detailed. Once you set it to 10, you'll see that console output will contain information regarding the tests, methods, and listeners, etc. <suite name="Suite" thread-count="5" verbose="10"> Note* You can specify -1 and this will put TestNG in debug mode. The default level is 0. Alternatively, you can set the verbose level through attribute in "maven-surefire-plugin" in pom.xml, as shown in the image. #testNG #automationTesting #verbose # #testAutomation

Encode/Decode the variable/response using Postman itself

We get a lot of use cases where we may have to implement Base64 encoding and/or decoding while building our APIs. And, if you are wondering if it is possible to encode/decode the variable/response using Postman itself or how to encode/decode the token or password in postman and save it in a variable? To Base64 encode/decode, the quickest way is to use JavaScript methods btoa, atob: atob - It turns base64-encoded ASCII data back to binary. btoa - It turns binary data to base64-encoded ASCII. Sample code : var responseBody = pm.response.json(); var parsedPwd = JSON.parse(atob(responseBody.password)); // presuming password is in the payload pm.collectionVariables.set("password", parsedPwd);