{"id":1389,"date":"2021-08-09T12:49:20","date_gmt":"2021-08-09T12:49:20","guid":{"rendered":"https:\/\/staging.hubino.com\/newblog\/?p=1389"},"modified":"2023-02-01T09:04:43","modified_gmt":"2023-02-01T09:04:43","slug":"api-testing-using-karate","status":"publish","type":"post","link":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/","title":{"rendered":"API Testing Using KARATE"},"content":{"rendered":"\n<p><strong>About Karate:<\/strong><\/p>\n\n\n\n<p>Karate is an open source library used to test the web services based on the plain English in the text file such as feature file. You could automate the web service testing based on the below highlighted points associated with Karate;<\/p>\n\n\n\n<ul><li>Support both SOAP and REST<\/li><li>Schema validation<\/li><li>Assert the response<\/li><li>Response data chaining to next request<\/li><li>Data elements comparison (Support both JSON and XML)<\/li><li>Data driven approach using .js file<\/li><li>Test Report generation<\/li><\/ul>\n\n\n\n<p>Please refer the long list of supported feature sets using the below URL;<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/intuit\/karate\">https:\/\/github.com\/intuit\/karate<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/tinyurl.com\/karatera\">http:\/\/tinyurl.com\/karatera<\/a>&nbsp;(REST Assured vs Karate)<\/p>\n\n\n\n<p><strong>Karate Setup:<\/strong><\/p>\n\n\n\n<ol><li>Create a maven specific project in your Eclipse<\/li><li>Please use the below dependencies to download the Karate library in your Eclipse IDE;<\/li><\/ol>\n\n\n\n<p>&lt;dependency&gt;<\/p>\n\n\n\n<p>&lt;groupId&gt;com.intuit.karate&lt;\/groupId&gt;<\/p>\n\n\n\n<p>&lt;artifactId&gt;karate-apache&lt;\/artifactId&gt;<\/p>\n\n\n\n<p>&lt;version&gt;0.7.0&lt;\/version&gt;<\/p>\n\n\n\n<p>&lt;scope&gt;test&lt;\/scope&gt;<\/p>\n\n\n\n<p>&lt;\/dependency&gt;<\/p>\n\n\n\n<p>&lt;dependency&gt;<\/p>\n\n\n\n<p>&lt;groupId&gt;com.intuit.karate&lt;\/groupId&gt;<\/p>\n\n\n\n<p>&lt;artifactId&gt;karate-junit4&lt;\/artifactId&gt;<\/p>\n\n\n\n<p>&lt;version&gt;0.7.0&lt;\/version&gt;<\/p>\n\n\n\n<p>&lt;scope&gt;test&lt;\/scope&gt;<\/p>\n\n\n\n<p>&lt;\/dependency&gt;<\/p>\n\n\n\n<p><strong>How to use Karate:<\/strong><\/p>\n\n\n\n<p><strong>Create a Runner file (Java class file) based on the below details;<\/strong><\/p>\n\n\n\n<p>package&nbsp;karate.test;<\/p>\n\n\n\n<p>import com.intuit.karate.junit4.Karate;<\/p>\n\n\n\n<p>import&nbsp;cucumber.api.CucumberOptions;<\/p>\n\n\n\n<p>import&nbsp;org.junit.runner.RunWith;<\/p>\n\n\n\n<p>@RunWith(Karate.class)<\/p>\n\n\n\n<p>@CucumberOptions(features = \u201cFeatures\/MockTest.feature\u201d)<\/p>\n\n\n\n<p>public class Runner {<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Create a feature file (Example: Test.feature) based on the below details;<\/strong><\/p>\n\n\n\n<p>Feature: REST API testing using Karate with Mockoon<\/p>\n\n\n\n<p>Background:<\/p>\n\n\n\n<p>* url endpoint<\/p>\n\n\n\n<p>Scenario: Test REST request and assert the response<\/p>\n\n\n\n<p>Given path queryName<\/p>\n\n\n\n<p>When method GET<\/p>\n\n\n\n<p>Then status 200<\/p>\n\n\n\n<p>And print response<\/p>\n\n\n\n<p>And match response contains {name:\u2019Gopi\u2019}<\/p>\n\n\n\n<p>And def accountNumber = response.multipleaccounttype[0].savings<\/p>\n\n\n\n<p>And print accountNumber<\/p>\n\n\n\n<p><strong>Calling the data such as URL and path name from&nbsp;karate-config.js&nbsp;file as mentioned below; This file should be added in the project src location;<\/strong><\/p>\n\n\n\n<p>function () {<\/p>\n\n\n\n<p>var port = 3000;<\/p>\n\n\n\n<p>var baseUrl = &#8216;<a href=\"https:\/\/staging.hubino.com\/resources\/\">https:\/\/staging.hubino.com\/resources<\/a>&#8216;<\/p>\n\n\n\n<p>var config = {<\/p>\n\n\n\n<p>endpoint : baseUrl + \u2018:\u2019 + port + \u2018\/accountList\u2019,<\/p>\n\n\n\n<p>queryName: \u2018\/name\u2019,<\/p>\n\n\n\n<p>};<\/p>\n\n\n\n<p>karate.configure(\u2018connectTimeout\u2019, 30000);<\/p>\n\n\n\n<p>karate.configure(\u2018readTimeout\u2019, 60000);<\/p>\n\n\n\n<p>return config;<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>About REST Testing using Karate based on the above code snippet:<\/strong><\/p>\n\n\n\n<ul><li>Calling the URL from&nbsp;karate-config.js&nbsp;file using the syntax of \u2018* url endpoint\u2019<\/li><li>Pass the path name in the \u2018Given\u2019 condition<\/li><li>Trigger the GET method using \u2018When method GET\u2019<\/li><li>Assert the status as \u2018200\u2019 in the \u2018Then\u2019 condition<\/li><li>Print the response received from the web server<\/li><li>Assert the response contains the name is Gopi in the \u2018And\u2019 condition<\/li><li>Assign a variable using def and getting the account number using \u2018And\u2019 condition<\/li><li>Print the fetched account number in the response<\/li><\/ul>\n\n\n\n<p><strong>&nbsp;About&nbsp;karate-config.js&nbsp;file:<\/strong><\/p>\n\n\n\n<ul><li>Declare the URL details in a variable to reuse in another feature files<\/li><li>Declare the path name in queryName variable to reuse in feature file<\/li><li>Return the values<\/li><\/ul>\n\n\n\n<p>After running the runner file(Runner.java), Karate will start executing the feature file based on the step by step detailed information and publish the test results in HTML format as mentioned below;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"999\" height=\"532\" src=\"https:\/\/staging.hubino.com\/newblog\/wp-content\/uploads\/2021\/08\/karate_1.jpg\" alt=\"\" class=\"wp-image-1391\" srcset=\"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_1.jpg 999w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_1-300x160.jpg 300w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_1-768x409.jpg 768w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_1-585x312.jpg 585w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"999\" height=\"503\" src=\"https:\/\/staging.hubino.com\/newblog\/wp-content\/uploads\/2021\/08\/karate_3.jpg\" alt=\"\" class=\"wp-image-1392\" srcset=\"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_3.jpg 999w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_3-300x151.jpg 300w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_3-768x387.jpg 768w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_3-585x295.jpg 585w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/figure>\n\n\n\n<p>I\u2019ve used mockoon to mock the REST API using the below details;<\/p>\n\n\n\n<p><a href=\"https:\/\/mockoon.com\">https:\/\/mockoon.com<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"999\" height=\"532\" src=\"https:\/\/staging.hubino.com\/newblog\/wp-content\/uploads\/2021\/08\/karate_2.jpg\" alt=\"\" class=\"wp-image-1393\" srcset=\"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_2.jpg 999w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_2-300x160.jpg 300w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_2-768x409.jpg 768w, https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/karate_2-585x312.jpg 585w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/figure>\n\n\n\n<p>Thank you!!!<\/p>\n\n\n\n<p>Image Credit \u2013 https:\/\/www.lifehacker.com.au<\/p>\n","protected":false},"excerpt":{"rendered":"<p>About Karate: Karate is an open source library used to test the web services&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1839,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[26],"tags":[30],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>API Testing Using KARATE - Hubino Resources<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"API Testing Using KARATE - Hubino Resources\" \/>\n<meta property=\"og:description\" content=\"About Karate: Karate is an open source library used to test the web services&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/\" \/>\n<meta property=\"og:site_name\" content=\"Hubino Resources\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-09T12:49:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-01T09:04:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/api-testing-using-karate.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"718\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/staging.hubino.com\/resources\/#website\",\"url\":\"https:\/\/staging.hubino.com\/resources\/\",\"name\":\"Hubino Resources\",\"description\":\"Blog | Case Studies\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/staging.hubino.com\/resources\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/api-testing-using-karate.jpg\",\"contentUrl\":\"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/api-testing-using-karate.jpg\",\"width\":1200,\"height\":718},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#webpage\",\"url\":\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/\",\"name\":\"API Testing Using KARATE - Hubino Resources\",\"isPartOf\":{\"@id\":\"https:\/\/staging.hubino.com\/resources\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#primaryimage\"},\"datePublished\":\"2021-08-09T12:49:20+00:00\",\"dateModified\":\"2023-02-01T09:04:43+00:00\",\"author\":{\"@id\":\"https:\/\/staging.hubino.com\/resources\/#\/schema\/person\/2d61fd49f70401801b84564f70deee81\"},\"breadcrumb\":{\"@id\":\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/staging.hubino.com\/resources\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"API Testing Using KARATE\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/staging.hubino.com\/resources\/#\/schema\/person\/2d61fd49f70401801b84564f70deee81\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/staging.hubino.com\/resources\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d968406bfd6a4fbd587c2798bc6716de?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d968406bfd6a4fbd587c2798bc6716de?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/staging.hubino.com\/resources\/\"],\"url\":\"https:\/\/staging.hubino.com\/resources\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"API Testing Using KARATE - Hubino Resources","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/","og_locale":"en_US","og_type":"article","og_title":"API Testing Using KARATE - Hubino Resources","og_description":"About Karate: Karate is an open source library used to test the web services&hellip;","og_url":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/","og_site_name":"Hubino Resources","article_published_time":"2021-08-09T12:49:20+00:00","article_modified_time":"2023-02-01T09:04:43+00:00","og_image":[{"width":1200,"height":718,"url":"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/api-testing-using-karate.jpg","path":"\/home\/hubino.com\/resources\/wp-content\/uploads\/2021\/08\/api-testing-using-karate.jpg","size":"full","id":1839,"alt":"","pixels":861600,"type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/staging.hubino.com\/resources\/#website","url":"https:\/\/staging.hubino.com\/resources\/","name":"Hubino Resources","description":"Blog | Case Studies","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/staging.hubino.com\/resources\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#primaryimage","inLanguage":"en-US","url":"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/api-testing-using-karate.jpg","contentUrl":"https:\/\/staging.hubino.com\/resources\/wp-content\/uploads\/2021\/08\/api-testing-using-karate.jpg","width":1200,"height":718},{"@type":"WebPage","@id":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#webpage","url":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/","name":"API Testing Using KARATE - Hubino Resources","isPartOf":{"@id":"https:\/\/staging.hubino.com\/resources\/#website"},"primaryImageOfPage":{"@id":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#primaryimage"},"datePublished":"2021-08-09T12:49:20+00:00","dateModified":"2023-02-01T09:04:43+00:00","author":{"@id":"https:\/\/staging.hubino.com\/resources\/#\/schema\/person\/2d61fd49f70401801b84564f70deee81"},"breadcrumb":{"@id":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/staging.hubino.com\/resources\/api-testing-using-karate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/staging.hubino.com\/resources\/"},{"@type":"ListItem","position":2,"name":"API Testing Using KARATE"}]},{"@type":"Person","@id":"https:\/\/staging.hubino.com\/resources\/#\/schema\/person\/2d61fd49f70401801b84564f70deee81","name":"admin","image":{"@type":"ImageObject","@id":"https:\/\/staging.hubino.com\/resources\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/d968406bfd6a4fbd587c2798bc6716de?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d968406bfd6a4fbd587c2798bc6716de?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/staging.hubino.com\/resources\/"],"url":"https:\/\/staging.hubino.com\/resources\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/posts\/1389"}],"collection":[{"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/comments?post=1389"}],"version-history":[{"count":2,"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/posts\/1389\/revisions"}],"predecessor-version":[{"id":2472,"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/posts\/1389\/revisions\/2472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/media\/1839"}],"wp:attachment":[{"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/media?parent=1389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/categories?post=1389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.hubino.com\/resources\/wp-json\/wp\/v2\/tags?post=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}