Karate Framework for API Testing

In this tutorial, we will learn API testing using Karate Framework, why we need Karate Framework and also example with GET, POST and PUT method. Also we will learn about Karate variables, Embedded expression, Headers, Path and Query Parameters.

If you want to perform API testing but you don’t have knowledge of any programming language then you should choose Karate framework to perform API testing.

What is Karate Tool?

Karate tool was developed by Peter Thomas in 2017. It is one of the great tool for API testing.

Karate is an open-source API (SOAP & REST) testing automation tool written in Java. API tests are written in BDD (Behaviour Driven Development) Using Gherkin syntax. Unlike other API testing tool like Cucumber, JBehave and Specflow, Karate has written all step definitions so we don’t have to write it.


Why we need Karate Framework

  1. Open Source
  2. API Testing, Mocking
  3. Performance Testing
  4. UI Automation, Reporting
  5. Parallel Execution
  6. BDD Style, Native JSON/XML support
  7. Readable syntax
  8. Mix API and UI test Automation
  9. Cross Platform desktop testing

Karate can handle

  • Web Socket support
  • SOAP request
  • HTTP
  • Browser cookie handling
  • HTML – form Data
  • XML request


Perquisites and Setup for Karate Framework

Java – Karate requires at least Java 8.

IDE (eclipse, IntelliJ)


Project Setup Step by Step

  1. Create Project
  2. Add Maven dependencies
  3. Make project ready for Karate API Tests


Create Karate Framework Sample Project

Step 1: Open Eclipse

Step 2: File > New > Maven Project

Step 3: Provide the project details and create project

Step 4: Add Maven dependencies in pom.xml

  • Karate core
  • Karate Apache
  • Karate Junit4

Step 5: Saved the Project.

Create Karate Framework Project in eclipse


Added karate dependencies

Karate Maven Dependencies


Create First API Test Using Karate

Step 1: Create a feature file under src > test > java folder.

Step 2: Add Cucumber plugin in Eclipse > Restart eclipse.

Step 3: Create a feature file and write a Scenario.

In below image we can see I have created feature file. (Also added cucumber plugin and restart the eclipse). For adding cucumber plugin Go to eclipse marketplace > Search Cucumber > Install it.


cucumber feature file

Now we will create a scenario in feature file.

Feature: Sample API Test

  Scenario: Test a sample GET API
    Given URL 'https://reqres.in/api/users?page=2'
    When method GET
    Then status 200

Now we can right click on feature file and run it. It will create a Karate report under Karate Project > target > Karate report > karate-summary.html

Run Feature File

Target Folder Created-


Karate Report

Step 4: Create a TestRunner.java class under src/test/java

package tests;

import org.junit.runner.RunWith;

import com.intuit.karate.junit4.Karate;

public class TestRunner {


Added @RunWith(Karate.class) annotation.

Step 5: Now we can run this TestRunner class as JUnit.

Run TestRunner class

After run TestRunner class, we can see Junit console report.

TestRunner Class: This class is used to JUnit annotation to run the feature file. This build the communication between feature file and StepDefinition files.

It is also very useful when we want to run our feature files with some conditions using tags or we want to run specific feature file, all things are control by TestRunner class.

Karate Framework – GET Method

What is GET Method?

Get method in HTTP is used to read or access data or information.


Get list of users

Get details of employee etc.

Note: In GET API request, we do not need to provide the body (payload).

GET Method:

Step 1: Create a feature file under src/test/java folder.

Step 2: Add feature and scenario description.

Step 3: Add steps to run a sample GET API request.

Step 4: Run this feature file and get the report in target > karate-reports > karate-summary.html.

Karate Framework Report:

By Clicking on each step in report we can see the step’s information.

Example: After click on response step we can see response of GET request.

GET with Background

Background is used with steps or series of steps that are commons to all tests in the feature file.

The steps which are defined under background will run before each and every scenario for a feature file.

Example: In an application testing if we are login the application in each scenario then we can put the login scenario under background.

Feature File:

get feature file with background

Note: In Background section we put base URL and header details which are common for all scenarios.

Karate Report:

GET Example 2: In the Given section we are using path/query parameter. We can use this with param in And condition like below.


Assertion in Karate Feature File

We need to use assertion to validate the response data.

In below image in get demo 4 scenario I have added few assertions.

Note: You can use Jsonpathfinder – to find the path of json data. I have used this for my response.


Karate POST Method

What is POST Method?

POST method in HTTP is used to create a new resource on the server.


Create new user

Create new employee

Any new data..

Note: In POST API request, we have to provide the body (payload).

POST Method:

Step 1: Create a feature file under src/test/java folder.


POST Feature File

Step 2: Add feature and scenario description.

Step 3: Add steps to run a sample POST API request

POST method

Karate Framework POST Report

Karate PUT Method

What is PUT Method?

PUT method in HTTP is used to update the resources on the server.


Update Job title

Update employee details

See below screenshot I have created new put feature file and written Put method for updating employee name.

Feature: Put API demo

  Scenario: Put Demo 1
    Given url 'https://reqres.in/api/users/2'
    And request { "name": "morpheus", "job": "zion resident"}
    When method PUT
    Then status 200
    And print response
    And match response.name == "morpheus"


karate put report

Karate- Headers, Path and Query Parameters

Headers: In this section we can defined additional details of API to process the request.
Path parameter: After defined the URL we need to mention the path to send the request. Path is a keyword in karate.
EndPoint: ‘https://www.kloia.com/ blog?page=2’

Given url ‘https://www.kloia.com/’
And path ‘blog?page=2’

Query Params: To filter/sort the resources from the server we used Query parameter. It is always start with Question mark (?).

Given url ‘https://www.kloia.com/’
And path ‘blog’
And param page = 2

Karate Variable

def keyword is coming from Karate framework. And it is used to create a variable.

Where we can use the variables-

  • When a constant value keeps on repeating replace it with a variable.
  • Passing the data from one feature file to another file.
  • Storing the data from external file.

<Gherkin Keyword> <def> <variable name> = <Value>


1. During variable creation, the gherkin keyword is optional. You can use * char instead of Gherkin keyword.

2. Scope of this variable is only on scenario in which it is defined.

Karate – Embedded Expression

In the post request, instead of giving hard coded value we can give the variable and this is done by embedded expression.

def name = “Amit”

def job=”Lead”

Embedded Expression of above variable –



Request with embedded expression:-

{“name”: ‘#(name)’,”job”: ‘#(job)’}


Given def user = { name: 'john', age: 21 }
And def lang = 'en'
When def session = { name: '#(user.name)', locale: '#(lang)', sessionUser: '#(user)'  }

For more details check this link- Embedded Expression


In this post, we have covered ‘Karate Framework for API Testingwith GET, POST, PUT Method.

Final word, Bookmark this post  ‘Karate Framework for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

Leave a Comment