Building Cucumber BDD API Testing Framework from scratch: Part 5
Now in the last part, we saw how we can drive the data dynamically. In this part, we will see how we can parameterize our tests with multiple data sets.
Whenever you build an automation test, you should test it with multiple data set to make sure if it’s working or failing.
Now we need to add one more row under the Examples keyword.
Now in general, when you want to achieve the same thing through excel then, you have to write a lot of code to scan each row and from each row, it has to pick the data and trigger the tests, but that is not the case with Cucumber.
Now if you run this test, automatically it will run two times with two different data sets. Make sure you change the ISBN and aisle information otherwise it will throw a 404 error in the console output.
Now let’s run the test with the Test Runner class
You can also see it’s giving two IDs because we passing two sets of information from the feature file.
Now, let’s see the test case details in the log file.
If you can see, our log file is displaying information for only one set of data, and it’s replacing the second set of data information.
To prevent data replacement we need to provide if condition in the Utils class where we are also logging all the information in the log file.
We also require to make the request specification variable static so that it doesn't create another instance but use the same instance throughout the class to prevent the null pointer error in the if condition.
Let’s run the test cases one more time to check if everything is logging in the log file. Make sure to change the ISBN and aisle information before you run the test cases.
Below, I have pasted all the information from the log file and the feature file image to verify if the data matches.
Request method: POSTRequest URI: http://216.10.245.166/Library/Addbook.phpProxy: <none>Request params: <none>Query params: <none>Form params: <none>Path params: <none>Headers: Accept=*/*Content-Type=application/jsonCookies: <none>Multiparts: <none>Body:{“name”: “Geography”,“isbn”: “pordf”,“aisle”: 2567,“author”: “Prem”}HTTP/1.1 200 OKDate: Sat, 31 Jul 2021 18:47:02 GMTServer: ApacheAccess-Control-Allow-Origin: *Access-Control-Allow-Methods: POSTAccess-Control-Max-Age: 3600Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-WithKeep-Alive: timeout=5, max=100Connection: Keep-AliveTransfer-Encoding: chunkedContent-Type: application/json;charset=UTF-8{“Msg”: “successfully added”,“ID”: “pordf2567”}Request method: POSTRequest URI: http://216.10.245.166/Library/Addbook.phpProxy: <none>Request params: <none>Query params: <none>Form params: <none>Path params: <none>Headers: Accept=*/*Content-Type=application/jsonCookies: <none>Multiparts: <none>Body:{“name”: “Biology”,“isbn”: “strty”,“aisle”: 2389,“author”: “Rahul”}HTTP/1.1 200 OKDate: Sat, 31 Jul 2021 18:47:03 GMTServer: ApacheAccess-Control-Allow-Origin: *Access-Control-Allow-Methods: POSTAccess-Control-Max-Age: 3600Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-WithKeep-Alive: timeout=5, max=100Connection: Keep-AliveTransfer-Encoding: chunkedContent-Type: application/json;charset=UTF-8{“Msg”: “successfully added”,“ID”: “strty2389”}
Everything is working as expected now. So, we have fixed the data replacement issue by optimizing the code in the Utils class and at the same time, we saw how to run test cases with multiple data sets.
And, Here is the link to the GitHub repository to download the code.
Thanks for reading.