Skip to content

Commit 40ef5c3

Browse files
committed
Implement Cypress framework with SauceDemo tests and GitHub Pages deployment
1 parent 63594b2 commit 40ef5c3

22 files changed

+1706
-367
lines changed

.eslintrc.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: [
5+
'@typescript-eslint',
6+
'cypress'
7+
],
8+
extends: [
9+
'eslint:recommended',
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:cypress/recommended'
12+
],
13+
env: {
14+
node: true,
15+
browser: true,
16+
'cypress/globals': true
17+
},
18+
rules: {
19+
'@typescript-eslint/no-namespace': 'off',
20+
'cypress/no-assigning-return-values': 'error',
21+
'cypress/no-unnecessary-waiting': 'warn',
22+
'cypress/assertion-before-screenshot': 'warn',
23+
'no-console': 'warn',
24+
'no-unused-vars': 'warn',
25+
'@typescript-eslint/no-unused-vars': ['warn']
26+
}
27+
};

.github/workflows/cypress.yml

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,72 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout
14-
uses: actions/checkout@v3
15-
14+
uses: actions/checkout@v4
15+
1616
- name: Setup Node.js
17-
uses: actions/setup-node@v3
17+
uses: actions/setup-node@v4
1818
with:
19-
node-version: 18
20-
21-
- name: Install Dependencies
19+
node-version: 20
20+
cache: 'npm'
21+
22+
- name: Install dependencies
2223
run: npm ci
23-
24-
- name: Create env file
25-
run: |
26-
echo '{
27-
"standardUsername": "standard_user",
28-
"lockedOutUsername": "locked_out_user",
29-
"password": "secret_sauce"
30-
}' > cypress.env.json
31-
24+
3225
- name: Cypress run
33-
uses: cypress-io/github-action@v5
26+
uses: cypress-io/github-action@v6
3427
with:
28+
build: npm run build
3529
browser: chrome
3630
headed: false
37-
38-
- name: Upload Test Results
31+
env:
32+
# pass GitHub token to enable accurate reporting of test status
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
35+
- name: Upload screenshots if test fails
36+
uses: actions/upload-artifact@v3
37+
if: failure()
38+
with:
39+
name: cypress-screenshots
40+
path: cypress/screenshots
41+
42+
- name: Upload Mochawesome reports
3943
uses: actions/upload-artifact@v3
4044
if: always()
4145
with:
42-
name: cypress-report
43-
path: cypress/reports/html
44-
retention-days: 30
45-
46+
name: cypress-reports
47+
path: cypress/reports
48+
4649
deploy-report:
4750
needs: cypress-run
4851
runs-on: ubuntu-latest
52+
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
53+
permissions:
54+
pages: write # to deploy to Pages
55+
id-token: write # to verify the deployment originates from an appropriate source
56+
contents: read # to read the repository contents
57+
58+
environment:
59+
name: github-pages
60+
url: ${{ steps.deployment.outputs.page_url }}
61+
4962
steps:
5063
- name: Checkout
51-
uses: actions/checkout@v3
52-
53-
- name: Download Cypress Reports
64+
uses: actions/checkout@v4
65+
66+
- name: Download reports artifact
5467
uses: actions/download-artifact@v3
5568
with:
56-
name: cypress-report
57-
path: cypress/reports/html
58-
59-
- name: Deploy to GitHub Pages
60-
uses: JamesIves/github-pages-deploy-action@v4
69+
name: cypress-reports
70+
path: cypress/reports
71+
72+
- name: Setup Pages
73+
uses: actions/configure-pages@v3
74+
75+
- name: Upload artifact
76+
uses: actions/upload-pages-artifact@v2
6177
with:
62-
folder: cypress/reports/html
63-
branch: gh-pages
78+
path: './cypress/reports/html'
79+
80+
- name: Deploy to GitHub Pages
81+
id: deployment
82+
uses: actions/deploy-pages@v2

.gitignore

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
1-
# Dependencies
1+
# Dependency directories
22
node_modules/
33

4-
# Cypress
5-
cypress/videos/
6-
cypress/screenshots/
4+
# Environment configuration
75
cypress.env.json
86

9-
# Reports
10-
cypress/reports/
11-
12-
# Logs
13-
logs
14-
*.log
15-
npm-debug.log*
16-
yarn-debug.log*
17-
yarn-error.log*
18-
19-
# Runtime data
20-
pids
21-
*.pid
22-
*.seed
23-
*.pid.lock
7+
# Cypress generated files
8+
cypress/videos/
9+
cypress/screenshots/
2410

25-
# IDE files
26-
.idea/
11+
# IDE specific files
2712
.vscode/
28-
*.sublime-project
29-
*.sublime-workspace
13+
.idea/
14+
*.swp
15+
*.swo
3016

31-
# macOS files
17+
# OS specific files
3218
.DS_Store
33-
.AppleDouble
34-
.LSOverride
19+
Thumbs.db
20+
21+
# Debug logs
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*

README.md

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,102 @@
1-
# SauceDemo Cypress Test Automation
1+
# SauceDemo Cypress Tests
22

3-
This project contains automated tests for the SauceDemo website using Cypress with TypeScript.
3+
This project contains automated tests for the [SauceDemo](https://www.saucedemo.com) website using Cypress with TypeScript.
44

5-
## Project Structure
6-
7-
```
8-
├── cypress/
9-
│ ├── e2e/ # Test files
10-
│ ├── fixtures/ # Test data
11-
│ ├── pages/ # Page objects
12-
│ ├── support/ # Support files (commands, etc.)
13-
│ └── reports/ # Test reports (generated)
14-
├── .gitignore # Git ignore file
15-
├── cypress.config.ts # Cypress configuration
16-
├── cypress.env.json # Environment variables (not committed)
17-
├── package.json # Project dependencies
18-
├── README.md # Project documentation
19-
└── tsconfig.json # TypeScript configuration
20-
```
5+
## Features
216

22-
## Test Scenarios
7+
- Written in TypeScript
8+
- Page Object Model pattern
9+
- Custom commands
10+
- Environment configuration
11+
- Detailed logging
12+
- Mochawesome reporting
13+
- GitHub Actions integration
14+
- GitHub Pages for test reports
2315

24-
1. **Valid Login Test** - Tests successful login with standard user
25-
2. **Locked Out User Test** - Tests error message for locked out user
16+
## Prerequisites
2617

27-
## Setup
18+
- Node.js (v16 or higher)
19+
- npm (v8 or higher)
2820

29-
1. Clone the repository
30-
2. Install dependencies:
21+
## Installation
3122

3223
```bash
33-
npm install
34-
```
24+
# Clone the repository
25+
git clone <repository-url>
3526

36-
3. Create a `cypress.env.json` file with the following content:
27+
# Navigate to the project directory
28+
cd cypress-saucedemo
3729

38-
```json
39-
{
40-
"standardUsername": "standard_user",
41-
"lockedOutUsername": "locked_out_user",
42-
"password": "secret_sauce"
43-
}
30+
# Install dependencies
31+
npm install
4432
```
4533

4634
## Running Tests
4735

48-
### Headless Mode
36+
```bash
37+
# Run tests in headless mode
38+
npm run cy:run
4939

50-
To run all tests in headless mode:
40+
# Open Cypress Test Runner
41+
npm run cy:open
5142

52-
```bash
53-
npm test
43+
# Run linting
44+
npm run lint
5445
```
5546

56-
### Interactive Mode
47+
## Project Structure
48+
49+
```
50+
cypress-saucedemo/
51+
├── .github/ # GitHub Actions workflows
52+
│ └── workflows/
53+
│ └── cypress.yml # CI workflow configuration
54+
├── cypress/
55+
│ ├── e2e/ # Test files
56+
│ │ └── login.cy.ts # Login tests
57+
│ ├── fixtures/ # Test data
58+
│ │ └── testData.json # Common test data
59+
│ ├── pages/ # Page Object Models
60+
│ │ ├── BasePage.ts # Base page with common methods
61+
│ │ ├── LoginPage.ts # Login page object
62+
│ │ └── InventoryPage.ts # Inventory page object
63+
│ ├── reports/ # Generated test reports (git ignored)
64+
│ └── support/ # Support files
65+
│ ├── commands.ts # Custom commands
66+
│ └── e2e.ts # e2e support file
67+
├── cypress.config.ts # Cypress configuration
68+
├── cypress.env.json # Environment variables (git ignored)
69+
├── package.json # Dependencies and scripts
70+
├── tsconfig.json # TypeScript configuration
71+
└── README.md # Project documentation
72+
```
5773

58-
To open Cypress in interactive mode:
74+
## Test Reports
5975

60-
```bash
61-
npm run cy:open
76+
After running the tests, reports are generated in the `cypress/reports` directory. When tests are run in the CI pipeline, the reports are published to GitHub Pages.
77+
78+
## Environment Variables
79+
80+
Sensitive data like credentials are stored in `cypress.env.json` which is git-ignored for security. Create this file locally with the following structure:
81+
82+
```json
83+
{
84+
"users": {
85+
"standard": "standard_user",
86+
"locked": "locked_out_user",
87+
"problem": "problem_user",
88+
"performance": "performance_glitch_user"
89+
},
90+
"password": "secret_sauce"
91+
}
6292
```
6393

64-
## Reports
94+
## CI/CD
95+
96+
The project uses GitHub Actions for continuous integration. On each push to the `master` branch, the workflow:
6597

66-
Test reports are generated using cypress-mochawesome-reporter and can be found in the `cypress/reports/html` directory after running the tests.
98+
1. Installs dependencies
99+
2. Runs Cypress tests
100+
3. Uploads screenshots on failure
101+
4. Generates and publishes test reports
102+
5. Deploys the report to GitHub Pages

cypress.config.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ export default defineConfig({
44
reporter: 'cypress-mochawesome-reporter',
55
reporterOptions: {
66
charts: true,
7-
reportPageTitle: 'SauceDemo Test Report',
7+
reportPageTitle: 'SauceDemo Tests',
88
embeddedScreenshots: true,
99
inlineAssets: true,
1010
},
1111
e2e: {
12-
baseUrl: 'https://www.saucedemo.com',
1312
setupNodeEvents(on, config) {
1413
require('cypress-mochawesome-reporter/plugin')(on);
1514

@@ -18,8 +17,15 @@ export default defineConfig({
1817
log(message) {
1918
console.log(message);
2019
return null;
21-
}
20+
},
2221
});
2322
},
23+
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
24+
baseUrl: 'https://www.saucedemo.com',
25+
viewportWidth: 1280,
26+
viewportHeight: 720,
27+
video: false,
28+
screenshotOnRunFailure: true,
29+
pageLoadTimeout: 120000,
2430
},
2531
});

cypress/e2e/lockedOutUser.cy.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)