REST API automation testing using Apache HttpClient – The Approach
|After the initial introduction to REST , its time for looking into techniques and tools , which can be used for automation testing of APIs based on REST.
What can be a better option rather than this – Something that can execute various HTTP request methods (PUT/GET etc etc.) , outputs response from server irrespective of whatever format it is, a flexibility to user to capture the response, extract out the needful and release the connection!
If we consider Java as our language to make it happen , to name a such library is – Apache HttpClient
Well, there are several implementations of the request and response Content-Types(JSON, XML,HTML etc etc) , representations and all, which implements REST to design APIs. We are going to take example on – JSON request and response representations.
What are the requirements? – Tools/Libraries used
1. Apache HttpClient – A Java based library for HTTP communications
2. JSON parser for Java – we will be using org.json (It has APIs which really complements what JSON representations actually mean)
3. Eclipse
4. JUnit
Java JDK and some basic environmental set up etc are – Understood. 🙂
Basic idea behind combining these tools:
1. We will be creating instances of HttpClient for various methods like GET,PUT,POST etc. by mentioning the request URL
For example : We want to send a GET request to – https://public-api.wordpress.com/rest/v1/sites/www.qaautomationsimplified.com
It supposed to provide some information about the domain: www.qaautomationsimplified.com
So we will be creating instance of HttpGet and setting the URL as : https://public-api.wordpress.com/rest/v1/sites/www.qaautomationsimplified.com
2. We will be composing the request body as JSON
As the above example is a GET request , so it can not have a request body. In case of POST, PUT etc if we need to set the request body then that needs to be represented in JSON format according to the design if the API. So we will be extensively using org.json methods/APIs to build JSON objects and set it as HTTP request body.
3. Execute the desired HTTP method when headers etc are all set
As the request body contains request information in JSON format , so we need to set a header – Content-Type as ‘application/json’ , which gives instruction to server that request body is represented as JSON and should be parsed accordingly to process the request.
4. Capture the response and convert it to a JSON object.
Response object is captured and parsed to a JSON object.
5. Verify various Key- Value pairs along with the response code
Verify / assert the components in response – e.g ID , name and its values in the above response.
Objectives of Testing:
1. Verify Response code (200, 400 etc etc)
2. Verify Response Line (OK, SUCCESS etc etc)
3. Verify Presence of desired headers in response (application/json, image/png etc etc)
4. Verify presence of various Keys in response body
5. Verify values of Keys whenever we have expected values in hand
6. Verify if response is in JSON Array, JSON string etc etc.
Let’s have a look into how to combine these Java libraries for effective testing of REST APIs (From coding point of view) in next few blogs.
Happy Testing!!
- Bring the auto Sync magic of Protractor to Selenium with Java - October 23, 2015
- Restarting Appium Server could improve various server freezing issues and may improve execution time - January 20, 2015
- Appium with iOS 8 and XCode 6 : What’s new? - November 5, 2014
- REST API automation testing using Apache HttpClient – The Approach - October 3, 2014
- An Overview of mobile application : Moving forward to automation - October 1, 2014
- An introduction to REST - September 29, 2014
- Run ChromeDriver with Chrome Driver Service to reduce script execution time significantly - September 26, 2014
- Selenium WebDriver – Get Cookies from an existing session and add those to a newly instantiated WebDriver browser instance. - September 26, 2014
- Simulate Copy Paste action using Java Robot and Clipboard class - September 26, 2014
- Android : How to test if Android Application has memory leaks - August 11, 2014
-
Siva
-
Nikhil Shetty
-
Pritam Samantaray
-
susithra sampath
-
Girish Mohan
-
palani