Lets Do API Testing With Karate Framework

Reading Time: 3 minutes

API Testing

Application Programming Interface(API) is nothing and it is a logic that is written by the developer and as a tester you will testing it. tester verifying that it is meet the expected result or not it is called API Testing.API testing is a software testing that tests the APIs  from their functionality, reliability, performance, to security.

Karate (DSL) For API Testing

Initially Karate was created by ‘Peter Thomas’ In 2017.Karate is a open source tool for api testing(SOAP & REST).karate is written in java.we can do API mocking,UI testing,performance testing of the API’s using karate. karate uses BDD syntax. We also use the features and scenarios in karate.

Features of karate:-

  1. Easy for non programmers(written in java,but implemented using BDD style).
  2. Requires no technical programming language so you don’t worry if you have not any programming background.
  3. We can do both parallel testing and Distributed testing using karate.
  4. We can do multi environment testing and therefore we can set multiple environment and do our testing and we can get our data from external files (CSV, JSON files).

Types of API Request in API Testing

4. Programming Read/Write Services - RESTful .NET [Book]
  1. GET- Basically It used for the get the data from any resource.
  2. POST- It also used for the send the data to API.
  3. DELETE-While it is used for the delete the data from API.

Prerequisites for karate

Java 

Karate requires at least java 8

Also Karate works fine on openjdk

IDE

We need some platform where we write our test and run and check our result. we need an IDE that is an integrated development environment. Therefore We have multiple options(Eclipse IDE,Intellij,VSCode).

Maven

Also Install Cucumber plugin to create and run feature files

Karate project Structure

  1. Pom.xml file:-this is a file where we put all the dependencies and plugins to run the code.following are the dummy structure of the pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.intuit.karate</groupId>
<artifactId>karate-project</artifactId>
<version>1.1.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-project</artifactId>
<version>1.1.0</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>

</build>



</project>

2.TestRunner.java file:-it used for the execute  test scenario which is present in the feature files.following is the structure of the TestRunner.java file.

package tests;
import com.intuit.karate.junit5.Karate;


class testrunner {
@Karate.Test
Karate testSample() {
return Karate.run("get").relativeTo(getClass());
}
@Karate.Test
Karate testSample2() {
return Karate.run("post").relativeTo(getClass());
}
}

3 .feature file:-It puts all the testing scenarios that are needed to be tested ,Therefore feature file should be present in the same folder in which TestRunner is present. Feature file is where we write our test. following are the structure of the feature files for get and post method.

Feature: Get API Demo


Scenario: Get Demo 1
Given url 'https://reqres.in/api/users/2'
When method GET
Then status 200
Then print response
Feature:Post API Demo

Scenario: Post Demo
Given url 'https://reqres.in/api/users'
When method POST
Then status 201
Then print response

Karate Configuration(Karate-config)in karate framework

Karate expects a file called karate-config.js to exist in the class path and contains a function JavaScript.This function will  return the JSON object. all keys and values for that JSON object will be available as  variables.If you want to set some common variables across all your feature files. or if you run your test case in multiple environment. or when you want to globally stored the authentication token generation data like client id,client secret in some particular file. then you can make use of karate-config.js. lets see an example

Create a file karate-config.js in src java package

function fn(){
var config = {
abc : 'test'
}
return config;

Create config.feature file

Feature: user Details
Background:
* def expectedOutput = read('../data/result2.json')

Scenario: request user Details
Given url 'https://reqres.in/api/users/2'
When method GET
Then status 200
And match response == expectedOutput
Then print 'abc---' ,abc

References

https://github.com/karatelabs/karate

https://en.wikipedia.org/wiki/Karate_(software)

Scala Future

Written by 

Himani is a software intern in knoldus. She completed Post Graduation from Uttrakhand Technical University.