Karate DSL is a relatively new tool in the market to perform web services testing. One of the main advantage of this tool is that a tester need not to be a coding expert because it’s built on top of ‘Cucumber’ thus follow ‘Gherkin’ format. See, more details below.

In this guide, we are going to present solutions to perform REST API automation testing using Karate which are easily understandable and quick to implement and use. Well, this is not a complete guide but help you to get the basic details about tool. To learn this tool thoroughly, Go checkout ‘Karate Dsl’ tutorial here

Karate_Techndeck

Check out: APACHE HTTPCLIENT TUTORIAL with examples (Latest 2022)

Testing Web Services with KARATE...!!! Click To Tweet

1. Introduction

Karate DSL was released by Intuit as an open source tool to perform API Testing. Those who don’t know coding, this tool will make their life easier. 

Key Points:

  • It is built on top of Cucumber JVM.
  • Tests are easy to write even for non-programmers.
  • It supports multi-threaded parallel execution.
  • You can generate reports just like any other standard Java project.
  • If you are already using Java and Cucumber in your project, then KarateDSL might be a great choice.

2. Using Karate (How To)

In order to start the implementation of Karate DSL based test scripts, we need to set up the environment first. For this reason, this tutorial will explain and help to set up the prerequisite and required dependencies in Eclipse.

Maven based Project:

If you are using Maven as the build tool for you project, use below mentioned dependency and add it into the POM file:

 

Note: If you are going to use Junit-5 then make sure to use karate-junit5 instead of karate-junit4 in the dependency above. 

Gradle based Project:

3. Project Folder Structure

If you follow the below screenshot, there are 2 main files present in the Karate Project:

Karate_Project_Folder_Structre_Techndeck

  1. Feature file (.feature) : Those who worked on cucumber, they must be aware of what this file is all about. Basically, the karate test script has the file extension of .feature which is followed by Cucumber. You are free to organize your files using regular Java package conventions. 
  2. Test Runner file (.java) : Just like Cucumber, you need to use a runner class file to execute the feature files. The only difference you will see unlike cucumber is that you don’t need step definitions in Karate. 

If your project is maven based, you generally follow the regular maven based structure in which you keep the source files separately from the *.java files. But, what karate team suggested to keep both files at the same place. If you notice in the above screenshot, both .feature file and .java file are in one package (com.demo.test) inside src/test/java. 

Also, apart from these 2 files, there is one more file that you need if your project is maven based and i.e POM.xml. See below screenshot to refer:

Karate_POM_XML_Techndeck

4. Testing HTTP Resources

In this section, we will test the ‘Dummy Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purpose which are performing various CRUD operations.

4.1 GET Endpoint Request

Let’s take an example of one of the API GET endpoint available at the above-mentioned website which is ‘/employees’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/employees‘.

At the above resource URL, information about all the employees is present and now we are trying to access those employees details in this below example like id, employee_name, employee_age, employee_salary and profile_image.

Here is the code to send the GET request to the above mentioned Service Endpoint:

Let’s try to understand the code:

1. Specifying the Feature & Scenario (typical cucumber format)

2. Setting up Endpoint URI in ‘Given’ 

Endpoint URI is the address to the Resource. And, by this particular line of code, we are specifying to Karate to use “dummy.restapiexample.com/api/v1” as the root URL of the service.

3. Specifying the type of request (GET) in ‘When’ condition

4. Checking up Status Code & other Response validations in ‘Then’

a. status 200 : It will check the status code coming back from the service is 200

b. print ‘Response is: ‘, response : This line of code will print the response from the service in the console.

c. match response contains {“id”: “2091”,”employee_name”: “Wadkar Performance”,”employee_salary”: “700000”,”employee_age”: “29”,”profile_image”: “”} : This line of code helps to check if the specified content is present in the response or not.

Eclipse Console Output:

4.2 POST Endpoint Request

Let’s take an example of one of the API POST endpoint available at the above-mentioned website which is ‘/create’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/create‘.

At the above resource URL, we are going to submit data in the form of JSON to create an employee.

JSON Request Body:

Here is the code to send the POST request to the above mentioned Service Endpoint:

Let’s try to understand the code:

1. Specifying the Feature & Scenario (typical cucumber format)

2. Setting up Endpoint URI in ‘Given’ 

Endpoint URI is the address to the Resource. And, by this particular line of code, we are specifying to Karate to use “dummy.restapiexample.com/api/v1” as the root URL of the service.

3. Specifying the ‘json’ formatted employee data that need to be sent to the service to create the employee

4. Specifying the type of request (POST)

5. Checking up Status Code & other Response validations in ‘Then’

a. status 200 : It will check the status code coming back from the service is 200

b. print ‘Response is: ‘, response : This line of code will print the response from the service in the console.

c. match response == {“name”: “Isha”,”salary”: “5000”,”age”: “20”,”id”: “2205”} : This line of code helps to check if the response coming from the service exactly matches with the specified expected content.

Eclipse Console Output:

4.3 PUT Endpoint Request

Let’s take an example of one of the API PUT endpoint available at the above-mentioned website which is ‘/update/{id}’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/update/{id}’.

At the above resource URL, we are going to submit data in the form of JSON to update an existing employee which is having ‘id’ as ‘4710’.

NOTE: This ‘id’ belongs to the employee which is generated during the POST call to create the employee. So, this id : 4710 generated for me when I had executed the create employee using the POST call. In order to get yours, first execute the POST Request call available at this link and grab the ‘id‘ from the response and use it in your PUT Request Test below in place where I am using ‘4710’. 

JSON Request Body:

Here is the code to send the PUT request to the above mentioned Service Endpoint:

Let’s try to understand the code:

1. Specifying the Feature & Scenario (typical cucumber format)

2. Setting up Endpoint URI in ‘Given’ 

Endpoint URI is the address to the Resource. And, by this particular line of code, we are specifying to Karate to use “dummy.restapiexample.com/api/v1” as the root URL of the service.

3. Specifying the ‘json’ formatted employee data that need to be sent to the service to update the employee

4. Specifying the type of request (POST)

5. Checking up Status Code & other Response validations in ‘Then’

a. status 200 : It will check the status code coming back from the service is 200

b. print ‘Response is: ‘, response : This line of code will print the response from the service in the console.

c. match response == {“name”: “put_test_employee”,”salary”: “1123”,”age”: “23”} : This line of code helps to check if the response coming from the service exactly matches with the specified expected content.

Eclipse Console Output:

4.4 DELETE Endpoint Request

Let’s take an example of one of the API DELETE endpoint available at the above-mentioned website which is ‘/delete/{id}’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/delete/{id}’.

At the above resource URL, we are going to make a delete call to remove an existing employee who is having ‘id’ as ‘11400’.

NOTE: This ‘id’ belongs to the employee which is generated during the POST call to create the employee. So, this id : 11400 generated for me when I had executed the create employee using the POST call. In order to get yours, first execute the POST Request call available at this link and grab the ‘id’ from the response and use it in your DELETE Request Test in place where I am using ‘11400’.

Here is the code to send the DELETE request to the above mentioned Service Endpoint:

Let’s try to understand the code:

1. Specifying the Feature & Scenario (typical cucumber format)

2. Setting up Endpoint URI in ‘Given’

Endpoint URI is the address to the Resource. And, by this particular line of code, we are specifying to Karate to use “dummy.restapiexample.com/api/v1” as the root URL of the service.

3. Specifying the type of request (DELETE) in ‘When’ condition

4. Checking up Status Code & other Response validations in ‘Then’

a. status 200 : It will check the status code coming back from the service is 200

b. print ‘Response is: ‘, response : This line of code will print the response from the service in the console.

Eclipse Console Output:

5. Resources

intuit/karate: Test Automation Made Simple – GitHub

Testing Web Services with KARATE...!!! Click To Tweet

Checkout other useful tutorials (competitors of Karate), take a look:

Rest Assured Tutorial

Apache HttpClient Tutorial