Test Automation using Karate Framework (part 1)

Sankara katabathina
5 min readSep 13, 2020

In this Blog, I would like to discuss the overview about Karate Framework and internals of Karate will be discussed in the next blog post.

I am sure many people would have used Cucumber for some of the Test automation or Developing BDD style step execution process.

I recently got an opportunity to work in Functional Test automation for API in my project. we discussed internally as a team and decided to use Cucumber with Rest Assured for automating the functional test cases. we informed the same to our project director who is extremely knowledgeable and a great professor, so we were excited to hear his feedback on our choice.

He suggested karate framework which is new to me . My first feeling was, i like watching Karate but how does Karate fight help testing the API 😃

Immediately i started exploring Karate and its capabilities and Found that It is a great framework and made developers and testers life so easy in automating test scenarios, and yes of-course it will have little bit of learning curve and i am sure one would not be disappointed learning it.

In this Blog, I would like to discuss the overview about Karate Framework and internals of Karate will be discussed in the next blog post.

I am sure many people would have used Cucumber for some of the Test automation or Developing BDD style step execution process.

I recently got an opportunity to work in Functional Test automation for API in my project. we discussed internally as a team and decided to use Cucumber with Rest Assured for automating the functional test cases. we informed the same to our project director who is extremely knowledgeable and a great professor, so we were excited to hear his feed on our choice.

Immediately he pitched in and suggested karate framework which is new to me . My first feeling was, i like watching Karate but how does Karate fight help testing the API 😃

Immediately i started exploring Karate and its capabilities and Found that It is a great framework and made developers and testers life so easy in automating test scenarios, Thank you Peter (Director). and yes of-course it will have little bit of learning curve and i am sure one would not be disappointed learning it.

What is Functional Test Automation ?

Automated Testing is a complex subject in Software Development and usually not very prioritized, but once we start enhancing the projects and when they become big enough to manage, we will right away get to know the importance of Automation.

Testing Automation would save numerous hours of Testers and Developers time. Automating the Functional Test will help simplify the integration and regression testing in agile environment where many small releases going in by assuring the quality of product.

what is Cucumber Framework ?

Functional Testing is more meaningful when the platform allows all the stake holds starting from users, managers, testers and developers to participate in documenting the Functionalities of the API or a product. Using the same document for Testing functionality after completing the development. This requires a common framework to write the test scenarios in ubiquitous language.

Cucumber helps documenting Test scenarios in Behavior Driven Development(BDD) style which uses the natural language parser named Gherkin. we can write Gherkin steps in many human readable languages like English, French etc.

Cucumber has Three step process as pictured below :

step1: Parser the Gherkin step.

step2: call the function implemented for the step.

step 3: manipulate the end system and return the results.

An Example explaining the cucumber process :

Feature: Cell Site information API Functionality Testing  Scenario: Compare Cell Site information
Given api end point url
And authentication details
And urlpath parameters
When request method is GET
Then check the returned status is 200
And compare the expected response with actual response

There are many tools and frameworks available to automate different phases of SDLC using cucumber. Two popular frameworks for API Testing automation are Karate and Rest Assured.

Using Rest Assured Developers/Testers need to write Step definition in Domain Specific language for each of these steps to perform some action in the system. This would take more time to implement when we have many API endpoints in a bigger system. This can be eliminated using Karate.

What is Karate ?

Karate is an open source framework used for automating API Testing, Unit Test mocks, performance-testing even UI testing and developed to work with BDD style Cucumber framework.

Advantages of using Karate in API Functional Testing automation :

  • One framework for automating API Testing, Unit Test mocks, performance-testing even UI testing
  • Eliminates Cucumber Step definition coding effort, so it could save lot time to develop and automate.
  • Powerful JSON & XML match and assertion are built in, which makes us to work with data and configuration values in creating Test Scenarios with feasibility.
  • Easy to work with JavaScript and Java functions, we can make use of this functionality when we need to write a complex logic to full fill the Test scenarios.
  • Parallel test execution, enable the test scenarios to run in parallel which would be very useful when running automation in CI/CD pipeline.

Example of API Functional Test Scenario in Karate

Feature: API GetResponse Status check  Background:
* url baseUrl
* configure headers = {'apiKey' : '#(apiKey)', 'Content-Type': '#(acceptType)'}
* def expectedData = read('data/expectedresponse.json')
Scenario Outline: : API GetResponse Status Scenario
Given path ''
* def pathparams =
* if (pathparams.startDate != null) pathparams.startDate = yesterday
* if (pathparams.endDate != null) pathparams.endDate = yesterday
And params pathparams
When method GET
Then status
* match each response.result[*] == expectedresult.result[0]
Examples:
| testid | path | pathparams | expstatus |
| "test01_status5g" | cell/5g | {market : 'SEATTLE'} | 200 |
| "test04_statuslte" | cell/lte | {} | 400 |
| "test05_statuslte" | cell/ | {market : 'SEATTLE'} | 405 |

Conclusion…

Many API’s are developed to exchange data between applications and microservices. So it is highly important to adopt and enhance Karate framework for Functional Test automation. This would help find the issues related to functionality with less effort in the early stages of SDLC and provide very good experience for the API users.

Reference docs:

https://github.com/intuit/karate

--

--