Enroll in Selenium Training

In the introduction to newman and running collection tutorial we have focused on the official definition of Newman provided on the Postman official website. Postman's website says that Newman allows you to run collections in the same way they are executed inside a Postman collection runner. In the collection runner tutorial we ran collections and executed tests with few other settings such as setting the number of test iterations, providing some delay etc. If that is what Postman website says then it is also definite that we can do similar things from Newman. Focusing on the same, in this tutorial we will learn about:

  • How to Run a folder, which is inside Postman Collections in Newman?
  • How to set number of test iterations in Newman?
  • How to set the delay in Newman?
  • How to set environment variable in Newman?

Before starting the topics, I want to tell you the basic syntax of running collection through Newman.

Newman Optional Parameters & Configurations

Till now we only learnt that we have a collection and we want to run it through Newman. In this tutorial as I mentioned we will be setting some other collection features to apply them to our collection and then run. So when we have to apply [options] (as the Newman website says) there is a particular syntax we follow, and this will help you remember everything we learn later in this tutorial in an easy way.

The newman syntax is as follows

newman run <collection-file-source> [option]

There are many options that can be applied to a collection through Newman or Postman app for example setting up an environment variable or specify the  . While we have learnt it in Postman application, there is a need to use them using Newman because we cannot set these options through the app and run it through Newman. So the options has been divided into four parts

  • Utility: This include help and getting the version
  • Basic Setup: This contains setting up different options in your collection such as environment.
  • Request Options: These are the options which directly affect the requests such as delay request (specifying delay between requests)
  • Misc : These are other small options that does not fall in any other category discussed above. These include like disabling the color of the interface etc.

You can learn about each and every option on the Postman Newman documentation.

Running a folder inside a collection using Newman

In the collections tutorial we learnt about the folders inside a collection. A folder can be created inside a collection to combine similar APIs for better understanding. For example, If you have a folder named movies (similar to collections in Postman) then you can have two different folders inside it named Hindi movies and English movies (similar to folders inside a collection). But in a real and big project we have huge number of APIs inside a collection, so we segregate those into different folders. There can be a need when someone like to just test one set of APIs which are in single folder. In that case there is no point to execute all the collection as a whole, as it will execute all the folder which comes under the collection. Just like Postman, Newman also gives us the ability to run just a folder from collections.

1.Using the same collection that we used in the previous tutorial of  (Running Collections with Newman), make two folders and move each request to a folder of their name (it would be better if you make copy of Newman Collection since we need to get to original setting after this section)

For example: Move the customer Register API to Customer Register folder and Weather API to a weather API folder as shown.

Newman_Collection_Copy

  1. Export your Collection as JSON, as discussed in Running Collections with Newman.

  2. Go to the shell of your system and type the following command:

newman run <collection_name> --folder <folder name>

Newman_Running_Folder

  1. Press Enter and see that the folder you wanted has been executed successfully.

Newman_Running_Folder_Response

Setting test iterations using Newman

In the collection runner tutorial we learnt to set iterations on our collection which was actually the number of times our collection will run repeatedly. Iteration value set to 5 will execute all the APIs five times. We will try to achieve the same here by following the steps:

  1. We will use the same exported json collection file that we did above.

  2. Type the following command.

newman run  <collection_name> -n <number of iterations>

Number_of_iterations_Newman

Note: We are setting the iteration value to 2 here and running it on weather folder only.

  1. Press Enter and you will be able to see all your tests and APIs being executed two times.

Number_of_iterations_Newman_Response

Setting delay using Newman

Delay is also discussed in the Collection Runner tutorial, that delays are the time intervals between execution of each iteration. So a delay of 2 seconds will run the folder again after every 2 seconds. We will try to achieve same through Newman by following the steps.

1.We will be using the same exported JSON file that we are using till now.

  1. Go to your shell and type the following command.

newman run <collection_name> -n 2 --delay-request 5000

Newman_delay_request

Note: We are using 5s delay and running on only one folder. You can run it on both or other folder.

  1. Press enter and you can see that the second execution of collection runner was after a delay of 5 seconds.

Setting environment variables using Newman

Just as we used the environment variables in Postman, we can also set the environment variables in Newman.

First of all you need to create one environment called Newman_Env in Postman, which has only one environment variable address with the value of the address of url as shown.

address_environment_variable

Note : Refer the tutorial of Environment Variables in Postman for help.

Now change the domain of url in the request to the variable address and see if both the tests passes and variables work or not in your postman application. Both test will work, as the environment variable is set in the Postman and tests are being executed with in the Postman.

address_variable_in_request

Both test will work, as the environment variable is set in the Postman and tests are being executed with in the Postman.

Now we will try to run the same in Newman. For this you need to export the collection again since this copy is different from what you already have in your system.

Go to shell and again run the same command in Newman. You will encounter error now.

Environment_Variable_error

Note: Please refer to this tutorial for learning about running a collection in Newman.

Newman does not know what is address and hence throws an error INVALID URI. This has happened because address is an environment variable stored in an environment of which Newman has no idea about. So it won't run until we specify this environment specifically in Newman. So here we go.

Remember what we learnt in the above section about specifying the options to run along with the collection. We will go with the same syntax here. For specifying environment --environment option is used, so the complete syntax becomes

newman run <collection> --environment <file>

Let's start by specifying the environment, Write the following code in Newman:

newman run <collection> --environment Newman_Env

Specifying_Environment_Without_Export

Press enter to see the result.

Specifying_Environment_Without_Export_2

We are still getting the error. It is so because Newman does not work that way. For telling anything to Newman such as setting the environment, we can only do so by specifying the file which has environment and not environment directly. For this we need to export the environment.

How to export the environment

Exporting the environment is very simple and straightforward as it was for exporting the collection. Follow the given steps to export an environment.

1.Go to settings (gear icon)

  1. Alongside the environment name, you will see a download icon (downward arrow)

Export_Environment

  1. Press the icon and download the environment.

Now we have our environment downloaded, we can continue to set the environment using Newman. Remember to save the environment file in the same location as you have your collection. Since we change the directory before running the collection, our system does not allow newman to access the file out of that directory.

Write the following code in Newman.

newman run <collection> --environment <file>

Specifying_Environment_With_Export

Press enter and you can see the results like earlier without the environment.

Here you go. We have successfully specified an environment and run the collection using the environment variable. With concluding this part we have concluded all the most common usage of Newman. Hope you have a good grasp of Newman and always remember, keep practicing.

Running Collection Using Newman
Running Collection Using Newman
Previous Article
Postman with Newman & Jenkins
Postman with Newman & Jenkins
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback