Building End-to-End API Testing Framework: Part 7.2
In this part, we will add one final scenario using the DELETE method, where we will delete the information which we have added to the server using the POST call method in the first part i.e. 7.1 of this two-part end-to-end series and, that will conclude this end-to-end series.
We need to create one more scenario where we are going to call the Delete API string from the enum class. If you don’t know about enum class, then do check out the article linked above.
Every scenario is treated like one test case, so that means we are creating another test case.
Now, we need to provide the Given, When and then as we have provided in the first scenario
In Given, we need to provide the Delete method payload. which is book ID as you can see in the above image
In When, we need to call the Delete book API with the POST HTTP request
Make sure the Delete method name matches the name defined in the enum class.
In then, once we call the API, we will now verify the response status code. Which should be 200
In the And step, we can also verify the message in the response body which as you can see in the Postman window, is “book is successfully deleted”
Now, we only need to create the Delete request payload using request specification because everything else we have already created in the first scenario.
Now, let’s generate the step definition code by running the Test Runner file to get the equivalent code
Now, copy-paste the generated code into the step definition class
First, provide the necessary headers by calling the request specification method
Now, let’s create the Delete payload as we did for Add Book payload
We are passing one parameter called book id because we want to drive it from the POST call response body
And, we also need to call the Delete book payload into the Step Definition class
We need to push all the details to one variable called “req” to perform further operation
So, we have successfully created the payload for the Delete method and stored it in a variable.
Now, for other steps such as When, Then, and And we don’t need to create any separate methods because the framework will use the existing methods which we have created in the POST method, and it will map/tag the methods with the matching statements.
That’s called code reusability 😉
Also, you need the make the response and bookID variable static so, they get updated throughout the class and it also prevents the null pointer error, In case you get one.
For the last time, let’s change the value of ISBN and aisle before we run the tests.
Remember, you can give any name or information you want.
Now, let’s run the tests to see if our end-to-end framework is working as expected or not.
And that’s a wrap for the end-to-end testing framework.
Here is the link to the GitHub repository to download the code.
Salud 🍻