Karate Framework for API testing

Selecting the optimum tool for API automation testing is a tricky task. If you are looking to achieve

  • parallel testing,
  • mock testing,
  • data driven testing,
  • API testing,
  • UI automation,
  • performance testing by integration with gatling

all of the above with one open source tool, then karate framework is the most suitable option.

Karate is based on java. It requires jdk 1.8 and maven 3.6.X as prerequisites.

Use Visual Studio code as the IDE with two plugins:

  • Open default browser.
  • Cucumber with full gherkin support.

To get started all you have to do is copy paste the below maven archtype in your terminal:

mvn archetype:generate \

-DarchetypeGroupId=com.intuit.karate \

-DarchetypeArtifactId=karate-archetype \

-DarchetypeVersion=1.1.0 \

-DgroupId=com.mycompany \

-DartifactId=myproject

Open the project in VS code and voila the framework structure is ready!

Karate supports both java and javascript but you don’t need a prior understanding on either languages to start coding. The test cases are extremely easy to develop as it uses Gherkin keywords with BDD syntax i.e; Given, And, When, Then.

This makes is convenient to be understood by all types of audience be it technical or non-technical. Knowledge transition and onboarding of new resources too is a simple task.

Framework Structure:

POM.xml: consists of the maven, junit and karate dependencies by default post karate installation.

Karate-config.js: here is where you can declare the constants like, auth, base url, env, reusable code that needs to be utilized by all feature files.

TestRunner.java: has the template for running all the feature files at once, you can also mention the tags here to run a selected group of scenarios.

src/test/java: this will hold the package, which can be further divided into two folders: features and resources

features →user.feature

resources → payloads → addUser.json patchUser.json updateUser.json

The feature file is where you have to do the development of scenarios.

The file name must have the extension “.feature”, say for example user.feature

Here is an example of a feature file with two scenarios.

You can go ahead and add multiple scenarios and multiple feature files as per your requirement.

Now you are good to do the test execution.

Command for test execution:

1. Use below command to run all feature files in a particular test environment:

mvn clean test -Dkarate.env=QA

OR

2. Use below command to run testcases tagged as “smoke” in a particular test environment

mvn clean test “-Dkarate.options= — tags @smoke” -Dkarate.env=QA

Report:

The target  karate-reports folder in the framework structure will store the summary report post test execution completion.

File name: karate-summary.html

You can right click on it and choose the “open in default browser” option and a colorful report will open stating the feature, scenarios , pass, fail, time taken to execute.

On clicking the feature it will expand to show all the scenarios in detail.

For failed scenario the report looks like this:

Built-in report is the best part about the framewok you don’t need any third party tool or plugin or any extra code for report generation.

There you go, framework for basic API testing is ready!

References:

https://github.com/karatelabs/karate

Clients:

Category:

Tech

Date:

Dec 8, 2021