This repository contains an automated test suite developed using Cypress and TypeScript to test the functionality, reliability, and compliance of the Swagger Petstore API. The suite includes tests for CRUD operations, data validation, response schema verification, and authorization scenarios.
-
API Testing:
- Covers CRUD operations on Petstore API endpoints.
- Handles scenarios with and without API keys (
special-key
). - Validates response schemas using
AJV
and JSON schema files. - Tests include data validation and API contract validation.
-
Automation Framework:
- Built using Cypress with TypeScript for robust testing.
- Supports parallel execution and file manipulation (e.g., loading and saving test data).
- Extensible for additional tests and reusable components.
- POST: Add a new pet to the store.
- GET: Retrieve pets by ID and status.
- PUT: Update existing pet details.
- DELETE: Remove a pet by ID.
- Tests with and without API keys to validate authentication mechanisms.
- Validates API responses using the
AJV
library with a predefined JSON schema (petSchema.json
).
- Validates responses for unauthorized or invalid requests, ensuring proper status codes and error messages.
- Reads and writes test data dynamically to files for reusability across test cases.
git clone https://github.com/azrimangsor/petstore.git
cd petstore-api-testing
Ensure Node.js and npm are installed, then run:
npm install
To execute all tests:
npx cypress open
To run tests in headless mode:
npx cypress run
To run ONLY the API Test:
npm run cypress:run:spec
To run ONLY the Performance Test:
npm run cypress:run:perf
Ensure cypress.config.ts
is correctly configured with the base URL:
export default defineConfig({
e2e: {
baseUrl: 'https://petstore.swagger.io/v2',
},
});
For detailed information about the testing process, scenarios, and planning, refer to the Master Test Plan.
petstore-api-testing/
├── cypress/
│ ├── fixtures/
│ │ ├── petData.json # Test data file for dynamic CRUD operations
│ │ ├── petSchema.json # JSON schema for response validation
│ ├── integration/
│ │ ├── petstore-tests.ts # Core test suite
│ ├── support/
│ │ ├── commands.ts # Custom Cypress commands
│ │ ├── e2e.ts # Support for E2E tests
├── cypress.config.ts # Cypress configuration file
├── package.json # Project dependencies
├── tsconfig.json # TypeScript configuration
└── README.md # Project documentation
Here’s a snippet of a test case that validates the schema of a retrieved pet:
import petSchema from '../fixtures/petSchema.json';
import Ajv from 'ajv';
describe('GET Pet by ID - Schema Validation', () => {
it('Validates the pet response schema', () => {
const ajv = new Ajv();
const validate = ajv.compile(petSchema);
cy.request({
method: 'GET',
url: '/pet/9223372036854776000',
}).then((response) => {
expect(response.status).to.eq(200);
const isValid = validate(response.body);
expect(isValid, 'Response matches schema').to.be.true;
});
});
});
Contributions are welcome! Please fork the repository and create a pull request with your proposed changes.
This project is licensed under the MIT License. See LICENSE for more details.