API automation with POSTMAN
1 What is API Automation?
API testing is a software testing type to validate Application Programming Interfaces (APIs). The main purpose of this is to test functionality, reliability, performance, usability and security of the APIs. In here, software sends calls to the API, receives the API’s output and make note of the system’s response. Unlike GUI tests, API testing does not concentrate of look and feel, instead it focuses on the business logic layer of the software architecture.
The utmost goal of QA automation is to reduce the effort required for re-testing a produce manually since for Agile practices like continuous builds, the time taken to get QA team’s go after a manual regression test each time the development team gives a new build tends to be so high. As a result, API testing comes into place. API testing bypass the user interface and it tends to be quicker and way more maintainable than performing manual GUI test.
Advantages of API automation are as follows;
- Access without UI
QA testers can run API tests without needing to experience the software application.
2. Test for core functionality
Enhances build strength by an early evaluation before running GUI tests exposes small errors early, which can be interpreted as large errors after GUI testing.
3. Time effective
API testing is way more less time consuming than GUI testing.
4. Language independent
Since API tests exchange data with XML or JSON, user can select any core language for automating the application.
5. Easy integration with GUI
API automation makes integrable tests highly possible (E.g.: creation of new users in the application before starting the GUI test).
2 How to choose an API automation tool?
Selecting an API automation tool is a major and one of the most difficult steps in API automation. There are several key points that we need to consider when selecting the best API automation tool for our project.
1. Basic API Requirements : Does the tool support majority of the HTTP requests.
2. Complexity/ Team knowledge : Team skillset
3. CI/CD integration : Does the tool support CI tools like Jenkins, Bitbucket
4. Interoperability : Can the tool be connected to communication platforms like slack, PM tools
5. Non: technical friendly : Does the tool support BDD (Behavior Driven Development) conventions.
6. Pricing : Is the tool free, open source or priced.
7. Maximum team member count that the tool can be shared with.
8. Supported scripting languages.
9. Platform dependency.
10. Test debugging.
11. Test reporting.
12. Large scale project suitability.
13. Tutorial availability.
3 How Postman comes into play?
Postman is for interacting with HTTP APIs. Postman allows users to automate test scripts with salient features like write test cases, build requests that can contain dynamic parameters, pass data between requests and many more.
The main benefit of using postman can be considered as user not having to create a full JSON request programmatically to put asserts on it, unlike other API automation frameworks. Instead postman has pre-defined test cases for the user to use.
Pros of using Postman
1. User friendliness : Testers can quickly create test suits by quickly filling the sample templates given by postman.
2. Accessibility : Users can access their files anytime anywhere by logging into postman app or using browser extension.
3. Automation testing : Tests can be run multiple times by using collection runner or Newman.
4. Environments : Can use same collection to run in multiple environments to reduce repetition.
5. Debugging : Postman console assists in identifying the data received in order to make debugging easier.
6. Continuous integration.
7. Collaboration : Collections and environments can be accessed anytime anywhere by importing and exporting them or sharing a direct link to access those.
8. Supports all possible HTTP methods, saving progress, converting APIs to code.
9. Request tracking capabilities : Postman supports several status codes for HTTP requests (successful requests, bad request, empty request, unauthorized access, etc.).
Cons of using Postman
1. Limited testing area : Although postman is good for RESTful API tests, it’s not designed for SOAP or other API tests.
2. Script usability is low.
3. Limited automation capabilities and highly recommended for manual API testing.
Now that we have a solid theoretical background about API testing and postman, Let’s get started with setting up postman.
4 Let’s download and install Postman
Postman has it’s own desktop application, their chrome extension as well as it can be used through their website. Let’s see how we can use both options.
Postman desktop application
Step 1 : Download Postman
i. Navigate to https://www.postman.com/downloads/
ii. Select the desired OS
iii. Hit download
Step 2 : Run exe file
Once the setup is downloaded, you can;
i. Click on browser menu
ii. Downloads
iii. Click ‘Show in folder’
iv. Double click on the postman exe file.
Or
You can simple double click on the downloaded exe file preview shown below the browser.
Step 3 : Postman installation begins
i. Give it few minutes to install postman on to your computer.
ii. Once the installation is completed, you are redirected to postman scratch pad.
Step 4: Sign up
This step is mandatory hence signing up will ensure that your collections are saved and can be accessed for future use as well.
i. Click ‘Sign up for free’ — This redirects you to postman website
ii. Fill email, username and password fields OR use your google account to sign up
Step 5: Fill start up questions
i. Open the postman app
ii. Fill start up questions
iii. Skip inviting team members for now
iv. Navigate to the postman home
Postman website
Step 1 : Go to https://www.postman.com/
Step 2 : Click ‘Sign up for free’
Step 3 : Follow step 4 and step 5 of ‘Postman desktop application’ installation process.
Postman chrome extension
Step 1: Go to https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en from chrome browser
Step 2 : Click ‘Add to chrome’
Step 3 : After postman is installed, you will be redirected to installed apps page. You can select ‘Postman’ from there
Step 4 : Now follow step 4 and step 5 of ‘Postman desktop application’ installation process.
We have now successfully installed Postman! Now let’s see how to use different postman features and how to execute APIs with Postman.
5 Let’s explore!
How to switch to/ create a workspace
We are currently in our Postman Home. Let’s go to our workspace by;
i. Clicking on ‘Workspaces’ menu on the top of the page
ii. Select your default workspace
Or
You can even create your own workspace by ;
i. Clicking on ‘Workspaces’ menu on the top of the page
ii. Click on ‘Create workspace’
iii. Enter workspace name eg: ”My workspace”, a brief description about the workspace
iv. Finally select visibility (Personal, Private, Public, Team)
v. Hit ‘Create Workspace and Team’ and you will see that you are inside the created workspace
Workspace landing screen components
Now let’s identify what features and components we can find in the workspace landing screen.
1. Collections : A way to organize test suites. Each collection may have sub folders, multiple requests.
2. APIs : APIs define related collections and environments under a consistent schema
3. Environments : An environment is a set of variables that allows you to switch the context of your requests.
4. Mock Servers : Mock servers let you simulate endpoints and their corresponding responses in a collection without actually setting up a back end.
5. Monitors : A monitor lets you run a collection periodically to check for its performance and response.
6. Flows : Flows help you create API workflows by connecting series of requests through a drag-and-drop UI.
7. History : Shows past requests that a user has sent.
8. New : This is where you will create a new request, collection or environment.
9. Import : Used to import collections or environments.
10. Create collection : Short cut to create a new collection.
11. Request tab : Shows the title of the request that user is working on.
12. HTTP Request : This is where to choose the type of the request that user is creating GET, POST, DELETE , etc.
13. Request URL : Also known as an endpoint, this is where you will identify the link to where the API will communicate with.
14. Other functionalities;
- Params : Consists of parameters needed for a request such as key values.
- Authorization : Consists of username and password, bearer token, etc. in order to access APIs.
- Headers : Consists of headers such as content type JSON depending on the needs.
- Body : This is where one can customize details in a request commonly used in POST request.
- Pre-request Script : These are scripts that will be executed before the request. Usually, pre-request scripts for the setting environment are used to ensure that tests will be run in the correct environment.
- Tests : These are scripts executed during the request. It is important to have tests as it sets up checkpoints to verify if response status is ok, retrieved data is as expected and other tests.
6 Let’s try out our first GET and POST requests
GET request
GET requests are used in retrieving data from an end point. We won’t do any change to the end point since we are only retrieving data.
Let’s use https://jsonplaceholder.typicode.com/users this URL for our demo in this example.
Step 1 : Click on ‘+’ icon to get a new tab
Step 2 : Set request type to ‘GET’
Step 3 : Paste https://jsonplaceholder.typicode.com/users in the request URL field
Step 4 : Click ‘Send’
Output : You will see status as ‘200 ok’ and will see request data results in the response body section.
POST request
Unlike GET requests that are used to retrieve data, POST requests are used to feed data into the end point. One example is adding a user to the database. Let’s see how we do that,
Let’s use https://jsonplaceholder.typicode.com/users the same URL we used for GET request, for our demo in this example as well.
Step 1 : Click on ‘+’ icon to get a new tab
Step 2 : Set request type to ‘POST’
Step 3 : Paste https://jsonplaceholder.typicode.com/users in the request URL field
Step 4 : Switch to the ‘Body’ tab
Step 5 : Select ‘raw’
Step 6 : Change ‘Text’ to ‘Json’
Step 7 : In the ‘Body tab’ copy and paste the following json object.
Important : You should have proper json formatting here to add the record. You can use https://jsonformatter.curiousconcept.com/ if you prefer to confirm the formatting.
Since the DB already has 10 records ( as you saw from GET request results), the next in line ID is 11, hence we are adding id as 11 and you may fill other details as you may desire.
You should see the request data like below at the end of step 7;
Step 8 : Now click ‘Send’
Output : You will see status as ‘201 created’ [ 201 code : a request was successful and as a result, a resource has been created) and will see the created data results in the response body section.
Postman API execution errors
You may encounter errors as follows in postman;
- 200: This code is used for a successful request.
- 201: For a successful request and data was created.
- 204: For empty response.
- 400: This is used for Bad Request. If you enter something wrong or you missed some required parameters, then the request would not be understood by the server, and you will get 400 status code.
- 401: This is used for Unauthorized Access. If the request authentication failed or the user does not have permissions for the requested operations, then you will get a 401 status code.
- 403: This is for Forbidden or Access Denied.
- 404: This will come if the Data Not Found.
- 405: This will come if the method not allowed or if the requested method is not supported.
- 500: This code is used for Internal Server Error.
- 503: And this code is used for Service Unavailable.
Followings are some of the occasions where above errors might be triggered;
- You have entered request URL wrong.
- The request URL you entered is not valid.
- You have sent a POST request without body data.
- You are trying to add a user with same ID as a user who is already in the DB.
- You have entered invalid json format for body data.
7 What is parameterization of requests
Data parameterization can be considered as one of the most powerful features of Postman. In occasions where user creates same request with different data, user can create variables with parameters instead, thus reducing repetition and helps in iteration during automation testing.
Environment variables
When we are working in a real-life project, we have development, QA and production environments that we need to execute API test cases on. Instead of us having to duplicate same requests to test in different environments, we can user the same request data and simply change the environment in order to run API test cases in all three environments. These variables can only be used within it’s environment.
Let’s see how we can create an environment;
Step 1 : Click on Eye icon
Step 2 : Click on ‘Add’ button under Environment title
Step 3 : Provide the environment name as ‘Prod
Step 4 : Provide variable name as ‘Production’ and initial value as ‘https://jsonplaceholder.typicode.com’
Step 5 : Click ‘save’
Step 6 : Select ‘Prod’ environment from environment dropdown
Step 7 : Go to your ‘GET’ request and replace ‘https://jsonplaceholder.typicode.com’ part of the url with {{Production}}
Note: Parameters are case sensitive. You have to user exact case you put in variable name to your request URL field as well.
Step 8 : Hit ‘Send’
Output : You will see status as ‘200 ok’ and will see request data results in the response body section.
Global variables
The difference between environment variable and global variable is, that the global variable can be used in all environments within your script.
Let’s make the https://jsonplaceholder.typicode.com URL a global variable;
Step 1 : Click the Eye icon.
Step 2 : Click ‘Add’ to create a Global variable, that can be used in all environments.
Step 3 : Set variable name as ‘URL’
Step 4 : Leave Type as ‘Default’
Step 5 : Set Initial value as ‘https://jsonplaceholder.typicode.com’
Step 6 : Current value will be auto set to ‘https://jsonplaceholder.typicode.com’
Step 7 : Click ‘Save’
Step 8 : Go to your ‘GET’ request and replace ‘https://jsonplaceholder.typicode.com’ part of the url with {{URL}}
Note: Parameters are case sensitive. You have to user exact case you put in variable name to your request URL field as well.
Step 9 : Hit ‘Send’
Output : You will see status as ‘200 ok’ and will see request data results in the response body section.
8 All about postman collections
Collections play a major role in organizing postman API projects. Collections can be created, imported or even exported and shared with team members.
Creating a collection
Step 1 : Click on the ‘New’ button of top left corner of the page
Step 2 : Select ‘Collection’ from the next page
Step 3 : Add a name for the collection and you will see the collection is created on the left of the page
Step 4 : Go to the previous ‘GET’ request we created and click save
Step 5 : Choose the newly created collection ‘Postman Demo’ and save it
Output : You will see the GET request is under the newly created collection.
Exporting a collection
Step 1 : Click the ellipse icon of the collection
Step 2 : Click ‘Export’, use recommended json file format for exporting and hit ‘Export’ button again.
Step 3 : Select a file save location and click save
Sharing a collection
Step 1 : Click the ellipse icon of the collection
Step 2 : Click ‘Share’
Step 3 : Share with people by entering their emails, visa run in postman or visa json link
Step 4 : Click ‘Share collection’
Importing a collection
Step 1 : Click File -> Import
Step 2 : There are many import options as import from file, raw text, link, code repository, API gateway. Choose any method you prefer and import the collection successfully.
9 Let’s create postman tests!
Postman tests are javaScript codes which are used in order to verify request status and to compare expected results.
Let’s do few assertions for the GET request we have already made.
Test scenario 1
We’ll check if request status is 200 here in this example.
Step 1 : Go to ‘GET’ request and switch to ‘Test’ tab
Step 2 : From ‘Snippets’ section, click on ‘Status code: Code is 200” — you will see the code will be auto populated.
Step 3: Click ‘Send’
Output : You will see ‘Status code is 200’ message in ‘Test Results’ tab.
Test scenario 2
We’ll check if the 2nd user in our json object array has a username as “Antonette”, which happens to be the 2nd property of the json object.
Here we are dealing with nested json objects. Example is as belows;
Step 1 : Go to ‘GET’ request and switch to ‘Test’ tab
Step 2 : From ‘Snippets’ section, click on ‘Response body: JSON value check”- you will see the code will be auto populated.
Step 3: Let’s replace “Your Test Name” with “Check if username of user id 2 is Antonette” for better understanding of the result sentence.
Step 4 : Replace jsonData.value with “jsonData[1]. Username”, since we are accessing the second object of the array.
Step 5 : Replace ‘100’ with “Antonette”
Step 6 : Click ‘Send’
Output : You will see ‘Status code is 200’ message in ‘Test Results’ tab and “Check if username of user id 2 is Antonette” as pass results.
10 Running collections with Collection Runner or Newman?
Difference between Collection Runner and Newman
Postman collection runner is a great tool to run all requests inside a collection locally in the postman app multiple times. Collection runner uses GUI. This also generate reports so we can measure the performance of our API tests.
Whereas, Newman is a command line collection runner for postman, which allows us to directly run the collection multiple times by the command line. In addition to that, Newman can be integrated with continuous integration servers as well, hence has become to go-to collection runner for postman nowadays.
Let’s run a collection with Collection Runner!
Step 1 : Click on the ellipse icon near the collection
Step 2 : Click ‘Run collection’ from the menu
Step 3 : Change run order if you want by dragging the requests after you get ‘hand’ icon
Step 4 : Set ‘Iterations’ value to the number of times the tests should be run
Step 5 : Set ‘Delay’ value to the number of milliseconds that you wish the runner should wait before executing the next case
Step 6 : Then click ‘Run Postman Demo’ button
Step 7 : Once the test run is completed, you can view the individual test results with time, test summary of all iterations, you can either export test results or re-run the test runner.
Let’s run a collection with Newman!
Since Newman is an add on for Postman, we need to install it separately.
Step 1 : Download nodejs using https://nodejs.org/en/ link
Step 2 : Installing node.js
i. Install nodejs by double clicking on the downloaded msi file
ii. Complete passing all the remaining screens and finally hit ‘Install’
Step 3 : To confirm if we have downloaded nodejs properly, open command line and type the following command
C:\Users\tharushi.d> Node -v
Then you will see the node version if nodejs is installed properly.
Step 4 : In order to install Newman, enter the following command in the command line
C:\Users\tharushi.d> npm install -g newman
Step 5 : To confirm if we have downloaded Newman properly, open command line and type the following command
C:\Users\tharushi.d> newman --version
Then you will see the Newman version if it is installed properly.
Here’s how you should get both node.js and newman versions.
Step 6 : As we have already learnt under topic 8-Exporting a collection;
i. Go back to the Postman app
ii. Click ellipse icon near the postman collection
iii. Click ‘Export’
iv. Keep the recommended file format
v. Select the desired file location
vi. Click ‘Save’
Step 7 : Now follow the below steps to export the environment as well. Otherwise you will not be able to run newman collection since you have used environment variables in request URL;
i. Go to “Environments” tab from the left corner of the screen
ii. Select the environment you wish to export. Note: This should be the environment that you have used the variables in request files
iii. Click on the ellipse icon on the top right corner of the screen
iv. Select ‘Export’
v. Make sure to choose the same file location you choose to save the collection here also
vi. Click ‘Save’
Step 8 : Now postman collection and environment exports should be in the same folder
Step 9 : Now go to the command line and change the directory to the location where our collection and environment files are saved.
In my case, I have the filed saved in “ C:\Users\tharushi.d\Documents\Newman Collection Runner Demo” folder.
C:\Users\tharushi.d> cd C:\Users\tharushi.d\Documents\Newman Collection Runner Demo
Step 10 : Let’s run our collection using the below command;
newman run <collection name> -e <environment name>
In my case, the command is as follows;
C:\Users\tharushi.d\Documents\Newman Collection Runner Demo> newman run PostmanDemo.postman_collection.json -e workspace.postman_globals.json
Test results will be shown as below;
Newman collection runner codes
1. Run a collection only
newman run <collection name>
2. Run a collection and environment
newman run <collection name> -e <environment name>
3. Run a collection with required number of iterations
newman run <collection name> -n <no.of iterations>
4. Run a collection and environment with required number of iterations
newman run <collection name> -e <environment name> -n <no.of iterations>
5. Run with data file
newman run <collection name> — data <file name> -n <no.of iterations> -e <environment name>
6. Set delay time
newman run <collection name> -d <delay time>
And everything considered, we have come to the end of my article. In a nutshell I hope you learnt what API testing is, what Postman is, how to write handle postman requests, how to write API tests and how to run postman collections in this article.
However, this article is a kickstart for all the beginners out there, who are interested in learning postman API automation. In closing, let’s make note that there’s still lot more we can do with Postman for the API automation pathway!
References: https://learning.postman.com/docs/getting-started/introduction/, https://www.guru99.com/postman-tutorial.html