Parameterization in Rest Assured API Automation by using the Examples keyword.

30 / Aug / 2024 by Ankit Shrivastava 0 comments

Introduction:

This blog describes the strategy to parametrize your API Automation Code using the Examples keyword in feature files to optimize your script/code.

In API testing, automating the complete functional testing is a very common practice used in the industry. There are many tools/libraries available in the market by using them we can achieve complete end-to-end automation. But when you are working on any huge project then you need to run your test script with a large number of test data. In this scenario, we need to parametrize the test data to ensure end-to-end execution and manage the test data in a single place.

Also, if we implement the parametrization in the test data, it eventually optimizes the code and gives better code readability.

What you will learn from this blog:
Way to optimize your automation code using the Examples keyword in the Feature File.

Tech Stack used in this blog:
Here in this blog tech stack used is JAVA, Rest Assured, BDD, and Cucumber. For the demo, I am using one open available API “http://postalpincode.in/api/pincode/”.

Let’s get started:
In real-time when we write our automation scripts to verify the API with multiple test data, one common way is to write the Scenarios with all the possible test data.
Let’s consider the example below to understand this.

API – http://postalpincode.in/api/pincode/110016 This endpoint takes any Indian ZIP Code as input and returns all the Post office lists of that area. So here we are writing theAutomation Script for three different test cases which are listed below.

Case 1: Any valid Zip Code of India.    Eg. 110016
Case 2: Any invalid Zip Code of India. Eg. 111100
Case 3: Valid Zip Code of Australia.     Eg. 4215

If we follow the normal way of writing the test cases in the Feature File then we need to write the three scenarios with all different test data sets. Refer to the screenshot below for the same.

By looking at the above three test cases we can say the code coverage is done but we have to write the actual test script in (Step Definition) three times for all the inputs. This increases the time complexity and repetition of the code and also Feature File looks so bulky. We can overcome this situation by optimizing our Feature File and Implementing the Examples keyword. Refer to the below screenshot for the optimized Feature File.

In the above feature file, all three test cases are covered in one Scenario, likewise, we can add N number of test cases in the one scenario and optimize our code.

Actual Test Script (Step Definition):

After creating the feature File we need to write the test script in the same manner so that it can map all the cases defined in the feature file. Please find the Test Script (Step Definition) below.

public class postOffice_API_TestScript 

{

RequestSpecification request = RestAssured.given();

JSONObject objjson;

Response response ;

String endPoint = "http://postalpincode.in/api/pincode/";

@Given("User calls the API with the Zip Code as {string},{string}")

public void user_calls_the_api_with_the_zip_code_as(String zipcode, String caseType )

{

switch (caseType) 

{

case  "Valid Indian ZipCode" :

String API_URL =  endPoint+zipcode;

response = request.get(API_URL);

break;

    case  "Invalid Indian ZipCode" :

    String api_URL =  endPoint+zipcode;

    response = request.get(api_URL);

    break;

    case  "Australia ZipCode" :

    String Api_URL =  endPoint+zipcode;

    response = request.get(Api_URL);

   break;

}

}

@Then("In the API response the Status is {string},{string}")

public void in_the_api_response_the_status_is(String ResponseStatus, String caseType) 

{    

switch (caseType) 

{

  case  "Valid Indian ZipCode" :

  String API_Status  = response.jsonPath().get("Status");

      assertEquals(API_Status,ResponseStatus );   

       break;

      case  "Invalid Indian ZipCode" :

     String API_status  = response.jsonPath().get("Status");

     assertEquals(API_status,ResponseStatus );  

     break;

   case  "Australia ZipCode" :

     String api_Status  = response.jsonPath().get("Status");

     assertEquals(api_Status,ResponseStatus );   

     break;   

}

}

}

Conclusion:

The blog’s approach illustrates that implementing the parametrization helps us to optimize our test scripts. Initially without using the Examples we used to write the scenarios and corresponding step definitions for each set of test data but after implementing the Examples we achieved it in one Scenario for all sets of test data.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *