Skip to main content

GET STARTED

Examples

Learn Karate through practical, real-world examples that demonstrate core concepts and best practices.

Basic API Test

A simple REST API test example:

Gherkin
Feature: Users API


Background:
* url 'https://jsonplaceholder.typicode.com'

Scenario: Get user by id
Given path 'users', 1
When method get
Then status 200
And match response.name == 'Leanne Graham'
And match response.email == 'Sincere@april.biz'

POST Request with JSON

Creating a resource with JSON payload:

Gherkin
Feature: Create a demo post

Scenario: Create post
Given url 'https://api.restful-api.dev/objects'
And request
"""
{
"name": "Karate Demo Post",
"data": {
"type": "blog-post",
"createdBy": "Karate DSL"
}
}
"""
When method post
Then status 200
And match response.name == 'Karate Demo Post'
And match response.id == '#string'
And match response.data.type == 'blog-post'
And match response.data.createdBy == 'Karate DSL Author'

Data-Driven Testing

Using a data table for multiple test cases:

Gherkin
Feature: Verify user emails from JSONPlaceholder

Scenario Outline: Verify user emails
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', <id>
When method get
Then status 200
And match response.email == '<email>'

Examples:
| id | email |
| 1 | Sincere@april.biz |
| 2 | Shanna@melissa.tv |
| 3 | Nathan@yesenia.net |

Response Validation

Advanced response matching:

Gherkin
Feature: Get User by ID with complex validation

Scenario: Get User by ID
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
And match response ==
"""
{
id: '#number',
name: '#string',
username: '#notnull',
email: '#regex (?i)[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}',
address: {
street: '#string',
suite: '##string',
city: '#present',
zipcode: '#regex [0-9]{5}-[0-9]{4}',
geo: {
lat: '#string',
lng: '#string'
}
},
phone: '#regex .*',
website: '#string',
company: {
name: '#string',
catchPhrase: '#ignore',
bs: '#string'
},
}
"""

Reusable Features

Calling another feature file:

Gherkin
Feature: Reuse a feature to create and verify an object

Background:
* url 'https://api.restful-api.dev'
* def createPost = call read('postRequest.feature')
* def postId = createPost.response.id

Scenario: Verify created post
Given path 'objects', postId
When method get
Then status 200
And match response.name == 'Karate Demo Post'
And match response.data.createdBy == 'Karate DSL Author'

Working with Headers

Managing request headers:

Gherkin
Feature: Working with Headers

Background:
* url 'https://httpbin.org'
* def token = 'dummy-token-xyz789'
* header Authorization = 'Bearer ' + token
* header X-API-Key = 'dummy-api-key-123'
* header User-Agent = 'Karate/1.5.0'

Scenario: API call with custom headers
Given path 'headers'
When method get
Then status 200
And match response.headers['X-Api-Key'] == 'dummy-api-key-123'
And match response.headers['User-Agent'] == 'Karate/1.5.0'
And match response.headers['Authorization'] == 'Bearer dummy-token-xyz789'

File Upload

Multipart file upload example:

Gherkin
Feature: File Upload

Scenario: Upload a document with additional fields
Given url 'https://httpbin.org'
And path 'post'
And multipart file myFile = { read: 'sample.pdf', filename: 'sample.pdf', contentType: 'application/pdf' }
And multipart field description = 'Test document upload via Karate'
When method post
Then status 200
And match response.files.document == '#notnull'
And match response.form.description == 'Test document upload via Karate'

Retry Until

Polling for status changes:

Gherkin
Feature: Retry Until a condition is met

Background:
* url 'https://httpbingo.org'

Scenario Outline: Wait for simulated processing to complete
Given path 'delay', jobId
And retry until responseStatus == 200
When method get
Then match response.url contains + jobId

Examples:
| jobId |
| 3 |
| 5 |
| 7 |

Next Steps