This project uses Playwright and Cucumber to test API endpoints. It demonstrates how to create and run API tests using BDD principles.
- Node.js 14 or higher
- npm or yarn package manager
- Clone the repository.
- Run the following command to install the dependencies:
npm install
To execute the tests, run:
npm test
pages/
apiPage.js
(API interactions and request methods)
features/
api.feature
(Cucumber feature file with API test scenarios)jsonplaceholder.feature
(Test scenarios for JSONPlaceholder API)schemas/
jsonplaceholder_user.json
(JSON schema for user validation)users_schema.json
(JSON schema for users endpoint validation)
step_definitions/
apiSteps.js
(Step definitions for API tests)commonSteps.js
(Reusable step definitions across features)jsonplaceholderSteps.js
(Step definitions specific to JSONPlaceholder API)
cucumber.js
(Cucumber configuration)package.json
(Project dependencies and scripts)
The test verifies that the GET request to the following endpoint returns a 200 status and contains a field named data
:
https://app.beeceptor.com/mock-server/fake-json-api
-
Basic Endpoint Verification:
- Verify GET requests to various endpoints return 200 status codes
- Validate that endpoint responses contain expected fields
- Check array responses have the expected number of items
-
Authentication Tests:
- Verify successful login with valid credentials returns a token
- Test single resource retrieval (users, companies, comments)
-
Data Structure Validation:
- Validate root endpoint meta information
- Verify JSON structure of responses using schema validation
-
Invalid HTTP Methods:
- Verify the response for unsupported HTTP methods like PUT or DELETE
- Test response codes (405 Method Not Allowed)
-
Missing or Invalid Parameters:
- Test endpoints with missing required parameters
- Test endpoints with invalid parameter values
- Validate 400 Bad Request responses
- Verify 422 Unprocessable Entity for invalid parameters
-
Edge Cases:
- Test endpoints that return empty responses
- Test endpoints with unexpected data formats
- Handle invalid endpoints (404 responses)
- Test invalid resource IDs
- Verify authentication failures (401 responses)
- Test empty response bodies (204 No Content)
-
Error Handling:
- Verify error messages in responses
- Validate handling of non-JSON responses
Refer to the features/api.feature
and features/jsonplaceholder.feature
files for detailed scenarios.
To add new tests:
- Create a new feature file in the
features
directory - Implement step definitions in
features/step_definitions
- Run the tests using
npm test
You can modify the Cucumber configuration in the cucumber.js
file to customize test execution options.