API Test Automation Using Java KarateAPI Automation using Java Karate Framework

API Automation using Java Karate Framework

Automate API testing with the Java Karate framework in algoQA — install prerequisites, configure API nodes, generate test cases and scripts, and execute them to view detailed reports.

The Java Karate framework enables API test automation within algoQA using Gherkin-based syntax. This guide covers installing prerequisites, configuring API nodes, generating test cases and scripts, and executing them to view reports.

Prerequisites

Installations

Install Java and Maven by following the steps described in Configuring Web Applications Using Java Selenium Framework.

Adding dependencies in the POM file

Perform the following:

Navigate to the algoQA script generated folder structure.

Click the POM file.

Project folder view with pom.xml file highlighted among src, lib and target folders
Project folder view with pom.xml file highlighted among src, lib and target folders

Make sure to create a Maven Project if it has not been created earlier and add the following dependencies in the POM file:

<dependency>
  <groupId>com.intuit.karate</groupId>
  <artifactId>karate-core</artifactId>
  <version>1.4.0</version>
</dependency>
<dependency>
  <groupId>com.intuit.karate</groupId>
 <artifactId>karate-apache</artifactId>
 <version>0.9.6</version>
 <scope>test</scope>
</dependency>
<dependency>
 <groupId>com.intuit.karate</groupId>
 <artifactId>karate-junit4</artifactId>
 <version>1.3.1</version>
 <scope>test</scope>
</dependency>

API Test Automation Leveraging algoQA

This section describes how to automate an API by leveraging the algoQA platform.

Example

Consider the following examples to log in, create a profile, and delete a profile for a user using the following Feature names and HTTP Methods:

Feature NamesHTTP Methods to call API
LoginPOST
Create a profilePOST
Delete a profileDELETE

In this automation, configure the nodes, corresponding features, and UI elements as described in the following steps:

Perform the following:

Navigate to the Canvas.

Add node and enter 'User management' as the name.

Add Login, Create Project and Delete Project features in Test Design.

algo karate Configuration page with Login feature controls table showing endpoint, method and headers
algo karate Configuration page with Login feature controls table showing endpoint, method and headers

Click the Login feature and add the following control names and control values as shown in the following table.

Ensure to select Text Box as the Control Type for all the UI elements that you are providing:

Control/UI element NameControl Values
Login End Point url"https://algoshack.net/user/login/"
Login MethodPOST
Login HeaderContent-Type = 'application/json'
Login Request body&#123;"user":{"email":"<username>@gmail.com", "password":"encrypted format"}}. Here, replace "<username>@gmail.com" with the actual user's email ID and "encrypted format" with the actual user's encrypted password.
Login Output200
Login store variable<accessToken> Here, replace with an actual variable.

Sample screenshot:

Controls tab listing Login End Point url, Method, Header, Request body, Output and store variable fields
Controls tab listing Login End Point url, Method, Header, Request body, Output and store variable fields

Similarly, enter the appropriate UI elements and corresponding control values for Create Project and Delete Project.

Record a scenario using Offline Recorder and generate Test Cases.

Test cases are generated in the Test Cases tab. For more information, see Record a Scenario using Offline Recorder article. The feature file can be viewed in Test Cases and a unique ID will be generated for each test case.

Test Cases Editor showing algo karate.txt feature file with login scenario and API request
Test Cases Editor showing algo karate.txt feature file with login scenario and API request

Selecting Test Step Settings options in the Scenario Steps List

Perform the following:

Navigate to Scenarios List>>Scenario Steps List>>Edit Step Form secondary screen by clicking the Edit icon of the first GWT step arranged in the sequence, select the 'no precondition' option from the Test Step Settings dropdown list and save (Login End Point url in this case).

Edit Step dialog with Control Name Login End Point url, TestStep Settings set to no precondition, Save button
Edit Step dialog with Control Name Login End Point url, TestStep Settings set to no precondition, Save button

For the last step, select the 'no postcondition' option (in this case- Delete_Project_output) from Test Step Settings dropdown list.

In the Scenario Steps List, clear the Gwt Input contents if it exists for all the GWT steps.

You can add specific comments in the Comment textbox, which is specifically used for this framework.

Click the '+' icon beside the GWT step and click Comment to view the comment space. Add a comment and click Save.

Add Step dialog with Comment tab selected, Feature Name and Control Name fields, and comment text box
Add Step dialog with Comment tab selected, Feature Name and Control Name fields, and comment text box

Generating Test Cases and Test Scripts

Perform the following:

  1. Click Generate from the Scenario List screen and generate scripts either from test case generation pop-up window or from test cases tab.

  2. Navigate to Test Cases, click Generate Script and select the generated Feature File and XLS file.

    Scripts generation pop-up appears from where you can view and download the scripts.

For more information on generating scripts, refer to the Generate Test Cases and Test Scripts article.

Executing the Scripts

Download the generated script to your local folder.

Extract the contents to your local drive.

Open the command prompt from the same folder where you have extracted the script.

Run the following command to execute the script:

mvn test -Dkarate.options="--tags @test001"

You can view the respective report once the script is executed.

Karate Labs report showing algoQA API flow scenario with 0 passed, 1 failed, and request details
Karate Labs report showing algoQA API flow scenario with 0 passed, 1 failed, and request details