Karate Framework: Master of API Test Automation ?

Ish Mishra
3 min readFeb 17, 2021

For all the API Developers and testers, a not so new API test framework is starting to catch a lot of limelight as we enter 2021.

So what makes this framework stand out ? Isn’t RestAssured enough? and why are we even spending our time to read about it ?

Well to start with, Karate framework has moved into the inner radar of ThoughtWorks, which makes it worthy of our little attention.

Let’s explore a few features it provides:

  1. BDD Syntax and a true DSL

The scripts in Karate look very similar to Cucumber-Gherkin feature files, but are the actual scripts and do not require to mapping to an underlying code explicitly.

In the extract below the Karate-DSL provides keywords that perform all API related requests and even keywords to assert.

@debugs
Feature: Test for the home page

Background: Define URL
Given url "https://localhost/"

Scenario: Get all the tags and assert
Given path 'tags'
When method Get
Then status 200
And match response.tags contains ['test']
And match response.tags !contains ['truck']
And match response.tags contains any ['fish','dog','SIDA']
And match response.tags == "#array"
And match each response.tags == "#string"

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

This makes Karate very interesting. It has a very short learning curve and all those manual testers who aren't into coding can easily pick it up.

Don’t get me wrong knowledge of Java or JavaScript will definitely enable one to truly utilize the framework.

For example we want to verify the format of a timestamp field in the JSON response. We create a function in JavaScript and call Java code inside it.

function fn(s) {
var SimpleDateFormat = Java.type("java.text.SimpleDateFormat");
var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.ms'Z'");
try {
sdf.parse(s).time;
return true;
} catch(e) {
karate.log('*** invalid date string:', s);
return false;
}
}

Then create a feature file and use that to verify date format of a field within your response JSON:

Scenario: Get 10 articles and assert and validate schema
* def timeValidator = read('com/learn/timeValidator.js')

Given params {limit: 10, offset: 0}
Given path 'articles'
When method Get
Then status 200
And match each response.data ==
"""
{
"createdAt": "#? timeValidator(_)",
"updatedAt": "#? timeValidator(_)"
}
"""

You can even directly call the Java methods in the Karate feature file.

3. Scripts can call other scripts making all your feature files reusable and even pass data from one to the other

Background: Define URL
Given url apiUrl
* def tokenResponse = callonce read('classpath:com/learn/CreateToken.feature')
* def token = tokenResponse.authToken

Scenario: Create a new article

Given header Authorization = 'Token ' + token
And path 'articles'
And request articlesRequestBody
When method Post
Then status 200

4. Native support for reading YAML and even CSV files — and you can use them for data-driven test

5. Ideal for testing the highly dynamic responses from GraphQL API-s because of Karate’s built-in text-manipulation and JsonPath capabilities

6. And so many more..

--

--