Automate Tests Using Newman (EN)

Cihan Eroğlu
Cihan Eroğlu | Blog
4 min readSep 16, 2022

Newman is an NPM (Node Package Manager) technology that allows us to run our Collections in Postman through the command line (without using Postman) and not through the Collection Runner.

Newman has three different uses. These are:

  1. Link Sharing
  2. File Export
  3. Postman APIs

In Link Sharing method, If you change the Collection, or requests, the current link will not provide you with this change. To do this, you must create a new link through Postman for each change. The same applies to the File Export method. When you change the Collection, you must export again in Postman.

The Postman API method is different. However, we run tests using Trello’s APIs to learn about Postman; Postman has APIs that you can access and have a KEY that Postman created specifically for you. We can run our tests by accessing these APIs so that no further action is required via the command line, even if we make a change through Postman.

Now let’s see how this is done.

For Windows:

1. Open the Node.js page to download Node.js (Node.js for windows) for windows. https://nodejs.org/en/download/

2. Run the downloaded application to grant the necessary permissions and complete the installation.

3. After installation, open the windows command terminal and type: Node-v, and make sure the application is installed. (After this command, the node.js version should appear.)

4. Restart your computer after installation.

5. Then, run the npm install npm -- global command on the command line to update the NPM.

For macOS:

1. Download the installation file from https://nodejs.org/en/

2. Click on the .dmg file to complete the installation.

3. Make sure the installation is complete with the node-v command over the terminal.

We have now completed NodeJS and Npm installations. We’re going to set up Newman now. This step applies to both operating systems:

· We are installing Newman by sending the command “npm install — g Newman” via the command line.

We’ve completed the Newman installation. Now it’s time to create the tests we can run at Newman in Postman. You can create a Collection and Requests, such as at this. I’ll use my Trello API Collection again.

It’s time to create a new Collection with Postman APIs in Postman

1. We set our Collection name as the Postman API by clicking the “+” sign next to Collection.

2. We log in using our information at http://www.postman.co. We click Settings from the menu on the right.

3. We click to ‘Generate API Key’ in API keys area, and our API Key is created.

4. First, we create a new request to bring all our collections (called Get All Collections) and run the link “https://api.getpostman.com/collections?apikey=$apiKey" USING THE GET method. (We paste the API KEY that we copy instead of $apiKey.) We find the Collection Trello API and get the code written against the uid from the Response.

5. Now we’ve taken all the Collections with THE GET method, and we’ve got the uid of the Collection we want to run from. By pasting this UID into $uid on link https://api.getpostman.com/collections/$uid?apikey=$apiKey and entering our Key, we have obtained the first link we will use in Newman.

6. Newman can’t access your variables unless you give him a link to your variables. We will create a new Request (called Get Environments) for this. By placing our API Key on the link https://api.getpostman.com/environments?apikey=$apiKey, we will access our local variables (environments). We conduct our inquiry with THE GET method and copy the uid of the relevant environment.

7. Finally, We edit our link in step six in this format:

…environments/{{uid}}?apikey=…

(The code we copied in step six will be pasted here: {{uid}})

Now we have our second link.

We’re ready now. For the run our test in Newman, We write this on the command line newman run “first link” — environment “second link”

And we run the command by pressing Enter.

Our test is succesfully completed. See you when I will write my next post.

--

--