Skip to content

This repository demonstrates API automation testing using the Karate Framework. It includes test scripts for RESTful APIs to validate functionality, response structures, and performance. The project showcases how Karate simplifies API testing with its BDD-style syntax and built-in support for JSON, XML, and HTTP calls.

Notifications You must be signed in to change notification settings

adityadwic/karateFramework-API-automation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ₯‹ Karate Framework API Automation

Karate Java Maven Test Status

Modern API automation framework using Karate Framework 1.4.1 with Page Object Model (POM) pattern for testing automation exercise APIs. Features comprehensive test coverage, multiple execution strategies, and detailed HTML reporting.

🌟 Key Features

  • βœ… Karate Framework 1.4.1 with GraalVM JavaScript engine support
  • βœ… Page Object Model (POM) pattern for maintainable test structure
  • βœ… Multiple Test Runners for different execution scenarios
  • βœ… Tag-based Test Execution for targeted testing
  • βœ… Comprehensive API Validation (status, structure, data types)
  • βœ… Negative Testing for error scenario coverage
  • βœ… Rich HTML Reports with detailed request/response logs
  • βœ… JUnit 5 Integration for modern testing framework
  • βœ… Environment Configuration support

πŸ“ Project Structure

karateFramework-API-automation/
β”œβ”€β”€ src/
β”‚   └── test/
β”‚       └── java/
β”‚           β”œβ”€β”€ api/
β”‚           β”‚   β”œβ”€β”€ products/
β”‚           β”‚   β”‚   └── ProductsApi.feature         # Products API test scenarios
β”‚           β”‚   β”œβ”€β”€ brands/
β”‚           β”‚   β”‚   └── BrandsApi.feature           # Brands API test scenarios
β”‚           β”‚   β”œβ”€β”€ runners/
β”‚           β”‚   β”‚   β”œβ”€β”€ ApiTestRunner.java          # All API tests runner
β”‚           β”‚   β”‚   β”œβ”€β”€ SmokeTestRunner.java        # Smoke tests runner
β”‚           β”‚   β”‚   β”œβ”€β”€ RegressionTestRunner.java   # Regression tests runner
β”‚           β”‚   β”‚   └── SimpleKarateRunner.java     # Direct execution runner
β”‚           β”‚   β”œβ”€β”€ utils/
β”‚           β”‚   β”‚   └── common-utils.js             # Utility functions
β”‚           β”‚   └── data/
β”‚           β”‚       └── README.md                   # Test data documentation
β”‚           └── karate-config.js                    # Global configuration
β”œβ”€β”€ target/
β”‚   └── karate-reports/                             # Generated test reports
β”œβ”€β”€ pom.xml                                         # Maven configuration
└── README.md                                       # This file

🌐 APIs Under Test

1. Products API

  • Endpoint: https://automationexercise.com/api/productsList
  • Method: GET
  • Description: Retrieves complete products catalog with details
  • Expected Response: 200 OK with JSON array of products
  • Test Coverage: Success scenarios, data validation, negative testing

Sample Response:

{
  "responseCode": 200,
  "products": [
    {
      "id": 1,
      "name": "Blue Top",
      "price": "Rs. 500",
      "brand": "Polo",
      "category": {
        "usertype": {"usertype": "Women"},
        "category": "Tops"
      }
    }
  ]
}

2. Brands API

  • Endpoint: https://automationexercise.com/api/brandsList
  • Method: GET
  • Description: Retrieves all available brands information
  • Expected Response: 200 OK with JSON array of brands
  • Test Coverage: Success scenarios, data validation, negative testing

Sample Response:

{
  "responseCode": 200,
  "brands": [
    {
      "id": 1,
      "brand": "Polo"
    }
  ]
}

🏷️ Test Categories & Tags

Tag Purpose Description
@smoke Critical Tests Essential scenarios for basic functionality
@regression Full Coverage Comprehensive test suite for all features
@negative Error Testing Invalid inputs and error scenario validation
@products Products API All test scenarios related to products endpoint
@brands Brands API All test scenarios related to brands endpoint

⚑ Quick Start

Prerequisites

  • β˜‘οΈ Java 11+ - Required for Karate 1.4.1
  • β˜‘οΈ Maven 3.6+ - Build and dependency management
  • β˜‘οΈ IDE (IntelliJ IDEA, VS Code, Eclipse) - For development

Installation

  1. Clone the repository

    git clone https://github.com/adityadwic/karateFramework-API-automation.git
    cd karateFramework-API-automation
  2. Install dependencies

    mvn clean compile test-compile
  3. Verify setup

    mvn test

πŸš€ Running Tests

Option 1: Maven Commands

Run All Tests

mvn test

Run Specific Test Runners

# Run smoke tests only (critical scenarios)
mvn test -Dtest=SmokeTestRunner

# Run regression tests only (full coverage)
mvn test -Dtest=RegressionTestRunner

# Run all API tests (comprehensive)
mvn test -Dtest=ApiTestRunner

Run by Tags (Karate Options)

# Run smoke tests
mvn test -Dkarate.options="--tags @smoke"

# Run regression tests
mvn test -Dkarate.options="--tags @regression"

# Run negative tests only
mvn test -Dkarate.options="--tags @negative"

# Run product tests only
mvn test -Dkarate.options="--tags @products"

# Run brand tests only
mvn test -Dkarate.options="--tags @brands"

# Exclude specific tags
mvn test -Dkarate.options="--tags ~@ignore"

Option 2: Direct Java Execution

Run tests directly using the SimpleKarateRunner:

# Compile first
mvn clean compile test-compile

# Run with Java (includes classpath dependencies)
mvn exec:java -Dexec.mainClass="api.runners.SimpleKarateRunner"

Option 3: IDE Execution

  • IntelliJ IDEA: Right-click on any runner class β†’ Run
  • VS Code: Use Java Test Runner extension
  • Eclipse: Right-click on runner β†’ Run As β†’ JUnit Test

πŸ“Š Test Results & Reporting

Console Output

Tests provide real-time feedback with emojis and color coding:

πŸš€ Running Karate API Tests...
βœ… Smoke Tests Results:
Features Total: 2, Features Passed: 2, Features Failed: 0
Scenarios Total: 6, Scenarios Passed: 6, Scenarios Failed: 0
πŸŽ‰ All tests passed! Karate Framework is working perfectly!

HTML Reports

After execution, detailed reports are generated in:

target/karate-reports/
β”œβ”€β”€ karate-summary.html          # Main report dashboard
β”œβ”€β”€ api.products.ProductsApi.html # Products API detailed report
β”œβ”€β”€ api.brands.BrandsApi.html     # Brands API detailed report
└── *.karate-json.txt            # Raw JSON results

Open karate-summary.html in your browser for:

  • πŸ“ˆ Test execution statistics
  • πŸ” Request/Response details
  • ⏱️ Performance metrics
  • πŸ› Failure analysis
  • πŸ“‹ Step-by-step execution logs

βš™οΈ Configuration

Environment Configuration

Base URL and global settings are configured in karate-config.js:

function fn() {
  var config = {
    baseUrl: 'https://automationexercise.com/api',
    timeout: 30000,
    env: 'dev'
  };
  
  karate.log('Environment:', config.env);
  return config;
}

Maven Configuration

Key dependencies and versions in pom.xml:

<properties>
    <karate.version>1.4.1</karate.version>
    <junit.version>5.9.3</junit.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

πŸ—οΈ Project Architecture

Framework Design Principles

  1. Page Object Model (POM)

    • Separates test logic from test data
    • Improves code reusability and maintainability
    • Organized feature files by API endpoints
  2. Tag-Based Execution

    • Flexible test execution strategies
    • Easy integration with CI/CD pipelines
    • Selective test running capability
  3. Multiple Test Runners

    • ApiTestRunner: Executes all API tests
    • SmokeTestRunner: Critical functionality only
    • RegressionTestRunner: Comprehensive test coverage
    • SimpleKarateRunner: Direct execution with detailed logging

Test Scenario Structure

Each feature file follows this pattern:

Feature: API Name Tests

Background:
  * url baseUrl

@smoke @api-name
Scenario: Positive test scenario
  Given path 'endpoint'
  When method GET
  Then status 200
  And match response.field == 'expected'

@negative @api-name  
Scenario: Negative test scenario
  Given path 'invalid-endpoint'
  When method GET
  Then status 404

πŸ”§ Development Guide

Adding New Tests

  1. Create Feature File

    src/test/java/api/new-endpoint/NewEndpoint.feature
  2. Follow Naming Convention

    • Use descriptive scenario names
    • Add appropriate tags (@smoke, @regression, @negative)
    • Include API endpoint name in tags
  3. Add to Test Runner

    @Karate.Test
    Karate testNewEndpoint() {
        return Karate.run("classpath:api/new-endpoint").tags("~@ignore");
    }

Best Practices

  • βœ… Use meaningful scenario descriptions
  • βœ… Add both positive and negative test cases
  • βœ… Validate response structure and data types
  • βœ… Include proper tags for categorization
  • βœ… Follow consistent naming conventions
  • βœ… Add comments for complex validations

🚨 Troubleshooting

Common Issues

Issue Solution
Java version mismatch Ensure Java 11+ is installed and JAVA_HOME is set
Maven build failures Run mvn clean compile test-compile
Test discovery issues Check JUnit 5 annotations and runner configuration
Karate script errors Verify JavaScript syntax in feature files
Network timeouts Increase timeout in karate-config.js

Debug Mode

Run tests with debug logging:

mvn test -Dkarate.options="--debug"

πŸ“ˆ CI/CD Integration

GitHub Actions Example

name: API Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          java-version: '11'
      - run: mvn test
      - uses: actions/upload-artifact@v3
        with:
          name: test-reports
          path: target/karate-reports/

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-api-tests)
  3. Follow the existing POM structure and naming conventions
  4. Add appropriate tags to new scenarios
  5. Include both positive and negative test cases
  6. Update README.md when adding new features
  7. Commit changes (git commit -am 'Add new API tests')
  8. Push to branch (git push origin feature/new-api-tests)
  9. Create a Pull Request

Code Review Checklist

  • Tests follow POM pattern
  • Proper tags are applied
  • Both positive and negative scenarios included
  • Code is properly documented
  • All tests pass locally

πŸ“„ License

This project is created for educational and testing purposes. No license restrictions apply.

πŸ”— Links


Made with ❀️ using Karate Framework 1.4.1

About

This repository demonstrates API automation testing using the Karate Framework. It includes test scripts for RESTful APIs to validate functionality, response structures, and performance. The project showcases how Karate simplifies API testing with its BDD-style syntax and built-in support for JSON, XML, and HTTP calls.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published