Skip to content

Commit a9ccf47

Browse files
committed
Initial commit with all history squashed
0 parents  commit a9ccf47

19 files changed

+11381
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Publish Test Reports
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
pages: write
11+
id-token: write
12+
13+
jobs:
14+
test-and-publish:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
20+
- name: Setup Node.js
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: "18"
24+
25+
- name: Install dependencies
26+
run: npm ci
27+
28+
- name: Run tests
29+
run: npm test
30+
31+
- name: Deploy to GitHub Pages
32+
uses: JamesIves/github-pages-deploy-action@v4
33+
with:
34+
folder: reports
35+
branch: gh-pages
36+
clean: true

.github/workflows/static.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Simple workflow for deploying static content to GitHub Pages
2+
name: Deploy static content to Pages
3+
4+
on:
5+
# Runs on pushes targeting the default branch
6+
push:
7+
branches: ["main"]
8+
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13+
permissions:
14+
contents: read
15+
pages: write
16+
id-token: write
17+
18+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
19+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
20+
concurrency:
21+
group: "pages"
22+
cancel-in-progress: false
23+
24+
jobs:
25+
# Single deploy job since we're just deploying
26+
deploy:
27+
environment:
28+
name: github-pages
29+
url: ${{ steps.deployment.outputs.page_url }}
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v4
34+
- name: Setup Pages
35+
uses: actions/configure-pages@v5
36+
- name: Upload artifact
37+
uses: actions/upload-pages-artifact@v3
38+
with:
39+
path: 'reports'
40+
- name: Deploy to GitHub Pages
41+
id: deployment
42+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v22.11.0

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.

cucumber.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
default: {
3+
paths: ['features/**/*.feature'],
4+
require: ['features/step_definitions/**/*.js'],
5+
format: [
6+
'summary',
7+
'json:reports/cucumber_report.json',
8+
'html:reports/cucumber_report.html'
9+
],
10+
}
11+
}

features/api.feature

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Feature: API Testing
2+
3+
Background: Default Parameters
4+
Given the base API endpoint is set
5+
6+
Scenario Outline: Verify list endpoints
7+
Given the API endpoint is set to "<endpoint>"
8+
When I make a GET request
9+
Then the response status should be 200
10+
Then the response should contain the field "0.id"
11+
12+
Examples:
13+
| endpoint |
14+
| /users |
15+
| /companies |
16+
17+
Scenario Outline: Verify single resource endpoints
18+
Given the API endpoint is set to "<endpoint>/<id>"
19+
When I make a GET request
20+
Then the response status should be 200
21+
Then the response should contain the field "id"
22+
23+
Examples:
24+
| endpoint | id |
25+
| /users | 1 |
26+
| /companies | 1 |
27+
28+
Scenario Outline: Verify specialized endpoints
29+
Given the API endpoint is set to "<endpoint>"
30+
When I make a GET request
31+
Then the response status should be 200
32+
Then the response should contain the field "<field>"
33+
34+
Examples:
35+
| endpoint | field |
36+
| /customers | 0.username |
37+
| /notifications | 0.title |
38+
| / | status |
39+
40+
# Negative test cases
41+
Scenario: Handle invalid user endpoint
42+
Given the API endpoint is set to "/users/invalid_id"
43+
When I make a GET request
44+
Then the response status should be 200
45+
Then the response should not be valid JSON
46+
47+
Scenario Outline: Validate HTTP error responses
48+
Given I send a <method> request to "<endpoint>" <details>
49+
Then the response status should be <status>
50+
And the response body should contain "<message>"
51+
52+
Examples:
53+
| method | endpoint | details | status | message |
54+
| PUT | /api/endpoint | | 405 | Method Not Allowed |
55+
| GET | /api/endpoint | without parameters | 400 | Bad Request |
56+
| GET | /api/endpoint | with "invalidParam=123" | 422 | Unprocessable Entity |
57+
58+
Scenario: Test endpoint with empty response
59+
Given I send a GET request to "/api/emptyEndpoint"
60+
Then the response status should be 204
61+
And the response body should be empty

features/jsonplaceholder.feature

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Feature: JSONPlaceholder Mock API
2+
3+
Background: Default Parameters
4+
Given I use the JSONPlaceholder API with base URL "https://json-placeholder.mock.beeceptor.com"
5+
6+
Scenario Outline: Verify GET list endpoints
7+
Given the endpoint is "<endpoint>"
8+
When I send a GET request
9+
Then the response status should be 200
10+
And the response array length should be <expectedLength>
11+
12+
Examples:
13+
| endpoint | expectedLength |
14+
| /comments | 20 |
15+
| /companies | 10 |
16+
| /posts | 10 |
17+
| /roles | 5 |
18+
| /todos | 20 |
19+
| /users | 20 |
20+
21+
Scenario Outline: Verify GET single-item endpoints
22+
Given the endpoint is "<endpoint>"
23+
When I send a GET request
24+
Then the response status should be 200
25+
And the response should contain the field "id"
26+
27+
Examples:
28+
| endpoint |
29+
| /comments/1 |
30+
| /companies/1 |
31+
| /posts/1 |
32+
| /roles/1 |
33+
| /todos/1 |
34+
| /users/1 |
35+
36+
Scenario Outline: Verify authentication endpoints
37+
Given the endpoint is "/login"
38+
When I send a POST request with body '<requestBody>'
39+
Then the response status should be <statusCode>
40+
And the response should contain the field "<responseField>"
41+
42+
Examples:
43+
| scenario | requestBody | statusCode | responseField |
44+
| successful login | {"username":"michael","password":"success-password"} | 200 | token |
45+
| invalid login | {"username":"user123","password":"failed-password"} | 401 | error |
46+
47+
Scenario Outline: Handle error cases
48+
Given the endpoint is "<endpoint>"
49+
When I send a <method> request
50+
Then the response status should be <statusCode>
51+
52+
Examples:
53+
| scenario | endpoint | method | statusCode |
54+
| invalid endpoint | http://example.com/invalid-endpoint | GET | 404 |

features/schemas/jsonplaceholder_user.json

Whitespace-only changes.

features/schemas/users_schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "array",
3+
"items": {
4+
"type": "object",
5+
"required": ["id", "name", "company", "username", "email", "address", "zip", "state", "country", "phone", "photo"],
6+
"properties": {
7+
"id": { "type": "number" },
8+
"name": { "type": "string" },
9+
"company": { "type": "string" },
10+
"username": { "type": "string" },
11+
"email": { "type": "string" },
12+
"address": { "type": "string" },
13+
"zip": { "type": "string" },
14+
"state": { "type": "string" },
15+
"country": { "type": "string" },
16+
"phone": { "type": "string" },
17+
"photo": { "type": "string" }
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)