Skip to main content

Platforms and Technology

Simplifying API Testing: GET Requests Using Karate Framework

Diverse Team Working Together In Modern Co Working Space

The GET HTTP method is commonly used to retrieve data from a server. In this blog, we’ll explore how to automate GET requests using the Karate testing framework, a powerful tool for API testing that supports both BDD-style syntax and rich validation capabilities.

We’ll cover multiple scenarios starting from a simple GET call to advanced response validation using files and assertions.

Step 1: Creating the Feature File

To begin, create a new feature file named GetApi.feature in the directory:

/src/test/java/features

Ensure the file has a valid .feature extension. We’ll use the ReqRes API, a public API that provides dummy user data.

Scenario 1: A Simple GET Request

Feature: Get Api feature 
Scenario: Get API Request 
    Given url 'https://reqres.in/api/users?page=2' 
    When method GET 
    Then status 200 
    And print response

 

Feature: Get Api feature

  Scenario: Get API Request
    Given url 'https://reqres.in/api/users?page=2'
    When method GET
    Then status 200
    And print response

Step-by-Step Breakdown:

  • Sends a GET request to https://reqres.in/api/users?page=2
  • Asserts the status code is 200
  • Prints the response for visibility.

Sample Response:

{
  "page": 2,
  "per_page": 6,
  "total": 12,
  "data": [
    {
      "id": 7,
      "email": "michael.lawson@reqres.in",
      "first_name": "Michael",
      "last_name": "Lawson",
      "avatar": "https://reqres.in/img/faces/7-image.jpg"
    },
    {
      "id": 8,
      "email": "lindsay.ferguson@reqres.in",
      "first_name": "Lindsay",
      "last_name": "Ferguson",
      "avatar": "https://reqres.in/img/faces/8-image.jpg"
    }
    // additional data here...
  ]
}

If the expected status is changed, for example to 201, the scenario will fail:

GetApi.feature:11 - status code was: 200, expected: 201, response time: 1786, url: https://reqres.in/api/users?page=2

Scenario 2: GET Request with Background

Feature: Get Api feature

  Background:
    Given url 'https://reqres.in/api'
    And header Accept = 'application/json'

  Scenario: Get API Request with Background 
    Given path '/users?page=2'
    When method GET
    Then status 200
    And print responseStatus

The Background section allows us to define common settings such as URLs and headers once and reusing them across multiple scenarios.

Scenario 3: GET Request with Query Parameter

Feature: Get Api feature

  Background:
    Given url 'https://reqres.in/api'
    And header Accept = 'application/json'

  Scenario: Get API Request with Query Parameter 
    Given path '/users'
    And param page = 2
    When method GET
    Then status 200
    And match header Connection == 'keep-alive'
    And print "response time: " + responseTime

This approach explicitly adds query parameters using param, improving flexibility.

Output:

[print] response time: 1319

Scenario 4: Verifying the Response with Assertions

In Karate, assertions are used to validate the behavior of APIs and other test scenarios. These assertions help verify the response values, structure, status codes, and more. Karate provides several built-in functions for assertions, making it easy to validate complex scenarios.

The match keyword is the most common assertion method in Karate. It can be used to match:

  • Simple values (strings, numbers, etc.)
  • Complex objects (arrays, nested structures)

Karate allows rich assertion syntax using the match keyword. It supports:

  • Exact Matching

  • Partial Matching

  • Fuzzy Matching

  • Boolean Assertions

Examples:

Exact Matching

Scenario: Validate exact match 
Given url 'https://reqres.in/api/users/2' 
When method GET Then match response.data.first_name == 'Janet'

 

Scenario: Validate exact match
  Given url 'https://reqres.in/api/users/2'
  When method GET
  Then match response.data.first_name == 'Janet'

Partial Matching

Scenario: Validate partial match
  Given url 'https://reqres.in/api/users/2'
  When method GET
  Then match response contains { "data": { "first_name": "Janet" } }

Fuzzy Matching:

Fuzzy matching ignores extra fields and only focuses on the structure or specific fields:

Scenario: Fuzzy match example
  Given url 'https://reqres.in/api/users/2'
  When method GET
  Then match response == { data: { id: '#number', email: '#string' } }
  • #number and #string are placeholders that match any numeric or string values, respectively
  • Karate also supports using assert for boolean expressions. It evaluates the expression and returns true or false.

Combined Assertions

Feature: Get Api feature

  Background:
    * Given url 'https://reqres.in/api'
    * And header Accept = 'application/json'

  Scenario: Get API Request with Assertions
    Given path '/users'
    And param page = 2
    When method GET
    Then status 200
    And match response.data[0].first_name != null
    And assert response.data.length == 6
    And match response contains deep {"data":[ {"name": "blue turquoise"}]} 
    And match response contains deep {"text": "To keep ReqRes free, contributions towards server costs are appreciated!"}
    And match header Content-Type == 'application/json'<br />And match header Connection == 'keep-alive'

Scenario 5: Validating Responses Using External Files

File validation using the Karate framework can be achieved by verifying the content, existence, and attributes of files in various formats (e.g., JSON, XML, CSV, or even binary files). Karate provides built-in capabilities to handle file operations, such as reading files, validating file contents, and comparing files.

In this scenario:

  • We use the read function to read the data.json file from the classpath.
  • The match step is used to validate the content of the response against the expected data in the file.
Feature: Get Api feature
  
  Background: set up the base url
     * Given url 'https://reqres.in'  
     * And header Accept = 'application/json'
  
  Scenario: Get API Request with File Validation
    Given path '/api/users?page=2'
    When method get
    Then status 200
    * def actualResponse = read("../JsonResponse.json") // create a veriable to store the data from external json file
    And print 'File-->" , actualResponse
    And match response == actualResponse

In this session, we’ve demonstrated how to

  • Create a basic GET request in Karate

  • Use background steps for reusability

  • Pass query parameters

  • Assert and validate API responses

  • Leverage file-based validation

The Karate framework makes it easy to write expressive, maintainable, and powerful API tests. By combining built-in features like match, assert, and read, you can create robust test suites to ensure your APIs behave as expected.

By leveraging scenarios, parameters, and assertions, we can effectively automate and validate API requests.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Hemlata Sahu

Hemlata Sahu is a Technical Consultant at Perficient Nagpur. She is passionate about exploring new technologies and tools in testing. A creative problem solver, she has extensive experience in API automation using the Rest Assured BDD framework, Karate, API testing with POSTMAN, manual testing, and GCloud Pub/Sub testing. Hemlata is dedicated to staying at the forefront of the testing field and sharing her insights with others.

More from this Author

Follow Us