Basics of Karate API Automation
What is Karate?
Karate is an Open source tool for API testing (SOAP and Rest) introduced in 2017. It is written in JAVA. We can do API mocking, UI Testing, Performance testing of the APIs as well. It uses BDD syntax to write feature and scenarios.
Features:
Very easy for non-programmers. . .yes this is correct. Although it is written in JAVA but implementation is done using Gherkin syntax in BDD style. So it requires no technical programming language.
Below are some more features available while using Karate-
- Parallel testing, Distributed testing
- Data driven testing
- Debugging and reporting
- Cross browser web UI testing
- Cross platform Desktop testing
- Multi environment testing
Prerequisites:
Java (Do not worry, you don’t need to learn java, you just need to have JAVA in your system). You can check if java is already installed by command java -version.
IDE (Eclipse IDE, IntelliJ, VS code). You can use any of these Platform to write test scripts. In this article/demo we are using Eclipse IDE. You can directly search and download from google.
Dependency management (Maven, Gradle). Now a days you do not need to download separately maven as eclipse comes along with Maven support.
Project setup:
Here we will see how to setup project from scratch. These are very simple steps and lets get started-
Step 1- Open eclipse(This will ask you where to keep project folder. You can go with the suggested one OR can change the folder path).
Step 2- You will find ‘Package explorer’ there you will create first project. There are 2 ways you can create project- Either by clicking on File >> New >> Maven project OR by right click inside Package explorer and New >> Maven project. Now click next >> Click Create a simple project.
Group id and artifact id you can enter like below and click on Finish- (this is just for demo)
Step 3- Now you have to add below Karate dependencies in pom.xml-
Karate Core, Karate Apache, Karate JUnit4
You can simply go to mvnrepository.com and search above dependencies. Add these dependencies in your pom.xml and this will look like-
<dependencies>
<!-- https://mvnrepository.com/artifact/com.intuit.karate/karate-core -->
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-core</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.intuit.karate/karate-apache -->
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>0.9.6</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.intuit.karate/karate-junit4 -->
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>1.3.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
As soon as you save pom.xml, it will create ‘maven Dependencies’ folder inside your project and all the dependencies will be visible here. So far we have completed setup required for first API test.
Let’s start with GET api-
Step 1- Create a feature file under src/test/java folder. OR you can create one folder naming ‘’tests’ under src/test/java and there you can keep all your feature files.
Step 2- Create one feature file e.g. ‘FirstTest.feature’ inside ‘Tests’ folder. If you do not get feature file prefilled with Gherkin syntax, then you need to add Cucumber extension from eclipse marketplace and restart eclipse. It will take few minutes to download and install.
After restarting the eclipse you create new feature file and this time you will get prefilled gherkin syntax like -
Step 3- Now you can remove this sample data and write new Scenario. Feature file starts with “Feature: “ followed by the “Scenario”. (This is the Gherkin syntax, so if you are not aware of Gherkins then you can first go through this link https://cucumber.io/docs/gherkin/). Let us get some sample APIs to automate. You can refer google and pick any sample API. For this article we are using reqres.in where you can find many sample APIs. So your first feature file will look like-
Feature: Sample API tests
Scenario: Test a sample Get API
Given url 'https://reqres.in/api/users?page=2'
When method GET
Then status 200
Syntax here is very simple. Step definitions for url, method or status are predefined. So you just need to pass the endPoints, API type and status code with their respective keywords.
Now you can directly run this feature file by right click and run as cucumber feature. You will get html report after refreshing the project folder inside target >> karate-reports >> karate-summary.html-
If it fails to run for any reason, then you can run it through TestRunner.java file. For that you need to create one file named TestRunner.java inside the same folder.
package tests;
import org.junit.runner.RunWith;
import com.intuit.karate.junit4.Karate;
@RunWith(Karate.class)
public class TestRunner{}
Right click on this file and run as Junit test and this will run all the feature files present in this folder.
So this way you are successfully able to run your first test case. Let’s discuss more advance test cases like Assertion, response match and all in detail.
Let’s validate response:
So far we have learnt how to request an API using karate and validate the status code. Now we need to validate the API response also and use assertion on the response body. Lets start and follow simple steps.
Use above code and print the response as below-
Feature: Sample API tests
Scenario: Test a sample Get API
Given url 'https://reqres.in/api/users?page=2'
When method GET
Then status 200
And print response
And print responseStatus
And print responseTime
karate provides keyword ‘print’ which is very easy to use and get the response. Now when you will run the feature file again, you will see the response in the karate-summary.html file as-
Now we need to validate the response by using match or Assert like-
Scenario: Get demo and use assertion
Given path '/users'
And param page = 2
When method GET
Then status 200
And print response
And match response.data[0].first_name != null
And assert response.data.length == 6
And match response.data[0].first_name == 'Michael'
Here we are validating if first_name is not blank, length of data is 6 and value of first_name is ‘Micheal’. So, this is the simple way to use validations.
Let’s explore POST API:
Now we are going to automate POST api using karate. So, before that lets first know why we use POST api? In http, we use POST method to create a new resource e.g. a new user, a new employee or any new data. We require request data or body to hit POST api. Now lets create new feature file to create test scenario say post.feature file. Again we can have POST sample api from https://reqres.in/ . Here we are using POST api “https://reqres.in/api/users” as-
Feature: Post API demo
# Simple post request
Scenario: Post api demo
Given url 'https://reqres.in/api/users'
And request { "name": "Punit","job": "QA"}
When method POST
Then status 201
And print response
We are passing method as ‘POST’ and using ‘request’ keyword to pass the request body. This is the simple way to hit POST api and get response.
Now run the feature file and you are going to see request response in the karate-summary.html file as-
So here you can see the request body and same response is getting generated.
This is the very basic steps to request and validate response in the Karate framework. For the detailed and more advance features of karate like how to use background and read request/response from different files etc, you can refer link https://puneetvashisth.medium.com/karate-api-testing-framework-c15cead4096b.
Hope you enjoyed this article and please share any feedback which requires improvement in the article. I would be very happy to hear the improvements.