Did you know that Postman can be used to automate API Calls? Also, there is a brother of Postman, called Newman, which can run the same automates from the command line. Let's see this thing in action.
Please fork the complete project from my Github.
Postman Environment variables
You may want to run your automates with a different set of data, based on
region, environment etc. In this case, you can create your script to read input value from environment variables, so that these values are externalized.
It is always, good to follow a naming convention for your Collection ENV. Else, it becomes a big mess.
I prefer, the below naming
It is better to group APIs into folders. I have a Project Collection and subfolder for each group of APIs.
Order the requests in this order,
CREATE → FETCH → UPDATE → FETCH (optional) → DELETE
We need to make the requests generic, so that, it can work with different env.
If you see, the baseURL is an environment variable. It can change between QA, DEV, PROD etc.
Also, the JSON payload is receiving data from environment variables.
There are scenarios when you need to send UUID from the front-end. In this case, you use
$guidin the BODY. Please find the usage below
In the previous scenario, we saw Create Article Request. We need to validate article creation when the script executes. Also, we need to use the same
article_id in the subsequent requests (UPDATE, GET, DELETE).
So we write Post Execution Actions.
Here, you can see
- We are copying
- Validating the status code returned
- Printing the
article_id, in the output. (This is very useful when you are running the script from cmd)
pm stands for Postman, which is a global object. It is similar to
windowobject in browser.
In the subsequent request, we can use the
article_id as shown below.
Postman Collection Runner
Now comes the execution part. You can execute the collection using collection runner.
There are scenarios, where you don’t want to perform particular requests, say DELETE (since it will be used in subsequent calls). In that case, you can untick those requests.
The execution result looks something like the below:
You can see, the success and failure in the end report.
You can export your collection and environment.
Newman (The CLI runner)
There are scenarios when you need to run this API scripts on a Linux instance. In my case, I wanted to run these scripts in a Linux Bastion machine.
For me, the best option was to use Newman.
The above commands install Newman in your Linux machine.
Now copy the collection and environment backup into Linux instance. You can use FileZilla for the same if you plan to run it on EC2.
You can execute the automate using the below command.
-e means, use the environment file that follows.
You can see the newman execution happening the cmd.
The result would look something like below
As discussed earlier, we can print id of the resource created in the output for debugging purpose.
Hurray, you have learned API automation using Postman and Newman.
Found it Interesting?
Please show your support by 👏.