Selecting the optimum tool for API automation testing is a tricky task. If you are looking to achieve
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:
To get started all you have to do is copy paste the below maven archtype in your terminal:
mvn archetype:generate \
Open the project in VS code and voila the framework structure is ready!
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.
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
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
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
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!
Dec 8, 2021