API Automation using Java Karate Framework

Modified on Fri, 19 Jul at 7:24 PM

TABLE OF CONTENTS

1.Overview

2. Prerequisites

2.1 Installations

2.2 Adding dependencies in the POM file

3. Automating API leveraging algoQA

3.1 Example

3.2 Selecting TestStep Settings options in the Edit Scenario

4. Generating the scripts

5. Executing the scripts.


1.Overview

Karate is an open-source framework for API testing that integrates API testing with BDD (Behavior Driven Development). It allows you to write tests in a readable format using Gherkin syntax.

2. Prerequisites

2.1 Installations

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

2.2 Adding dependencies in the POM file

Perform the following:

  1. Navigate to the algoQA script generated folder structure.
  2. Click the POM file, as shown:
  3. 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>

3. API Test Automation leveraging algoQA

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

3.1 Example

Consider the following example to login, create a profile and delete a profile for a user using the following Feature names and HTTP Methods:

Feature Names
HTTP Methods to call API 
Login
POST
Create a profile
POST
Delete a profile 
DELETE

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

Perform the following:

  1. Navigate to the Main Canvas. Drag-and-drop the node and enter 'User management' as the name. 
  2. Add the following features as shown in the following screenshot:
  3. Click edit icon for 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 Name 
    Control Values 
    Login End Point url
    "https://algoshack.net/user/login/"
    Login Method
    POST
    Login Header
    Content-Type = 'application/json'
    Login Request body
    {"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 Output
    200
    Login store variable
        <accessToken> Here, replace with a actual variable.

    Sample screenshot:


  4. Similarly enter appropriate UI elements and Corresponding Control values for Create Project and Delete Project. 

  5. Record a scenario using Follow Me feature and generate Test Cases. Test cases are generated in the Test cases folder. For more information, see Profiling article. A system-generated feature file is as follows:

3.2 Selecting TestStep Settings options in the Edit Scenario

Perform the following; 

  1. Navigate to Edit scenario Form secondary screen by clicking the Edit icon of the first GWT step arranged in the sequence, select the 'no precondition' option from the TestStep Settings dropdown list (Login End Point url in this case), as shown:
  2. For the last step, select the 'no postcondition' option (in this case- Delete_Project_output) from TestStep Settings dropdown list, as shown:
  3. In the Scenario Sequence section, clear the GwtInput contents if it exists for all the GWT steps.

  4. You can add specific comments in the Comment textbox, which is specifically used for this framework. Click the plus icon in the Edit Scenario Form to view Add Comment tab. By default, Add Step tab will be displayed. Select Add  Comment tab to enable it and then select the tick mark. Finally, click Save and Back button.


4. Generating the scripts

Perform the following:

  1. Navigate to the Main Canvas, select Script Generation button.
  2. In the Project Explorer pane, navigate to Test Cases folder and select the generated Feature File and XLS file.
  3. Click Analyze and Proceed to view the following window:
    For more information on Generating scripts, refer to Generate and Execute Scripts  in the How to Automate Web Applications article.

5. Executing the scripts.

  1. Navigate to the script generated folder in the Project Explore pane. 
  2. Download the script generated to your local folder.
  3. Extract the contents to your local drive. 
  4. Open the command prompt from the same folder where you have extracted the script. 
  5. Run the following command to execute the script:
    mvn test -Dkarate.options="--tags @test001
  6. You can view the respective report in the script generated folder, as shown: 















 


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article