|
| 1 | +# Playwright Cucumber API Test |
| 2 | + |
| 3 | +This project uses Playwright and Cucumber to test API endpoints. It demonstrates how to create and run API tests using BDD principles. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Node.js 14 or higher |
| 8 | +- npm or yarn package manager |
| 9 | + |
| 10 | +## Installation |
| 11 | +1. Clone the repository. |
| 12 | +2. Run the following command to install the dependencies: |
| 13 | + ```bash |
| 14 | + npm install |
| 15 | + ``` |
| 16 | + |
| 17 | +## Running Tests |
| 18 | +To execute the tests, run: |
| 19 | +```bash |
| 20 | +npm test |
| 21 | +``` |
| 22 | + |
| 23 | +## Project Structure |
| 24 | +- `pages/` |
| 25 | + - `apiPage.js` (API interactions and request methods) |
| 26 | +- `features/` |
| 27 | + - `api.feature` (Cucumber feature file with API test scenarios) |
| 28 | + - `jsonplaceholder.feature` (Test scenarios for JSONPlaceholder API) |
| 29 | + - `schemas/` |
| 30 | + - `jsonplaceholder_user.json` (JSON schema for user validation) |
| 31 | + - `users_schema.json` (JSON schema for users endpoint validation) |
| 32 | + - `step_definitions/` |
| 33 | + - `apiSteps.js` (Step definitions for API tests) |
| 34 | + - `commonSteps.js` (Reusable step definitions across features) |
| 35 | + - `jsonplaceholderSteps.js` (Step definitions specific to JSONPlaceholder API) |
| 36 | +- `cucumber.js` (Cucumber configuration) |
| 37 | +- `package.json` (Project dependencies and scripts) |
| 38 | + |
| 39 | +## Test Scenario |
| 40 | +The test verifies that the GET request to the following endpoint returns a 200 status and contains a field named `data`: |
| 41 | +``` |
| 42 | +https://app.beeceptor.com/mock-server/fake-json-api |
| 43 | +``` |
| 44 | + |
| 45 | +## Test Scenarios |
| 46 | + |
| 47 | +### Positive Test Cases |
| 48 | +1. **Basic Endpoint Verification**: |
| 49 | + - Verify GET requests to various endpoints return 200 status codes |
| 50 | + - Validate that endpoint responses contain expected fields |
| 51 | + - Check array responses have the expected number of items |
| 52 | + |
| 53 | +2. **Authentication Tests**: |
| 54 | + - Verify successful login with valid credentials returns a token |
| 55 | + - Test single resource retrieval (users, companies, comments) |
| 56 | + |
| 57 | +3. **Data Structure Validation**: |
| 58 | + - Validate root endpoint meta information |
| 59 | + - Verify JSON structure of responses using schema validation |
| 60 | + |
| 61 | +### Negative Test Cases |
| 62 | +1. **Invalid HTTP Methods**: |
| 63 | + - Verify the response for unsupported HTTP methods like PUT or DELETE |
| 64 | + - Test response codes (405 Method Not Allowed) |
| 65 | + |
| 66 | +2. **Missing or Invalid Parameters**: |
| 67 | + - Test endpoints with missing required parameters |
| 68 | + - Test endpoints with invalid parameter values |
| 69 | + - Validate 400 Bad Request responses |
| 70 | + - Verify 422 Unprocessable Entity for invalid parameters |
| 71 | + |
| 72 | +3. **Edge Cases**: |
| 73 | + - Test endpoints that return empty responses |
| 74 | + - Test endpoints with unexpected data formats |
| 75 | + - Handle invalid endpoints (404 responses) |
| 76 | + - Test invalid resource IDs |
| 77 | + - Verify authentication failures (401 responses) |
| 78 | + - Test empty response bodies (204 No Content) |
| 79 | + |
| 80 | +4. **Error Handling**: |
| 81 | + - Verify error messages in responses |
| 82 | + - Validate handling of non-JSON responses |
| 83 | + |
| 84 | +Refer to the `features/api.feature` and `features/jsonplaceholder.feature` files for detailed scenarios. |
| 85 | + |
| 86 | +## Additional Information |
| 87 | + |
| 88 | +### Writing New Tests |
| 89 | +To add new tests: |
| 90 | +1. Create a new feature file in the `features` directory |
| 91 | +2. Implement step definitions in `features/step_definitions` |
| 92 | +3. Run the tests using `npm test` |
| 93 | + |
| 94 | +### Configuring Tests |
| 95 | +You can modify the Cucumber configuration in the `cucumber.js` file to customize test execution options. |
0 commit comments