Site icon TestingDocs.com

Testing POST Request with Karate Framework

Overview

In this tutorial, we will test POST Rest API Endpoint with Karate Framework. We will use the Mantis Bug Tracker Restful API. We will create an issue with Post Rest API call to the local Bug tracker instance using Automation.

Mantis Rest API Guide.

https://www.testingdocs.com/mantisbt-rest-api-guide/

Steps to create Karate Automation Project with Maven Build Tool

https://www.testingdocs.com/create-karate-automation-project/

POST API Request

HTTP Post method is used to create server-side resources, create side effects or specify relations between the resources. A successful API call would get 201 Response code.

The POST method can be specified using the Karate DSL with BDD Gherkin syntax. The syntax would be similar to Cucumber tool.  The step definitions are already defined by the tool. We just need to add the Gherkin as shown below:

HTTP POST method step can be specified as:

When method POST

API Header step can be specified as:

And header Authorization = ‘<API_Token>’

Request body step can be specified as:

And request <request body>

The complete Gherkin feature for the POST call is displayed below:

Sample Feature with Karate DSL

#Sample Feature Definition Template

Feature: Post Mantis Issue
I want create an issue using POST Mantis API call

@Mantis
Scenario: Create MantisBT Issue
Given url 'http://localhost/mantis/api/rest/issues/'
And header Authorization = 'DoZSsCCiTFEx8jIm67F4mM7RWh_NLEVd'
And request { "summary": "This issue created using Karate 
Tool-JUnit5 Demo",
 "description": "[TestingDocs.com]Issue Description sample test bug API",
 "category": { "name": "General" }, "project": 
{ "name": "TestingDocsProject" } }
When method POST
Then status 201

 

Additionally verification steps can be added to verify the POST request API Response. Run the feature and check the Mantis bug tracker web instance in the browser. An issue would be created in the project specified in the API request.

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

Exit mobile version