Karate Series | Session 2 | GET API

What we explore in this series

Karate Basics
GET API
POST API
PUT API
DELETE API
Karate Config
Karate Reports

We all know, GET is an HTTP method used to retrieve information from a source. In this session, we are going to see how a GET request can be automated using Karate.

First, we are going to write a new feature, namely, Get Api feature. Inside the feature, we will write a scenario. And inside that scenario, we will write all the steps that are to be executed when the scenario gets run. Either we can run the entire feature file as a whole or execute each scenario. All the modern IDEs support running each scenario separately.

We are going to save our feature file in /src/test/java/features directory as GetApi.feature. It’s important that all the feature files should have a valid file name with the extension .feature.

Since we are not creating any REST endpoint for this session, we are making use of https://reqres.in/api in this series, and as you can see this API supports almost all HTTP methods.

So let’s start off.

Scenario 1

A simple GET call.

Feature: Get Api feature

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

Let’s go through the steps of what we are going to achieve here.

  1. Sends a request to https://reqres.in/api users?page=2
  2. Defines the request type (GET in this case)
  3. Asserts the response status as 200
  4. Finally, prints the response

Here’s the response we received.

{
  "per_page": 6,
  "total": 12,
  "data": [
    {
      "last_name": "Lawson",
      "id": 7,
      "avatar": "https://reqres.in/img/faces/7-image.jpg",
      "first_name": "Michael",
      "email": "michael.lawson@reqres.in"
    },
    {
      "last_name": "Ferguson",
      "id": 8,
      "avatar": "https://reqres.in/img/faces/8-image.jpg",
      "first_name": "Lindsay",
      "email": "lindsay.ferguson@reqres.in"
    },
    {
      "last_name": "Funke",
      "id": 9,
      "avatar": "https://reqres.in/img/faces/9-image.jpg",
      "first_name": "Tobias",
      "email": "tobias.funke@reqres.in"
    },
    {
      "last_name": "Fields",
      "id": 10,
      "avatar": "https://reqres.in/img/faces/10-image.jpg",
      "first_name": "Byron",
      "email": "byron.fields@reqres.in"
    },
    {
      "last_name": "Edwards",
      "id": 11,
      "avatar": "https://reqres.in/img/faces/11-image.jpg",
      "first_name": "George",
      "email": "george.edwards@reqres.in"
    },
    {
      "last_name": "Howell",
      "id": 12,
      "avatar": "https://reqres.in/img/faces/12-image.jpg",
      "first_name": "Rachel",
      "email": "rachel.howell@reqres.in"
    }
  ],
  "page": 2,
  "total_pages": 2,
  "support": {
    "text": "To keep ReqRes free, contributions towards server costs are appreciated!",
    "url": "https://reqres.in/#support-heading"
  }
}

So, our first scenario got passed and we got the response as well. If we change the assertion, for example, the status to 201, our scenario will obviously fail, and the output would be something like this.

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

From the above text, it’s clearly understood that our scenario has been failed due to an incorrect response status code. What we expected was 201, but received 200.

Scenario 2

GET call with Background

Feature: Get Api feature

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

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

The Background is a block where we can put common things. The Background block will be executed during each scenario, so that we can declare global variables, set URLs, and even headers. In our example, we set the URL and header in the background, and the path is set in the scenario. So the actual request URL would be background URL + path.

We will get the same response as before when we run this scenario.

Scenario 3

GET call with query param

Feature: Get Api feature

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

  #With param
  Scenario: Get API demo 3
    Given path '/users'
    And param page = 2
    When method GET
    Then status 200
    And print "response time: " + responseTime

Here, we are executing the same query as before. The only difference here is, the param has been declared in an additional step. After all, Scenarios 2 & 3 are the same.

Here we have also printed response time as well. The output for the same is given below.

06:50:48.952 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] response time:1319

Scenario 4

Verify the response with assertions

Feature: Get Api feature

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

  #With Assertions
  Scenario: Get API demo 4
    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 $.data[3].id == 10

Like we usually do in Junit, we can assert and match things here also. The assert keyword can be used to assert an expression that returns a boolean value. Everything to the right of the assert keyword will be evaluated as a single expression.

The match operation is more elegant because the white-space does not matter here, and the order of keys (or data elements) does not matter. The match syntax involves a double-equals sign ‘==’ to represent a comparison. The dollar symbol ($) given in this example represents the response object.

The entire GetApi.feature file is given below.

Feature: Get Api feature

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

  #Without background
  Scenario: Get API demo 1
    Given url 'https://reqres.in/api/users?page=2'
    When method GET
    Then status 201
    And print response

  #With background
  Scenario: Get API demo 2
    Given path '/users?page=2'
    When method GET
    Then status 200
    And print responseStatus

  #With param
  Scenario: Get API demo 3
    Given path '/users'
    And param page = 2
    When method GET
    Then status 200
    And print "response time: " + responseTime

  #With Assertions
  Scenario: Get API demo 4
    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 $.data[3].id == 10

In this session, we’ve seen how to automate a GET request in Karate. That’s all for this session. Thank you!