For typical API testing requirements, you might only need to validate an API behavior with few payload data combination. Nowadays, the applications are evolving to offer dynamic configuration options and flexibility to the end-user. This also means that it could be a concern for the testing team to cover all critical combinations and ensure that the application quality is not compromised.
In one of our recent projects, there was a requirement to seed more than 10,000 data to five different modules. The typical solution is to automate these data addition scenarios from the UI for all modules. However, this is very time-consuming since you have to inspect every element on each screen and then need to script the modules. Moreover, since you are interacting with the GUI to create the data, there is some inevitable delay as well. Another approach would be to inject the data directly in the database, but this would not be feasible in every scenario since data corruption in the backend could cause a setback in all planned activities including UAT testing and production release.
Is there an alternative solution for this? The answer is YES!!
Since API calls are pretty lightweight and straight forward, it doesn’t inherit the GUI interaction flaws like lag, loading issues etc. You can seed the required test data through API with minimal effort and less time consumption, and achieve highly accurate and reliable test data.
In API testing, you usually verify and validate the APIs without any modification by using hard coded request. However, in some cases which require you to pass different values, you may have to modify the request parameters. For data seeding, you will parameterize the request body in order to iterate the process and insert thousands of data with the same API call. You can parameterize the request body with the help of API interaction libraries. One such library for Java is Rest-Assured. Rest-Assured is a Java-based library that is used to test RESTful Web Services. Rest-Assured library also provides the ability to validate the HTTP Responses received from the server.
Why API Testing
As we all know APIs are responsible for allowing applications to interact and communicate with other applications. API testing is less time consuming than functional and validation testing. API test automation requires less code than GUI automated tests, thus providing faster test results and better test coverage. Also, maintaining UI tests is even more cumbersome. Since the code base is many times bigger than API level tests, it becomes very expensive to maintain too.
Steps for Analyzing and Automating API Calls
- In Chrome browser, open chrome developer tools (F12) and go to Network tab.
- Clear existing logs, enable recording and perform the required actions in browser.
- Check the corresponding docs loaded.
- Identify the API and identify the elements needed to automate.
- Extract each element from the request body and parameterize it with Rest Assured.
- Once the Element Extraction and Parameterizing is successful, send the request to endpoint.
- Verify that the endpoint returns a success or a 200 OK as the HTTP response or any success indicating response.
Rest assured is an API which is designed for automating REST services/APIs. It is a Java library that provides a domain-specific language for writing powerful, maintainable tests for RESTful APIs. It is available in the form of jar file and it contains plenty classes and methods by which you can automate our APIs. You can send multiple type of requests and the response from the API. You can do different type of validations on the response.
Pre-requisites for configuring RestAssured library are:
Parameterization is very important for automation testing. It helps you to iterate on input data with multiple data sets that make your scripts reusable and maintainable. In few scenarios, you can still manage with hard coded request but the same approach will not work out where sheer count of combinations is to be validated. By implementing the right solution, you can keep your code base and test data size at ideal range and still savor the benefits of optimal coverage.
Hope this blog helped you to understand the concept of parameterization and the actual implementation in a better way.
To learn about the steps for configuring Rest Assured, please click here
Mary Geethu C.A.
Senior Software Test Engineer, RapidValue