API Automation in Karate DSL
Karate DSL is a powerful open-source framework for API testing that combines API automation, data-driven testing, and BDD (Behavior-Driven Development) style syntax in one package. Unlike tools like RestAssured or Postman, Karate requires no Java coding to write tests—it's written in a readable .feature file format using Gherkin.
π Why Use Karate for API Automation?
✅ BDD-style syntax (readable and maintainable)
✅ Built-in HTTP client (no need for external libraries like RestAssured)
✅ Supports JSON, XML, SOAP, file uploads, and GraphQL
✅ Supports data-driven and parallel testing
✅ Easy integration with CI/CD pipelines
π¦ Basic Project Setup
1. Maven Dependency
Add this to your pom.xml:
xml
Copy
Edit
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>1.4.1</version> <!-- Use latest -->
<scope>test</scope>
</dependency>
π§ͺ Basic API Test Example
File: src/test/java/examples/api/getUser.feature
gherkin
Copy
Edit
Feature: Get user details
Scenario: Get user by ID
Given url 'https://jsonplaceholder.typicode.com/users/1'
When method GET
Then status 200
And match response.name == 'Leanne Graham'
π POST Request with JSON Payload
gherkin
Copy
Edit
Feature: Create a new user
Scenario: Create user
Given url 'https://jsonplaceholder.typicode.com/users'
And request { name: 'John Doe', email: 'john@example.com' }
When method POST
Then status 201
And match response.name == 'John Doe'
π Data-Driven Testing with Examples
gherkin
Copy
Edit
Feature: Login API test
Scenario Outline: Login with multiple users
Given url 'https://example.com/api/login'
And request { username: '<user>', password: '<pass>' }
When method POST
Then status 200
Examples:
| user | pass |
| user1 | pass123 |
| user2 | pass456 |
π Running Tests
With Maven:
bash
Copy
Edit
mvn test -Dtest=examples.api.getUser
With IntelliJ or Eclipse:
Right-click on the .feature file > Run as JUnit Test
⚙️ Karate Config & Variables
karate-config.js
Used to set environment variables:
js
Copy
Edit
function fn() {
var config = {
baseUrl: 'https://jsonplaceholder.typicode.com'
};
return config;
}
Use it in your tests:
gherkin
Copy
Edit
Given url baseUrl + '/users/1'
π Authentication Support
Basic Auth:
gherkin
Copy
Edit
Given url baseUrl + '/secure'
And header Authorization = call read('classpath:basic-auth.js')
Bearer Token:
gherkin
Copy
Edit
And header Authorization = 'Bearer ' + token
π Advanced Features
Assertions: match, contains, ==, !=
File upload: multipart file
Call other features: call read('someFeature.feature')
JavaScript support for custom logic
HTML reporting (default JUnit-style)
π Directory Structure Example
arduino
Copy
Edit
src/
└── test/
└── java/
└── examples/
└── api/
├── getUser.feature
├── postUser.feature
└── karate-config.js
π§ Summary
Feature Karate DSL Capability
REST/SOAP Support ✅ Yes
JSON & XML Handling ✅ Built-in
Parallel Execution ✅ Supported via Maven config
Data-Driven Testing ✅ Scenario Outline + Examples
CI/CD Integration ✅ Easily integrated with Jenkins/GitHub
Java Knowledge Required ❌ Minimal or none
Learn Testing Tools Training in Hyderabad
Read More
Building a CI/CD Pipeline with Selenium and Jenkins
Performance Testing with JMeter: Advanced Tips
Creating Custom Cypress Plugins
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment