Overview
In this tutorial, we will test GET API Request with Karate DSL. Get Request is the simple API call to test both in manual mode and in Automation Testing.
Test GET API Request
Get request is used to retrieve information of the server side resource. The Gherkin for the Get Mantis Issue API call is shown below:
To specify the API end point:
Given url ‘<Get_API_URL_Endpoint>’
To add HTTP Header
And header Authorization = ‘<API_Token>’
To specify the HTTP GET method:
When method GET
To verify the API Response status:
Then status 200
response is the variable that is set with the Response body. This variable will be refreshed for every HTTP call with the latest Response body.
response.projects[0].name is JsonPath expression that we will discuss later. We are asserting for the Project name in the response body to decide the API test status for Pass/Fail. The test would be marked as Pass if the HTTP Status code == 200 AND Response Body contains the given Project name in the API call.
Feature Gherkin BDD Syntax
## Karate API Tool Demo #Karate Framework Tutorials - www.TestingDocs.com @Mantis Feature: Get Project REST API Request I want to test Get Project Mantis Bug Tracker API Scenario: GET Project Test Given url 'http://localhost/mantis/api/rest/projects/1' And header Authorization = 'DoZSsCCiTFEx8jIm67F4mM7RWh_NLEVd' When method GET Then status 200 Then match response.projects[0].name == 'TestingDocsProject'
Common Failure cases are
404 Not Found -> When the API endpoint is not deployed or not exposed. Invalid API URL.
401 Un authorized -> Wrong API token or missing token
—
Karate Automation Tutorials
Karate tool tutorials on this website can be found at:
https://www.testingdocs.com/karate-automation-tool-tutorials/
More information on the tool:
https://github.com/intuit/karate