Skip to main content

EXTENSIONS

Examples and Demos

Explore production-ready Karate examples and integration patterns from the official repository to accelerate your test automation.

Why Learn from Examples

  • Copy-paste ready code - All examples are tested and work out of the box
  • Real-world patterns - See how to handle authentication, file uploads, and API integration
  • Best practices - Learn from production implementations used by teams worldwide

Example Repositories

The karate-examples GitHub repository contains comprehensive examples of integrations with popular frameworks and real-world testing patterns.

The karate-demo project showcases complete test suites with working API examples.

Simple API Tests

Start with basic API testing patterns for common scenarios.


Feature: User API

Scenario: Get user by ID
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
And match response.name == '#string'
And match response.email == '#string'
And match response.address.city == '#string'
Key Points
  • No Java code or step definitions required
  • Built-in assertions with match
  • JSON-first syntax without escape characters

Authentication Patterns

OAuth 2.0 Flow

Real-world OAuth 2.0 authentication example.


Feature: OAuth 2.0 Authentication

Background:
* url authServerUrl
* def tokenResponse = call read('classpath:oauth/get-token.feature')
* def accessToken = tokenResponse.response.access_token

Scenario: Access protected resource
Given url apiUrl
And path 'api/protected/users'
And header Authorization = 'Bearer ' + accessToken
When method get
Then status 200
And match response[0] contains { id: '#number', name: '#string' }

See the complete OAuth 2.0 demo in the repository: oauth2.feature

File Operations

File Upload

Handle multipart file uploads with custom metadata.


Feature: File Upload

Scenario: Upload PDF with metadata
Given url baseUrl
And path 'files/upload'
And multipart file myFile = { read: 'test.pdf', filename: 'report.pdf', contentType: 'application/pdf' }
And multipart field message = 'Monthly report'
When method post
Then status 200
And match response == { id: '#uuid', status: 'uploaded', size: '#number' }

For multiple file uploads and advanced patterns, see upload.feature

Integration Examples

GraphQL Testing

Karate excels at testing GraphQL with dynamic nested JSON responses.


Feature: GraphQL API

Scenario: Query with variables
Given url 'https://api.example.com/graphql'
And text query =
"""
query getUser($id: ID!) {
user(id: $id) {
id
name
email
posts {
title
}
}
}
"""
And def variables = { id: 'user123' }
And request { query: '#(query)', variables: '#(variables)' }
When method post
Then status 200
And match response.data.user.name == '#string'
And match response.data.user.posts == '#[]'

Complete GraphQL examples: graphql.feature

SOAP Services

Test SOAP web services with XML payloads.


Feature: SOAP Web Service

Scenario: SOAP request with XML
Given url 'http://www.dneonline.com/calculator.asmx'
And request
"""
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Add xmlns="http://tempuri.org/">
<intA>5</intA>
<intB>3</intB>
</Add>
</soap:Body>
</soap:Envelope>
"""
When soap action 'http://tempuri.org/Add'
Then status 200
And match /Envelope/Body/AddResponse/AddResult == '8'

More SOAP examples: soap.feature

Data-Driven Testing

Dynamic Parameters

Test multiple scenarios with varying parameters.


Feature: Search API

Scenario Outline: Search by different criteria
Given url 'https://api.example.com/search'
And param q = '<query>'
And param type = '<type>'
When method get
Then status 200
And match response.results == '#[<count>]'

Examples:
| query | type | count |
| laptop | product | 10 |
| smartphone | product | 15 |
| tablet | product | 8 |

Advanced dynamic testing: dynamic-params.feature

Framework Comparisons

Karate vs REST-assured

For teams evaluating Karate, here is a detailed comparison with REST-assured highlighting key differences.

Key advantages of Karate:

  • No Java glue code - Write tests directly in feature files
  • JSON-first syntax - Express payloads without escaping or quotes
  • Built-in assertions - Comprehensive match syntax for all data types
  • Parallel execution - Run scenarios in parallel by default
  • Java API available - Use Karate programmatically if preferred

Karate vs Cucumber

Unlike traditional Cucumber, Karate does not require step definitions.


# Traditional Cucumber (requires Java step definitions)
Scenario: Get user
Given the API is available
When I request user with id 1
Then the response status should be 200
And the user name should not be empty

# Karate (no step definitions needed)
Scenario: Get user
Given url baseUrl
And path 'users/1'
When method get
Then status 200
And match response.name == '#string'

Key differences:

  • No step definitions - All HTTP, JSON, and XML steps are built-in
  • Single layer - Only feature files, no separate Java code
  • Re-usable features - Call other feature files as functions
  • Dynamic data-driven - Use JSON arrays as data sources
  • Parallel by default - Scenarios run in parallel automatically

Complete Test Suite Example

E-Commerce API Testing

Production-ready test suite with authentication, CRUD operations, and validation.


Feature: E-Commerce User Journey

Background:
* url baseUrl
* def authToken = call read('classpath:auth/login.feature').token
* header Authorization = 'Bearer ' + authToken

Scenario: Complete purchase flow
# Create user cart
Given path 'cart'
And request { userId: '#(userId)' }
When method post
Then status 201
* def cartId = response.id

# Add product to cart
Given path 'cart', cartId, 'items'
And request { productId: 'prod-123', quantity: 2 }
When method post
Then status 200

# Get cart total
Given path 'cart', cartId
When method get
Then status 200
And match response.total == '#number'
And match response.items == '#[1]'

# Checkout
Given path 'orders'
And request { cartId: '#(cartId)', paymentMethod: 'credit_card' }
When method post
Then status 201
And match response contains { orderId: '#string', status: 'confirmed' }

Community Resources

Videos and Tutorials

Books and Courses

Community

Next Steps