Karate Framework: Master of API Test Automation ?

Karate Framework: Master of API Test Automation ?

For all API developers and testers, a not-so-new API testing framework is gaining significant attention as we enter 2021.

So, what makes this framework stand out? Isn't RestAssured sufficient? And why are we even spending our time reading about it?

Well, to begin with, the Karate framework has entered the inner radar of ThoughtWorks, making it deserving of our attention.

Let’s explore a few features it provides:

BDD Syntax and a true DSL

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

In the excerpt below, the Karate-DSL provides keywords that perform all API-related requests, as well as keywords for assertions.

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"

Java knowledge is not necessary, and even non-programmers can write tests.

This makes Karate quite fascinating. 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 undoubtedly enable one to fully 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 {
return true;
} catch(e) {
karate.log('*** invalid date string:', s);
return false;

Create a feature file and use it to validate the date format of a field within your JSON response:

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.

Scripts can call other scripts, making all your feature files reusable and allowing data to be passed from one to another.

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

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

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

And so many more..

Dear Readers,

I hope you are enjoying the content I provide on my blog. As a passionate writer and dedicated software developer, I strive to create valuable and informative articles that resonate with you. Today, I would like to extend an invitation to support my work and help me continue producing high-quality content.

I have set up a Buy Me a Coffee page, a platform that allows readers like you to show their appreciation by making a small donation. Your contribution, no matter how big or small, goes a long way in supporting my efforts and keeping the blog running. You can also sponsor using the links at the bottom of this page.

Did you find this article valuable?

Support Ish Mishra by becoming a sponsor. Any amount is appreciated!