This tutorial is an introduction to API Testing using Karate Framework. Learn about the structure of Karate Test Script and steps to build the first test script:
API is an acronym that stands for Application Programming Interface. In simple terms, we can define it as a software intermediary that allows communication between applications.
We need API testing because:
- Results are published faster, hence no more waiting to see if the API is working fine.
- With the faster response, the deployment of these APIs also becomes faster, hence allows for quick turnaround time.
- Early failure detection, even before the UI of the app is created, allow us to mitigate risks and correct faults.
- Large-scale delivery possible in a shorter period.
To be able to work on API Testing, we have various tools available in the market like Postman, Mocha, and Chai. These have demonstrated good results and effective usage for testing APIs, however, these are heavily code influenced. For being able to use these, one must be technically sound and familiar with programming languages.
The Karate Framework beautifully solves this issue of its preceding software tools.
What You Will Learn:
What Is Karate Framework
Karate? Let’s talk Karate. Is it the one from Japan? What do you think? Might be that the great Bruce Lee had developed this in his free time.
Though we would like to delve into the interesting roots of Karate, for now, let’s talk about the Karate tool that has been developed by Peter Thomas, one of the great tools that come to the rescue of API testers.
Karate framework follows the Cucumber style of writing the program which follows the BDD approach. The syntax is easy to understand by non-programmers. And this framework is the only API testing tool that has combined API Automation and performance testing into a single standalone tool.
It provides the users with the ability to execute the test cases in Parallel and perform the JSON & XML checks.
With this information, certain key points can be deduced to further understand the Karate tool in detail:
- Karate is a BDD testing framework instead of a TDD.
- It is designed to be easy for non-programmers. This feature is a game-changer as it allows for more use and access by many people regardless of their technical background or capacity.
- It makes use of the Cucumber feature file and the Gherkins language to write the test which is very easy to understand.
All of these features make it one of the most favorable automation tools available today.
History Of Karate Framework
Created by ‘Peter Thomas’ in 2017, this software aims to make testing functionalities readily available for everyone. It was written in Java and most people expected its files also to be in the same language, however, fortunately, that’s not the case.
Rather, it uses Gherkins files, which is a result of its relationship with the Cucumber framework. The automation software is an extension of Cucumber, therefore inherits the use of Gherkins file in its operation. The big difference between the two is that Karate makes no use of Java while testing, but Cucumber does.
This is the very reason why it caters to non-programmers as the Gherkins syntax is super readable and comprehensive. This is the reason Karate is most suitable and recommended for making an entry into the world of automated API testing.
The following are some features of the Karate Testing Framework:
- Makes use of easy-to-understand Gherkins language.
- It requires no technical programming knowledge like Java.
- It is based on the popular Cucumber standards.
- Easy to create a framework.
- Parallel testing is the core functionality that is provided by the Karate itself, hence we need not depend on Maven, Gradle, etc.
- UI for debugging the Test.
- Calling a feature file from another file.
- Provides supports for the Data Driver Testing that is built in-house, hence no need to depend on external frameworks.
- Built-in Native Rest Reports. Plus, it can be integrated with the Cucumber for better UI Reports and more clarity.
- Provides in-house support for switching configuration across different testing environments (QA, Stage, Prod, Pre-Prod).
- Seamless support for CI/CD integration that can be useful.
- Capable of handling various HTTP calls:
- Web Socket support
- SOAP request
- HTTP
- Browser cookie handling
- HTTPS
- HTML-form data
- XML request
Comparing Karate Vs Rest-Assured
Rest Assured: It is a Java-based library to test the REST services. It uses Java language for writing the lines of code. It helps in testing numerous request categories, which further results in the verification of different business logic combinations.
Karate Framework: A Cucumber/Gherkins based tool, used for testing for SOAP & REST services.
The following table enlists a few more prominent differences between Rest-Assured & Karate Framework:
S.No | Basis | Karate Framework | REST-Assured |
---|---|---|---|
1 | Language | It uses a combination of Cucumber and Gherkins | It makes use of Java Language |
2 | Code Size | Usually, the line of code is less, since it follows Cucumber-like structure | Line of code is more since it involves the usage of Java language |
3 | Technical Knowledge required | Non- Programmers can easily write the Gherkins code | Technical knowledge is required to write Java code |
4 | Data-Driven Testing | Need to make use of TestNG or equivalent to support the same | In-house tags can be used to support data testing |
5 | Does it provide SOAP call support | Yes, it does provide | It is only related to a REST request |
6 | Parallel Testing | Yes, parallel testing is easily supported with the parallel report generation too | Not to a large extent. Though people have tried doing this, the failure rate is more than the success rate |
7 | Reporting | It provides in-house reporting, hence doesn’t need to be dependent on external plugins. We can even integrate it with Cucumber reporting plugin for better UI. | Need to be dependent on External Plugins like Junit, TestNG |
8 | CSV support for external Data | Yes, from Karate 0.9.0 | No, have to use Java Code or library |
9 | Web UI Automation | Yes, from Karate 0.9.5 Web-UI Automation is possible | No, it’s not supported |
10 | Sample GET | Given param val1 = ‘name1’ | given(). |
Hence, as demonstrated by the differences above, it is safe to say that Karate is one of the easiest things that anyone can do.
Tools Required For Working With Karate Framework
Now, since we have got our basic knowledge about Karate Framework on point, let us look at the processes and tools required in setting up the Karate environment.
#1) Eclipse
Eclipse is an Integrated Development Environment used in the field of computer programming. It is mostly used for Java Programming. As mentioned earlier, Karate is written in Java, so it makes more sense why Eclipse is the go-to IDE for the API test software. Another reason is that it’s an open-source tool, and this is a pretty strong reason to opt for this tool.
Note: We could even use IntelliJ, Visual Studio, and other different editors available in the market.
#2) Maven
This is a build automation tool used primarily for building Java projects. It is one way of setting up a Karate environment and writing the code. To set up your Eclipse with Maven requirements, you can click here for Maven installation.
While working in Maven, use Maven dependencies that would help you support Karate Framework.
The following dependencies will be used with Maven in pom.xml.
<dependencies> <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> </dependencies>
Note: The latest versions might be available in the Maven repository.
#3) Gradle
Gradle is an alternative to Maven and can be used in equal capacity. They have their similarities and differences but can be equally used in setting up an environment for our Karate codes.
It is easier to use, flexible, and is recommended to use when our application has some modularization and management requirements with a bunch of plug-ins. The Gradle setup code would look something like this,
testCompile 'com.intuit.karate:karate-junit4:0.6.0' testCompile 'com.intuit.karate:karate-apache:0.6.0'
Note: You could either use MAVEN or GRADLE.
#4) Java Environment setup in your System
Need to setup the JDK and JRE environment to get started with the Karate Framework scripts.
Structure Of Karate Test Script
A Karate test script is known for the possession of the “.feature” extension. This property is inherited from Cucumber. The organization of files in Java convention is also equally permitted. You are free to organize your files according to the Java package conventions.
However, the Maven guidelines instruct that the storage of non-Java files be done separately. They are done in a src/test/resources structure. And the Java files are kept under src/main/java.
But as per the creators of the Karate Framework, they strongly believe that we keep both Java and non-Java files side by side. As per them, it is much easier to look out for the *.java and *.feature files when they are kept together, rather than following the standard Maven structure.
This can be easily done by tweaking your pom.xml as follows (For Maven):
<build> <testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> <plugins> ... </plugins> </build>
Following is the outline of the general structure of Karate Framework:
Now, since this Karate Framework is using the Runner file, which also is needed in Cucumber to run the feature files, so most of the writing will follow the Cucumber standards.
But, unlike Cucumber, the steps do not require a clear definition in Karate and which, in turn, enhance flexibility and ease of operations. We don’t need to add the extra glue that we have to usually add when we follow the Cucumber framework.
The “Runner” class is most of the time named TestRunner.java.
Then the TestRunner.java file will take the form of:
import com.intuit.karate.junit4.Karate; import org.junit.runner.RunWith; @RunWith(Karate.class) public class TestRunner { }
And talking about the .feature file, it contains all the testing scenarios that needs to be tested to make sure that the API is working as per the expected requirements.
A general *.feature file looks something like as shown below:
Feature: fetching User Details Scenario: testing the get call for User Details Given url 'https://reqres.in/api/users/2' When method GET Then status 200
Creating The First Basic Karate Test Script
This section will help you get started with the creation of your very first Test Script, which will be helpful for you to convert APIs in the form of a Karate framework.
Before we write the basic Karate test scripts, please install the following requisites on your machine:
- Eclipse IDE
- Maven. Set the appropriate Maven path.
- JDK & JRE. Set the appropriate path.
Let’s have a look at the step-by-step approach:
#1) Create a new MAVEN Project in Eclipse Editor
- Open Eclipse
- Click on File. Select New Project.
- Select Maven Project
- Choose the Workspace location.
- Select the Archetype (usually we choose “Maven-archetype-quickstart 1.1” for simple Maven projects).
- Provide the Group ID & the Artifact ID (we have used the following values in our example).
- Group ID: Karate
- Artifact ID: KarateTestScriptsSample
- Click on Finish to complete the setup.
#2) Once created, now you will be able to see the following structure in the Project Explorer window.
#3) Include all your Dependencies.
Our very first step, after the setup we will be to include all the dependencies that will be required for the execution. We will keep all the <Dependency> tag under the POM.xml (Assuming you are already aware of the POM.xml usage).
- Open POM.xml and copy the below code under the dependency tag and save the file.
<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>
Click here for source.
#4) Let’s Brainstorm the scenario, what are we going to test in this Karate Basic Test Script.
Scenario:
We will be testing an API with this URL.
Path: api/users/2
Method: GET
And we need to validate, whether the request is returning a Success code (200) or not.
In simple terms, we are just going to test a sample API to see whether or not it’s getting successfully executed.
Note: We are taking a sample API that is available for testing. You could choose any PATH or could refer to your API.
Click here for source.
#5) Now our next step would be to create a .feature file.
As discussed in the introduction section, the .feature file is the property that has been inherited from Cucumber. In this file, we will write out the test scenarios that need to be executed for performing the API Testing.
- Go to Folder src/test/java in your project.
- Right Click on it and create a new file – userDetails.feature. Then click on the Finish button.
Now you will see the following file under the folder src/test/java
The Green colored icon resembles the .feature file in Cucumber that we just created.
- Once the file has been created, now we will write our test scenarios that will be discussed in the following section.
#6) Since we have the scenario and the blank .feature file ready, now let’s get started with our first script. Let us start coding
Write the following line of Code under userDetails.feature file that we created in Step #5:
Feature: fetching User Details Scenario: testing the get call for User Details Given url 'https://reqres.in/api/users/2' When method GET Then status 200
Let us try to understand the components that are written in the above file:
- Feature: Keyword explains the name of the feature we are testing.
- Background: This is an optional section that is treated as a Pre-requisite section. This can be used to define what all is needed to test the API. It contains HEADER, URL & PARAM options.
- Scenario: Every feature file that you will see will have at least one feature (although it can give multiple scenarios). It is the description of the test case.
- Given: It is the step that needs to be executed before any other test step is performed. It is a mandatory action to be performed.
- When: It specifies the condition that should be met to perform the next test step.
- Then: It tells us that what should happen in case the condition mentioned in the When is satisfied.
Note: All the above-mentioned keywords are from the Gherkins language. These are the standard way of writing the test scripts using Cucumber.
And some more words used in the feature file are:
- 200: It is the status/response code which we are expecting (Click here for the list of status codes)
- GET: It is the API method like POST, PUT, etc.
We hope this explanation was easy for you to understand. Now you will be able to relate to what exactly is written in the above file.
Now we need to create a TestRunner.java file
As explained in the above section, Cucumber needs a Runner file that would be required to execute the .feature file that contains the testing scenarios.
- Go to Folder src/test/java in your project
- Right Click on it and create a New Java file: TestRunner.java
- Once the file has been created, place the following lines of code under it:
import org.junit.runner.RunWith; import com.intuit.karate.junit4.Karate; @RunWith(Karate.class) public class TestRunner { }
- Test Runner is the file that will now be executed to perform the desired scenario that has been written under Step #5.
#7) Now we are ready with both the files TestRunner.Java and userDeatils.feature. The only task left for us is to Run the script.
- Go to TestRunner.java file and Right-click on the file as shown in the below image.
- Choose Run As -> Junit Test
- Now, once selected, you will start observing that the test case has now started.
- Wait for the test script to execute. Once done you will observe something like shown in the below image in your window.
- Finally, we can say that we have successfully created our very first basic Test Script using the Karate Framework.
#8) Last, the Karate framework also gives an HTML report presentation for the execution that has been performed.
- Go to Target Folder -> surefire-reports-> Here you will see your HTML report that you can open.
** We would also suggest you to open the same using the Chrome Browser for a better look and feel.
- Following HTML Report will be shown to you depicting Scenarios & Test that has been executed for the mentioned scenario:
Conclusion
In this tutorial, we have discussed API testing, different testing tools available in the market, and how the Karate Framework is a better option compared to its counterparts.
We followed a step-by-step approach to create our first basic test script. We started with creating a basic Maven project in Eclipse IDE to create a .feature file, which contains all the testing scenario and a Runner file to execute the test case mentioned in the .feature file.
At the end of the multiple steps, we could see the execution report of the test results.
We hope, this tutorial was helpful for the beginners in learning how to build their first test script using the Karate Framework and carry out API Testing. This detailed step-by-step approach is a wonderful way to run and execute various tests on the API.