The Hoo Ha’s of Karate Framework — Introduction

Priyanka Prasad
Credit Saison (India)
5 min readFeb 18, 2021

--

Automation everywhere.

We are automating cars, coffee machines, home, curtains, and whatnot.

There is never really one defined way nor is there is one framework.

Karate is one of the automation frameworks that I came across and instantly fell in love with.

The reason being faster and easier automation.

So, about setting up Karate..

At first, a usual formal setup of maven, yarn, java, git.
Maven — to build and manage our Karate Project
Yarn — for dependencies we will be using
Git — SCM -Source code Management
Java — write, test, and debug Java applications
Visual Studio Code — IDE
Postman — To run our tests manually before automating

Extensions

We will be adding an extension: cucumber — VSCode Cucumber (Gherkin) Full Language Support + Formatting + Autocomplete

To run Karate tests, add an extension: Karate Runner — Open/Run/Debug Karate Tests and Build Reports by leveraging Codelens, Activity Bar, Debugger, and much more.

Add another extension: Java extension pack — Popular extensions for Java development and more.

Since we are gonna use Karate Runner, make sure to uninstall the Java Test Runner extension.

Karate Framework Setup

https://github.com/intuit/karate this holds a huge readme file with a very detailed documentation

Start with

mvn archetype:generate \

-DarchetypeGroupId=com.intuit.karate \

-DarchetypeArtifactId=karate-archetype \

-DarchetypeVersion=0.9.6 \

-DgroupId=com.mycompanycs \

-DartifactId=myKarateProject

PS: For this to work, java and maven are mandatory configs.

Open your project on Visual studio code

Karate Framework Overview

Pom.xml — manages all dependencies and plugins we need to run tests

Default dependencies are Karate and Karate JUnit Runner

Properties section contains the karate version which can be updated whenever a new version is released which will automatically update the rest of the karate related dependencies

Build section

Contains the test resources which show where the project files will be located.

Java is the executor file.

Two plugins are configured

Maven compiler and Maven Surefire

Users.feature file

We write our tests in Gherkin format. Given()- When()- Then() format.

Target Folder

Holds temporary generated data. (Test reports, etc)

Src/test/java Folder

This one is the main folder where all the work is done

Logback-test.xml: configuring logging needs

Karate-config.js

This is where the execution starts. Environment variables can be configured here. Also can execute code before running tests.

Examples Folder

ExamplesTest.java — unit 5 runner

The runner shown below is pretty much all needed to run the tests

Now, this is the base setup required.

PS: All the feature files located inside a particular folder will be executed excluding the helper feature files located outside that respective folder

Let's write a few Karate tests while we are all so curious about how karate works

I will be using APIs from an open repository from git to show karate test examples

Let's start off with a GET Request

Fetch the API from inspect element

The response should be as shown below

Let us run the same in Postman just to be sure

The same test, automated in karate looks as shown below easy to understand, readable gherkin test

Moving on to the POST method

I am posting an article through POST request

We will require an auth token to run this Post API, which I saved from the login folder in the network tab.

Running the POST method in postman for the above API

Now let's write a test for the same in Karate

We need to get the auth token that gets generated when a user logs in

Then the same token should be used to Authorize the post request

In the end, the assertion is made using the “match” keyword

The test is run to get the following results

Reports for the tests run can be viewed in the surefire-reports section:

Reports can also be viewed on your browser using a link generated after each test is run

Finally, to wrap things up, this is as good as a 101 for Karate Framework Setup.

Source: https://www.udemy.com/course/karate-dsl-api-automation-and-performance-from-zero-to-hero/?referralCode=9D858B912F18235A4CF8

--

--