8000 GitHub - adityadwic/Cypress-API-Automation: This repository demonstrates API automation using Cypress, a popular testing framework primarily used for end-to-end testing of web applications. It includes automation scripts specifically designed for testing RESTful APIs, verifying responses, and ensuring API functionality, security, and performance.
[go: up one dir, main page]

Skip to content

This repository demonstrates API automation using Cypress, a popular testing framework primarily used for end-to-end testing of web applications. It includes automation scripts specifically designed for testing RESTful APIs, verifying responses, and ensuring API functionality, security, and performance.

Notifications You must be signed in to change notification settings

adityadwic/Cypress-API-Automation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Cypress API Automation - Automation Exercise

Professional API Testing with Cypress using Page Object Model (POM) + HTML Test Reporting

Cypress Tests Test Reports Cypress Node.js

This project demonstrates enterprise-grade API automation testing for the Automation Exercise website using Cypress with modern best practices including Page Object Model design pattern, comprehensive test reporting, and CI/CD integration.

🎯 Overview

This repository contains production-ready API automation tests that validate the Automation Exercise APIs using Cypress. The implementation follows industry standards with clean architecture, reusable components, comprehensive test coverage, and professional HTML test reporting.

πŸ”— APIs Tested

API Endpoint Method Description
API 1 /api/productsList GET Get All Products List
API 3 /api/brandsList GET Get All Brands List

✨ Key Features

  • πŸ—οΈ Page Object Model (POM) - Clean separation of test logic and API interactions
  • οΏ½ HTML Test Reports - Interactive Mochawesome reports with charts and metrics
  • οΏ½πŸ› οΈ Utility Functions - Reusable validation and helper methods
  • 🎨 Custom Commands - Enhanced Cypress functionality for API testing
  • οΏ½ Comprehensive Testing - Response validation, performance testing, error handling
  • πŸ”„ Integration Testing - Cross-API data consistency validation
  • πŸ“ Test Data Management - Centralized test data generation and management
  • πŸš€ Performance Testing - Response time validation and optimization
  • πŸ›‘οΈ Error Handling - Comprehensive error scenario testing
  • πŸ“‹ CI/CD Ready - GitHub Actions workflow included
  • 🎯 100% Pass Rate - All 8 tests consistently passing

πŸ›οΈ Project Architecture

cypress/
β”œβ”€β”€ e2e/
β”‚   └── API-AutomationExerice/
β”‚       β”œβ”€β”€ simple-api-tests.cy.js          βœ… Main working tests (8/8 passing)
β”‚       β”œβ”€β”€ products-api.cy.js              πŸ“¦ Products API tests with POM
β”‚       β”œβ”€β”€ brands-api.cy.js                🏷️ Brands API tests with POM
β”‚       └── complete-api-suite.cy.js        πŸ”„ Integration tests
β”œβ”€β”€ support/
β”‚   β”œβ”€β”€ commands.js                         πŸ› οΈ Custom Cypress commands
β”‚   β”œβ”€β”€ e2e.js                             βš™οΈ Global test configuration
β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   └── AutomationExerciseAPI.js       πŸ“„ Page Object Model
β”‚   └── utils/
β”‚       β”œβ”€β”€ APIUtils.js                    πŸ”§ API utility functions
β”‚       └── TestDataFactory.js            🏭 Test data generator
β”œβ”€β”€ reports/                               πŸ“Š Test reports (auto-generated)
β”‚   β”œβ”€β”€ html/
β”‚   β”‚   └── merged-report.html             🎯 Interactive HTML report
β”‚   └── *.json                            πŸ“„ Raw test data
docs/
β”œβ”€β”€ REPORTING.md                           πŸ“‹ Reporting guide
└── TEST_REPORT.md                         πŸ“Š Latest test results

β”‚ └── *.html πŸ“‹ Individual reports └── support/ β”œβ”€β”€ pages/ β”‚ └── AutomationExerciseAPI.js πŸ—οΈ Page Object Model β”œβ”€β”€ utils/ β”‚ β”œβ”€β”€ APIUtils.js πŸ› οΈ Utility functions β”‚ └── TestDataFactory.js πŸ“Š Test data management β”œβ”€β”€ commands.js 🎯 Custom commands └── e2e.js βš™οΈ Support configuration


## πŸš€ Quick Start

### Prerequisites

- [Node.js](https://nodejs.org/) (v14 or higher)
- [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/)

### Installation

1. **Clone the repository:**
   ```bash
   git clone https://github.com/adityadwic/Cypress-API-Automation.git
   cd Cypress-API-Automation
  1. Install dependencies:
    npm install

πŸ§ͺ Running Tests

Quick Test Execution

# 🎯 Generate tests with HTML reports (RECOMMENDED)
npm run test:report

# Run all tests
npm test

# Run main working tests
npm run test:simple

# Run specific API tests
npm run test:products      # Products API only
npm run test:brands        # Brands API only
npm run test:complete      # Complete test suite
npm run test:all          # All API tests

# Run tests in different modes
npm run test:headed       # Headed mode
npm run test:chrome       # Chrome browser
npm run test:firefox      # Firefox browser

πŸ“Š Test Reporting

cypress-test-report
# Generate and view HTML reports
npm run test:report       # Run tests + generate report
npm run generate:report   # Generate report from existing data
npm run report:open       # Open HTML report in browser
npm run clean:reports     # Clean old reports

Cypress Interactive Mode

# Open Cypress Test Runner
npm run test:open

πŸ“Š Test Results & Reporting

🎯 Current Test Status

βœ… 8/8 Tests Passing (100% Success Rate)

Test Suite Status Duration Coverage
Products API βœ… 3/3 ~1.2s GET, Error Handling
Brands API βœ… 3/3 ~1.1s GET, Validation, Error Handling
Performance βœ… 1/1 ~0.7s Response Time < 5s
Integration βœ… 1/1 ~0.8s Cross-API Consistency

πŸ“ˆ HTML Test Reports

The project generates interactive HTML reports using Mochawesome:

Features:

  • πŸ“Š Visual charts and graphs
  • 🎯 Pass/Fail statistics
  • ⏱️ Performance metrics
  • πŸ” Detailed test results
  • πŸ’» Browser-friendly viewing

Report Location: cypress/reports/html/merged-report.html

Quick Access:

npm run test:report    # Generate new report
npm run report:open    # View latest report

πŸ“‹ Test Coverage Details

βœ… Successful Tests:

  1. Products API - GET request validation
  2. Products API - POST method error handling
  3. Brands API - GET request validation
  4. Brands API - Expected brands verification
  5. Brands API - POST method error handling
  6. Performance testing for both APIs
  7. Data consistency between APIs
  8. Error handling for invalid endpoints

πŸ—οΈ Page Object Model Implementation

API Page Object (AutomationExerciseAPI.js)

import AutomationExerciseAPI from '../../support/pages/AutomationExerciseAPI.js';

const apiPage = new AutomationExerciseAPI();

// Get all products
apiPage.getAllProducts().then((response) => {
  // Test logic here
});

// Get all brands
apiPage.getAllBrands().then((response) => {
  // Test logic here
});

Utility Functions (APIUtils.js)

import APIUtils from '../../support/utils/APIUtils.js';

// Validate status code
APIUtils.validateStatusCode(response, 200);

// Validate response structure
APIUtils.validateProductsListStructure(response);

// Log response details
APIUtils.logResponseDetails(response, 'Test Name');

Custom Commands

// Enhanced API request with logging
cy.apiRequest('GET', 'https://automationexercise.com/api/productsList');

// Validate API response
cy.validateApiResponse(response, 200);

// Automation Exercise specific API calls
cy.automationExerciseApi('/productsList');

🎯 Test Coverage

API Validation

  • βœ… Successful HTTP requests (GET)
  • βœ… HTTP method validation (POST, PUT, DELETE not allowed)
  • βœ… Response structure validation
  • βœ… Data type validation
  • βœ… Response code validation

Performance Testing

  • βœ… Response time validation (< 5 seconds)
  • βœ… Concurrent request handling
  • βœ… Load testing capabilities

Error Handling

  • βœ… Invalid HTTP methods
  • βœ… Invalid endpoints
  • βœ… Malformed requests
  • βœ… Network error scenarios

Integration Testing

  • βœ… Cross-API data consistency
  • βœ… Brand-Product relationship validation
  • βœ… Data integrity checks

πŸ› οΈ Configuration

8000 Package Scripts (package.json)

{
  "scripts": {
    "test": "cypress run",
    "test:open": "cypress open",
    "test:simple": "cypress run --spec 'cypress/e2e/API-AutomationExerice/simple-api-tests.cy.js'",
    "test:products": "cypress run --spec 'cypress/e2e/API-AutomationExerice/products-api.cy.js'",
    "test:brands": "cypress run --spec 'cypress/e2e/API-AutomationExerice/brands-api.cy.js'",
    "test:complete": "cypress run --spec 'cypress/e2e/API-AutomationExerice/complete-api-suite.cy.js'",
    "test:all": "cypress run --spec 'cypress/e2e/API-AutomationExerice/*.cy.js'",
    "test:headed": "cypress run --headed",
    "test:chrome": "cypress run --browser chrome",
    "test:firefox": "cypress run --browser firefox",
    "test:report": "npm run test:simple && npm run generate:report",
    "generate:report": "mochawesome-merge cypress/reports/*.json > cypress/reports/merged-report.json && marge cypress/reports/merged-report.json --reportDir cypress/reports/html",
    "clean:reports": "rm -rf cypress/reports",
    "report:open": "open cypress/reports/html/merged-report.html"
  }
}

Cypress Configuration (cypress.config.js)

module.exports = defineConfig({
  e2e: {
    baseUrl: 'https://automationexercise.com',
    requestTimeout: 10000,
    responseTimeout: 10000,
    retries: { runMode: 2, openMode: 1 },
    env: { apiBaseUrl: 'https://automationexercise.com/api' }
  },
  reporter: 'mochawesome',
  reporterOptions: {
    reportDir: 'cypress/reports',
    overwrite: false,
    html: true,
    json: true,
    timestamp: 'mmddyyyy_HHMMss'
  }
});

πŸ“ˆ CI/CD Integration

This project is production-ready for CI/CD integration with automated test reporting.

GitHub Actions Workflow (.github/workflows/api-tests.yml)

name: πŸš€ API Automation Tests
on: 
  push:
    branches: [ master, main ]
  pull_request:
    branches: [ master, main ]
  schedule:
    - cron: '0 6 * * *'  # Daily at 6 AM

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x, 20.x]
    
    steps:
      - name: πŸ“₯ Checkout Repository
        uses: actions/checkout@v4
        
      - name: πŸ”§ Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          
      - name: πŸ“¦ Install Dependencies
        run: npm ci
        
      - name: πŸ§ͺ Run API Tests
        run: npm run test:report
        
      - name: πŸ“Š Upload Test Reports
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: test-reports-node-${{ matrix.node-version }}
          path: cypress/reports/html/
          retention-days: 30
          
      - name: πŸ“ˆ Publish Test Summary
        if: always()
        run: |
          echo "## πŸ§ͺ Test Results Summary" >> $GITHUB_STEP_SUMMARY
          echo "- **Node Version:** ${{ matrix.node-version }}" >> $GITHUB_STEP_SUMMARY
          echo "- **Tests Status:** $(if [ $? -eq 0 ]; then echo 'βœ… PASSED'; else echo '❌ FAILED'; fi)" >> $GITHUB_STEP_SUMMARY

Local CI/CD Testing

# Simulate CI environment
npm ci                    # Clean install
npm run clean:reports     # Clean previous reports
npm run test:report       # Generate fresh reports
npm run report:open       # View results

πŸ“š Documentation

πŸ› οΈ Tech Stack

Component Technology Version
Testing Framework Cypress 13.3.1
Runtime Node.js 18.20.5+
Reporting Mochawesome 7.1.3
Architecture Page Object Model Custom
CI/CD GitHub Actions Latest

🀝 Contributing

Development Workflow

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/amazing-feature
  3. Make your changes
  4. Run tests and generate reports
    npm run test:report
  5. Commit your changes
    git commit -m 'Add some amazing feature'
  6. Push to the branch
    git push origin feature/amazing-feature
  7. Open a Pull Request

Code Standards

  • βœ… Follow Page Object Model pattern
  • βœ… Add comprehensive test coverage
  • βœ… Include proper error handling
  • βœ… Update documentation
  • βœ… Ensure all tests pass

πŸ‘¨β€πŸ’» Author

Aditya Dwi Cahyono

πŸ”— Useful Links

πŸ“Š Project Status

Metric Status
Tests βœ… 8/8 Passing (100%)
Coverage βœ… Both APIs Covered
Performance βœ… < 5s Response Time
Reporting βœ… HTML + Markdown
CI/CD βœ… GitHub Actions Ready
Documentation βœ… Complete

πŸš€ Quick Start Commands

# Clone and setup
git clone https://github.com/adityadwic/Cypress-API-Automation.git
cd Cypress-API-Automation
npm install

# Run tests with reports
npm run test:report

# Open HTML report
npm run report:open

# View in Cypress UI
npm run test:open

⭐ Star this repository if you find it helpful!
πŸ› Found an issue? Create an issue
πŸ’‘ Have suggestions? Start a discussion

About

This repository demonstrates API automation using Cypress, a popular testing framework primarily used for end-to-end testing of web applications. It includes automation scripts specifically designed for testing RESTful APIs, verifying responses, and ensuring API functionality, security, and performance.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0