Hi Folks!

Hope you are doing great!

It's been long, I haven’t posted anything so thought its high time, let's share some new learning :)

Till now I have been posting about Selenium and technical stuff related to it. Something new, today I am going to share my learning about the Karate API automation tool developed by Peter Thomas.

So roll your sleeves up, have Eclipse in your system up-and-running and lets rock!

To give you all a background, Karate is an open-source web API automation framework used to automate APIs and it asserts that the JSON or XML responses are ‘as expected’.

Karate is built on top of Cucumber, another BDD (Behaviour Driven Development) testing framework, and both share some of the concepts. One of these is the use of a Gherkin file. However, unlike Cucumber, tests aren’t written in Java and are fully described in the Gherkin file.

Now, you would think if not Java which programming language I should know to use karate framework. So, let me spill the beans for you, “No coding knowledge is required for Karate…”. Yes, you heard me right!

Why? Here is the list for all your questions:

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

Scripts are plain-text, require no compilation step or IDE, and teams can collaborate using Git or any other standard SCM (Source Code Management) tools

Tests are super-readable — as scenario data can be expressed in-line, in human-friendly JSON, XML, Cucumber Scenario Outline tables, or a payload builder approach unique to Karate

Now, people who have a fear to start automation or are less inclined towards automation and programming, lets take this up as a starter for automation.

And for folks who love programming and are doing wonders in their automation field, this would be an icing to your resume.

So, all, let's get started!!!

Pre-requisites:

Eclipse installed (Refer my medium post)

Maven installed in Eclipse (Refer my medium post)

Java (8 and above) installed (Refer my medium post)

Dependencies required:

Create a maven project and add the following 2 dependencies in pom.xml

<dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-apache</artifactId> <version>0.9.5</version> <scope>test</scope> </dependency> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit4</artifactId> <version>0.9.5</version> <scope>test</scope> </dependency>

This is the latest dependency taken from https://mvnrepository.com/

pom.xml

Feature files and TestRunner.java class

A Karate test script has the file extension .feature which is the standard followed by Cucumber.

Like Cucumber, you need to have a “Runner” class which runs the feature file(s). Unlike Cucumber, however, there are no step definitions! And this is the magic of Karate. Karate has pre-defined step definitions already present. So we just need to write feature files.

So bottom line is we need to create 2 types of files. One is TestRunner.java which would be our Runner file and other would be .feature file(s) for every feature/api

TestRunner.java

Maven creates two structures “src/main/java” and “src/test/java”. Under src/test/java create a new package “com.api.feature” and under that package, create the class file as “TestRunner.java”

Below would be the code:

package com.api.feature; import org.junit.runner.RunWith; import com.intuit.karate.junit4.Karate; @RunWith(Karate.class) public class TestRunner { }

Tip: To format code and indentations in eclipse, select all code by Ctrl+A and then format using Ctrl+Shift+F

Sample GET API for example:

Now, before writing the feature file lets learn which GET API we are going to automate.

Suppose, we have to test a simple GET API as follows:

API Url: “https://restcountries.eu/rest/v2/all” API method: GET API response: 200 OK with a list of countries

For better understanding just copy “https://restcountries.eu/rest/v2/all” and paste it in the browser. You would see the details of all countries in raw format.

GET API countries response

Select all the raw data and validate it using any json validator.

Copy all the raw data. Open “https://jsonlint.com/”. Paste the raw data in textbox. Click on “Validate JSON” button

JSON Lint Validator

The above image says that our JSON response is correct, also it makes the JSON more readable.

Now, with Karate we need to create a script for automating the above scenario and get the response. Also, we need to validate whether we are able to see any particular country details. For e.g:

Verify whether “Afghanistan” is listed in response Verify whether Afghanistan’s currency code is “AFN” Verify whether Afghanistan’s language name is “Pashto”

Countries.feature

Under the “com.api.feature” package (which was already created under src/test/java), let us create a feature file named “countries.feature”

Below would be the code:

Feature: Check list of countries Background: * url ‘https://restcountries.eu' * header Accept = ‘application/json’ Scenario: Get list of all countries Given path ‘/rest/v2/all’ When method GET Then status 200 And match response.[*].name contains [“Afghanistan”] And match response.[*].currencies[*].code contains [“AFN”] And match response.[*].languages[*].name contains [“Pashto”]

countries.feature

Explanation of the above feature file:

Feature keyword denotes Name of the feature under test.

keyword denotes Name of the feature under test. Background can be considered as prerequisites and contains URL , header, param options. (Background section is optional, “*” is the catch-all symbol which can be used instead “Given/ When/ Then”. You can think of it as bullet-point)

can be considered as prerequisites and contains , options. (Background section is optional, “*” is the catch-all symbol which can be used instead “Given/ When/ Then”. You can think of it as bullet-point) Scenario is the test case description. (A feature file can have more than 1 scenario and at least 1 scenario should be present)

is the test case description. (A feature file can have more than 1 scenario and at least 1 scenario should be present) Given is the prerequisite before the test steps get executed.

is the prerequisite before the test steps get executed. When describes the specific condition which should match in order to execute the next step

describes the specific condition which should match in order to execute the next step Then describes what should happen if the condition mentioned in When is satisfied.

describes what should happen if the condition mentioned in is satisfied. All above are Gherkin keywords which are the cucumber standard way of writing test scripts

path is the endpoint which we need to test

is the endpoint which we need to test GET is the API method (Other methods are POST, PUT, DELETE)

is the API method (Other methods are POST, PUT, DELETE) 200 is the status/response code which we are expecting

is the status/response code which we are expecting Core keywords used in Karate DSL are url, path, request, method and status. These are essential HTTP operations, they focus on setting one (un-named or ‘key-less’) value at a time and therefore don’t need an = sign in the syntax

Under countries.feature file, in Scenario, after “Then status 200” statement, we can see there are 3 statements which has match keyword. These 3 are verification statements.

JSON response verification using ‘match contains’: (Matching sub-sets of JSON Keys and Arrays)

When JSON response is dynamic we can use “match (name) contains” to check for the existence of some keys

Below is the explanation for all the 3 verification statements:

1. And match response.[*].name contains [“Afghanistan”]

-> Here response is the built-in variable which contains the whole JSON response body. If you have noticed, our JSON starts with array (refer the image ‘JSON Lint Validator’), so simply put a dot sign and then the array sign [*]. Under array we can see key value pair(“name”: “Afghanistan”). Hence after [*], put a dot and then key as ‘name’. Now we need to verify whether name contains Afghanistan, hence write “contains” keyword and then value as ‘Afghanistan’

2. And match response.[*].currencies[*].code contains [“AFN”]

-> Here we need to traverse from first array [*], then currencies array currencies[*], then key as ‘code’ and value as ‘AFN’

3. And match response.[*].languages[*].name contains [“Pashto”]

-> Here we need to traverse from first array [*], then languages array languages[*], then key as ‘name’ and value as ‘Pashto’

**Some points to remember**

.feature file is space and case sensitive. There should be space between “*” and “url”. Same with all other key-value pairs under ‘Background’. Otherwise, it will give an error in console as “http request failed: url not set, please refer to the keyword documentation for ‘url’ ”

* url ‘https://restcountries.eu'

There should be space between header option and “=”. Also space between “=” and header option value

* header Accept = ‘application/json’

So far, we have “TestRunner.java” and “countries.feature” file created.

Now, is the time to run our test. This would be achieved by running the “TestRunner.java” file.

Running TestRunner.java:

To run TestRunner.java, right-click on it and Run As-> 1JUnit Test.

Once the test run is complete we should be able to see JSON response in eclipse console, which we observed in browser before.

Observe JUnit tab has the details about test status (Pass in green, fail in red)

JUnit Runner console output

Reports:

As we have used JUnit runner, by default the test reports would be saved under /target/surefire-reports/ as html files.

Just refresh the target folder and we will see HTML report (in our case it should be ‘countries.html’). Open it in any browser to view complete report.

JUnit HTML Report

That is it for this post. In the next post I would explain in detail about Assert keywords used in Karate.

You can also refer Karate’s official github page which gives you a complete insight of Karate