Skip to main content
  1. Articles/

Karate API automation

·7 mins

person doing karate

Introduction to Karate #

Karate is an open-source web API automation framework used to automate APIs and it asserts that the JSON or XML responses are ‘as expected’.

Karate is built on top of Cucumber, another BDD (Behaviour Driven Development) testing framework, and both share some of the concepts. One of these is the use of a Gherkin file. However, unlike Cucumber, tests aren’t written in Java and are fully described in the Gherkin file.

Now, you would think if not Java which programming language I should know to use the karate framework? So, let me spill the beans for you, “No coding knowledge is required for Karate…”. Yes, you heard me right!

Why? Here is the list for all your questions #

  • Java knowledge is not required and even non-programmers can write tests

  • Scripts are plain-text, require no compilation step or IDE, and teams can collaborate using Git or any other standard SCM (Source Code Management) tools

  • Tests are super-readable - as scenario data can be expressed in-line, in human-friendly JSON, XML, Cucumber Scenario Outline tables, or a payload builder approach unique to Karate

Now, for people who have a fear to start automation or are less inclined toward automation and programming, let’s take this up as a starter for automation. And for folks who love programming and are doing wonders in their automation field, this would be icing on your resume.

So, all, let’s get started!!!

Pre-requisites #

Dependencies required #

Create a maven project and add the following 2 dependencies in pom.xml


This is the latest dependency taken from


Feature files and class #

  • A Karate test script has the file extension .feature which is the standard followed by Cucumber.

  • Like Cucumber, you need to have a “Runner” class that runs the feature file(s). Unlike Cucumber, however, there are no step definitions! And this is the magic of Karate. Karate has pre-defined step definitions already present. So we just need to write feature files.

So bottom line is we need to create 2 types of files. One is which would be our Runner file and other would be .feature file(s) for every feature/api #

Maven creates two structures “src/main/java” and “src/test/java”. Under src/test/java create a new package “com.api.feature” and under that package, create the class file as “”

Below would be the code:

package com.api.feature;
import org.junit.runner.RunWith;
       public class TestRunner {

Tip: To format code and indentations in eclipse, select all code by Ctrl+A and then format using Ctrl+Shift+F

Sample GET API for example #

  • Now, before writing the feature file let’s learn which GET API we are going to automate.

  • Suppose, we have to test a simple GET API as follows:

API Url: ""
API method: GET
API response: 200 OK with a list of countries

get api

  • Select all the raw data and validate it using any JSON validator.

Copy all the raw data.

Open “". Paste the raw data in textbox.

Click on “Validate JSON” button

json lint

  • The above image says that our JSON response is correct, also it makes the JSON more readable.

  • Now, with Karate we need to create a script for automating the above scenario and get the response. Also, we need to validate whether we are able to see any particular country details. For e.g:

  1. Verify whether “Afghanistan” is listed in response

  2. Verify whether Afghanistan’s currency code is “AFN”

  3. Verify whether Afghanistan’s language name is “Pashto”

Countries.feature #

Under the “com.api.feature” package (which was already created under src/test/java), let us create a feature file named “countries.feature”

Below would be the code:

Feature: Check list of countries
      * url ''
      * header Accept = 'application/json'
   Scenario: Get list of all countries
      Given path '/rest/v2/all'
      When method GET
      Then status 200
      And match response.[*].name contains ["Afghanistan"]
      And match response.[*].currencies[*].code contains ["AFN"]
      And match response.[*].languages[*].name contains ["Pashto"]

countries feature

Explanation of the above feature file #

  • Feature keyword denotes the Name of the feature under test.

  • Background can be considered a prerequisite and contains URL, header, and param options. (Background section is optional, “*” is the catch-all symbol that can be used instead “Given/ When/ Then”. You can think of it as bullet-point)

  • The scenario is the test case description. (A feature file can have more than 1 scenario and at least 1 scenario should be present)

  • Given is the prerequisite before the test steps get executed.

  • When describes the specific condition which should match in order to execute the next step.

  • Then describes what should happen if the condition mentioned in When is satisfied.

  • All above are Gherkin keywords which are the cucumber standard way of writing test scripts.

  • The path is the endpoint that we need to test.

  • GET is the API method (Other methods are POST, PUT, DELETE).

  • 200 is the status/response code that we are expecting.

  • Core keywords used in Karate DSL are url, path, request, method, and status. These are essential HTTP operations, they focus on setting one (un-named or ‘key-less’) value at a time and therefore don’t need a = sign in the syntax.

Under countries.feature file, in Scenario, after “Then status 200” statement, we can see there are 3 statements that have match keywords. These 3 are verification statements.

JSON response verification using ‘match contains’: (Matching sub-sets of JSON Keys and Arrays)

When JSON response is dynamic we can use “match (name) contains” to check for the existence of some keys.

Below is the explanation for all the 3 verification statements:

And match response.[*].name contains ["Afghanistan"]

Here, the response is the built-in variable that contains the whole JSON response body. If you have noticed, our JSON starts with an array (refer to the image ‘JSON Lint Validator’), so simply put a dot sign and then the array sign []. Under the array, we can see key-value pair(“name”: “Afghanistan”). Hence after [], put a dot and then key as ’name’. Now we need to verify whether the name contains Afghanistan, hence write the “contains” keyword and then value as ‘Afghanistan’.

And match response.[*].currencies[*].code contains ["AFN"]

Here we need to traverse from first array [], then currencies array **currencies[]**, then key as ‘code’ and value as ‘AFN’.

And match response.[*].languages[*].name contains ["Pashto"]

Here we need to traverse from first array [], then languages array **languages[]**, then key as ’name’ and value as ‘Pashto’

###Some points to remember 

  • .feature file is space and case sensitive. There should be space between “*” and “url”. Same with all other key-value pairs under ‘Background’. Otherwise, it will give an error in console as “http request failed: url not set, please refer to the keyword documentation for ‘url’ "
* url ''
  • There should be space between the header option and “=”. Also, the space between “=” and the header option value
* header Accept = 'application/json'

So far, we have “” and “countries.feature” file created.

Now, is the time to run our test. This would be achieved by running the “” file.

Running #

To run, right-click on it and Run As-> 1JUnit Test.

Once the test run is complete we should be able to see the JSON response in the eclipse console, which we observed in the browser before.

Observe the JUnit tab has the details about test status (Pass in green, fail in red)

junit runner

Reports #

As we have used JUnit runner, by default the test reports would be saved under /target/surefire-reports/ as HTML files.

Just refresh the target folder and we will see HTML report (in our case it should be ‘countries.html’). Open it in any browser to view the complete report.

junit report

That is it for this post. In the next post, I would explain in detail about Assert keywords used in Karate.

You can also refer to Karate’s official GitHub page which gives you a complete insight into Karate