Karate DSL : Getting started

Reading Time: 4 minutes

Hello guys, this is a short blog on how to setup a maven project in karate DSL and small introduction to it’s directory and file structure. So let’s start with,

Why should we use Karate DSL?

  • Karate DSL is an open-source framework that can be used for API testing, front-end UI testing, and performance testing as well. The main advantage of karate is that it is easy to understand and learn.
  • Karate has built-in JSON and XML assertions so it becomes very easy to validate the responses from the API.
  • Karate is fast and reliable as you don’t need to compile the code for executing the test cases. The test cases hit the endpoints directly and get the response.

How Karate DSL is different from other available tools for API testing?

  • You might be asking the same question that why should we use Karate DSL instead of using any other popular tool like Postman. So, the answer to that is tools like Postman does not support UI testing.
  • For UI testing you will need to use some other tool. But karate supports UI testing and can be done using a single framework.
  • Similarly, it supports performance testing as well. So, in a single project, we can also do performance testing using Gatling. Gatling (Scala) will only be used for only for defining the load-model else everything will be handled by karate.
  • Writing the test scripts in karate is very easy and also easy to understand as it uses Gherkins for defining test cases.
  • You just need to define Scenario, Given, when, and then to write a test case successfully.

karate maven project setup

  • Open Intellij and click on Create new project.
  • Select maven as project type and select create from archetype checkbox.
  • Click on Add Archetype and enter the following details in the fields.
  • Click on OK and then click on next.
  • Give a name to your project and click on next.
  • Verify the properties and click on Finish.
  • Wait for the project to load and when it finishes see that the POM file as automatically populated with all the required dependencies for a karate project.

Folder structure for karate

  • Lets see whats the folder structure we follow and what are the files which are required for writing tests in karate.
  • All the test cases are written in src/test/java directory. You can create sub directories according to your own requirements.
  • Lets go through each file one by one.

.feature files

  • All the test script is written in a .feature file. This file contains the test script, assertions and can also contain the request data.
Feature: test script

    * url 'http://www.mocky.io/v2'

  Scenario: get all users and then get the first user by id
    Given path '5eb7f1f52d00005800357b72'
    When method get
    Then status 200
    Then print response
    And match $.username == 'testUser'
  • Feature: We use it to identify the feature file and give it a small title or a one line definition.
  • Background: We use it for defining variables that will be used in the particular .feature file and will be used by all the requests in the feature file. Ex- headers. This section will be run before each script in the feature file.
  • Scenario: It is the description of each test cases and helps us identify that what is happening in the test case.
  • Given: Is like a pre-request script where we give the data which we are familiar with. For example path of a given URL can be different for different types of request. So, it can be defined here.
  • When: We use it to define the action to be performed.
  • Then: We use it to to verify the result of the request. It is used to check that the request executed correctly or not.
  • And: We use it for giving multiple when and then in any requests and also for putting up assertions as well.

Runner files

  • These are simple .java files which are used for running or executing the .feature files in the project.
class TestUsersRunner {
    Karate testUsers() {
        return Karate.run("testUsers").relativeTo(getClass());

  • We can pass the name of our feature file in place of testUsers and it will execute our file.
  • If we want to execute all our feature files in multiple sub directories we can use.
class ExamplesTest {
    Karate testAll() {
        return Karate.run().relativeTo(getClass());

karate-config.js file

  • This file uses the variable to store values which are common across all the feature files. Ex- it can be used for storing the value of baseURL which is common.
  • These variables can be called anywhere in the project.

So, this was a small introduction on how to setup a new maven karate project and about its file structure. In the next blog we will see how to perform API testing using karate.

References: https://github.com/intuit/karate


Written by 

Ankur is a Sr. QA Consultant having experience of more than 3 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advanced concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.