{"id":61617,"date":"2024-08-30T10:44:46","date_gmt":"2024-08-30T05:14:46","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=61617"},"modified":"2024-09-02T16:26:20","modified_gmt":"2024-09-02T10:56:20","slug":"optimize-api-testing-automation-code-using-examples-in-the-feature-file","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/optimize-api-testing-automation-code-using-examples-in-the-feature-file\/","title":{"rendered":"Parameterization in Rest Assured API Automation by using the Examples keyword."},"content":{"rendered":"<h2><strong>Introduction:<\/strong><\/h2>\n<p>This blog describes the strategy to parametrize your API Automation Code using the Examples keyword in feature files to optimize your script\/code.<\/p>\n<p>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.<\/p>\n<p>Also, if we implement the parametrization in the test data, it eventually optimizes the code and gives better code readability.<\/p>\n<p><b>What you will learn from this blog:<br \/>\n<\/b><b><\/b>Way to optimize your automation code using the <i>Examples<\/i> keyword in the Feature File.<b><\/b><\/p>\n<p><b><i>Tech Stack used in this blog:<br \/>\n<\/i><\/b>Here in this blog tech stack used is JAVA, Rest Assured, BDD, and Cucumber. For the demo, I am using one open available API &#8220;http:\/\/postalpincode.in\/api\/pincode\/&#8221;.<\/p>\n<p><b><i>Let&#8217;s get started:<br \/>\n<\/i><\/b>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.<br \/>\nLet&#8217;s consider the example below to understand this.<\/p>\n<p><b>API &#8211; <\/b>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.<\/p>\n<p>Case 1: Any valid Zip Code of India.\u00a0 \u00a0 Eg. 110016<br \/>\nCase 2: Any invalid Zip Code of India. Eg. 111100<br \/>\nCase 3: Valid Zip Code of Australia. \u00a0 \u00a0 Eg. 4215<\/p>\n<p>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.<\/p>\n<p><b><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61614\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-2024-05-08-at-3.48.38-PM-1-1024x396.png\" alt=\"\" width=\"702\" height=\"272\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-2024-05-08-at-3.48.38-PM-1-1024x396.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-2024-05-08-at-3.48.38-PM-1-300x116.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-2024-05-08-at-3.48.38-PM-1-768x297.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-2024-05-08-at-3.48.38-PM-1-1536x593.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-2024-05-08-at-3.48.38-PM-1-2048x791.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/Screenshot-2024-05-08-at-3.48.38-PM-1-624x241.png 624w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/b><\/p>\n<p>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.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61616\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/05\/img-2--1024x337.png\" alt=\"\" width=\"746\" height=\"246\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/05\/img-2--1024x337.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/img-2--300x99.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/img-2--768x253.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/img-2--1536x505.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/img-2--2048x674.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/05\/img-2--624x205.png 624w\" sizes=\"(max-width: 746px) 100vw, 746px\" \/><\/p>\n<p>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.<br \/>\n<b><br \/>\n<\/b><b><i>Actual Test Script (Step Definition):<\/i><\/b><\/p>\n<p>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.<\/p>\n<pre>public class postOffice_API_TestScript\u00a0\r\n\r\n{\r\n\r\nRequestSpecification request = RestAssured.<i>given<\/i>();\r\n\r\nJSONObject objjson;\r\n\r\nResponse response ;\r\n\r\nString endPoint = \"http:\/\/postalpincode.in\/api\/pincode\/\";\r\n\r\n<i>@Given<\/i>(\"User calls the API with the Zip Code as {string},{string}\")\r\n\r\npublic void user_calls_the_api_with_the_zip_code_as(String zipcode, String caseType )\r\n\r\n{\r\n\r\nswitch (caseType)\u00a0\r\n\r\n{\r\n\r\ncase\u00a0 \"Valid Indian ZipCode\" :\r\n\r\nString API_URL =  endPoint+zipcode;\r\n\r\nresponse = request.get(API_URL);\r\n\r\nbreak;\r\n\r\n\u00a0 \u00a0 case\u00a0 \"Invalid Indian ZipCode\" :\r\n\r\n\u00a0 \u00a0 String api_URL =  endPoint+zipcode;\r\n\r\n\u00a0 \u00a0 response = request.get(api_URL);\r\n\r\n\u00a0 \u00a0 break;\r\n\r\n\u00a0 \u00a0 case\u00a0 \"Australia ZipCode\" :\r\n\r\n\u00a0 \u00a0 String Api_URL =  endPoint+zipcode;\r\n\r\n\u00a0 \u00a0 response = request.get(Api_URL);\r\n\r\n\u00a0 \u00a0break;\r\n\r\n}\r\n\r\n}\r\n\r\n<i>@Then<\/i>(\"In the API response the Status is {string},{string}\")\r\n\r\npublic void in_the_api_response_the_status_is(String ResponseStatus, String caseType)\u00a0\r\n\r\n{ \u00a0\u00a0\u00a0\r\n\r\nswitch (caseType)\u00a0\r\n\r\n{\r\n\r\n\u00a0 case\u00a0 \"Valid Indian ZipCode\" :\r\n\r\n\u00a0 String API_Status\u00a0 = response.jsonPath().get(\"Status\");\r\n\r\n\u00a0 \u00a0 \u00a0\u00a0<i>assertEquals<\/i>(API_Status,ResponseStatus ); \u00a0\u00a0\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0break;\r\n\r\n\u00a0 \u00a0 \u00a0 case\u00a0 \"Invalid Indian ZipCode\" :\r\n\r\n\u00a0\u00a0 \u00a0 String API_status\u00a0 = response.jsonPath().get(\"Status\");\r\n\r\n\u00a0 \u00a0 \u00a0<i>assertEquals<\/i>(API_status,ResponseStatus );\u00a0\u00a0\r\n\r\n\u00a0 \u00a0 \u00a0break;\r\n\r\n\u00a0\u00a0 case\u00a0 \"Australia ZipCode\" :\r\n\r\n\u00a0 \u00a0 \u00a0String api_Status\u00a0 = response.jsonPath().get(\"Status\");\r\n\r\n\u00a0 \u00a0\u00a0\u00a0<i>assertEquals<\/i>(api_Status,ResponseStatus ); \u00a0\u00a0\r\n\r\n\u00a0 \u00a0 \u00a0break; \u00a0\u00a0\r\n\r\n}\r\n\r\n}\r\n\r\n}<\/pre>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>The blog&#8217;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.<b><\/b><b><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1404,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":98},"categories":[5880],"tags":[5899,5900,5901],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61617"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1404"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=61617"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61617\/revisions"}],"predecessor-version":[{"id":65048,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61617\/revisions\/65048"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=61617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=61617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=61617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}