Skip to content

Commit eb02a2f

Browse files
committed
Add Cypress tests for SauceDemo with GitHub Actions workflow
1 parent 63a4e1f commit eb02a2f

24 files changed

+463
-461
lines changed

.github/workflows/cypress.yml

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,48 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout
14-
uses: actions/checkout@v3
14+
uses: actions/checkout@v2
1515

1616
- name: Setup Node.js
17-
uses: actions/setup-node@v3
17+
uses: actions/setup-node@v2
1818
with:
19-
node-version: 18
19+
node-version: 16
2020

2121
- name: Install dependencies
22-
run: npm install
22+
run: npm ci
2323

24-
- name: Create cypress.env.json
24+
# Create cypress.env.json file using GitHub secrets
25+
- name: Create Cypress environment file
2526
run: |
2627
echo '{
27-
"standard_username": "standard_user",
28-
"locked_out_username": "locked_out_user",
29-
"password": "secret_sauce"
28+
"users": {
29+
"standard": {
30+
"username": "standard_user",
31+
"password": "secret_sauce"
32+
},
33+
"locked": {
34+
"username": "locked_out_user",
35+
"password": "secret_sauce"
36+
}
37+
}
3038
}' > cypress.env.json
3139
32-
- name: Run Cypress Tests
33-
uses: cypress-io/github-action@v5
40+
- name: Cypress run
41+
uses: cypress-io/github-action@v4
3442
with:
3543
browser: chrome
36-
headless: true
44+
headed: false
3745

38-
- name: Upload Mochawesome Report
39-
uses: actions/upload-artifact@v3
46+
- name: Upload screenshots on failure
47+
uses: actions/upload-artifact@v2
48+
if: failure()
49+
with:
50+
name: cypress-screenshots
51+
path: cypress/screenshots
52+
53+
- name: Upload Mochawesome report
54+
uses: actions/upload-artifact@v2
4055
if: always()
4156
with:
42-
name: mochawesome-report
57+
name: cypress-report
4358
path: cypress/reports

.gitignore

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
81
# Dependencies
92
node_modules/
10-
.pnp/
11-
.pnp.js
123

13-
# Cypress
4+
# Cypress generated files
145
cypress/videos/
156
cypress/screenshots/
167
cypress/downloads/
178
cypress/reports/
189

19-
# Environment
10+
# Environment variables (contains credentials)
2011
cypress.env.json
2112

22-
# Testing
23-
coverage/
24-
.nyc_output/
25-
mochawesome-report/
26-
27-
# Build
28-
.next/
29-
out/
30-
build/
31-
dist/
13+
# OS specific files
14+
.DS_Store
15+
Thumbs.db
3216

33-
# Editors
34-
.vscode/
17+
# IDE specific files
3518
.idea/
36-
*.swp
37-
*.swo
19+
.vscode/
3820

39-
# OS
40-
.DS_Store
41-
Thumbs.db
21+
# Log files
22+
*.log
23+
npm-debug.log*

README.md

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,65 @@
1-
# Sauce Demo Cypress Tests
1+
# SauceDemo Cypress Tests
22

3-
This repository contains automated tests for the Sauce Demo website using Cypress with TypeScript.
3+
This project contains automated tests for the SauceDemo website using Cypress with TypeScript and follows the Page Object Model pattern.
44

5-
## Features
5+
## Test Scenarios
66

7-
- TypeScript for type safety
8-
- Page Object Model (POM) design pattern
9-
- Custom Cypress commands
10-
- Mochawesome reporting
11-
- GitHub Actions integration
12-
- Environment variables for credentials
7+
1. Valid Login - Tests successful login with standard_user
8+
2. Locked Out User - Tests error message when logging in with locked_out_user
139

1410
## Project Structure
1511

1612
```
17-
├── .github/workflows # GitHub Actions workflow files
18-
├── cypress # Cypress test files
19-
│ ├── e2e # Test specifications
20-
│ ├── fixtures # Test data
21-
│ ├── support # Support files
22-
│ │ ├── commands # Custom commands
23-
│ │ ├── pages # Page Object Models
24-
├── .gitignore # Git ignore file
25-
├── cypress.config.ts # Cypress configuration
26-
├── cypress.env.json # Environment variables (gitignored)
27-
├── package.json # Project dependencies
28-
├── tsconfig.json # TypeScript configuration
29-
└── README.md # Project documentation
13+
├── cypress/
14+
│ ├── e2e/ # Test files
15+
│ ├── fixtures/ # Test data
16+
│ ├── pages/ # Page Objects
17+
│ └── support/ # Support files and commands
18+
├── .github/workflows/ # GitHub Actions workflows
19+
├── cypress.config.ts # Cypress configuration
20+
├── cypress.env.json # Environment variables (not committed to git)
21+
├── package.json # Project dependencies
22+
└── tsconfig.json # TypeScript configuration
3023
```
3124

3225
## Prerequisites
3326

34-
- Node.js (v16 or higher)
35-
- npm (v7 or higher)
27+
- Node.js (v14 or later)
28+
- npm (v6 or later)
3629

3730
## Installation
3831

39-
1. Clone the repository:
40-
```
41-
git clone <repository-url>
42-
```
43-
32+
1. Clone the repository
4433
2. Install dependencies:
45-
```
46-
npm install
47-
```
48-
49-
3. Create a `cypress.env.json` file in the root directory with the following content:
50-
```json
51-
{
52-
"standard_username": "standard_user",
53-
"locked_out_username": "locked_out_user",
54-
"password": "secret_sauce"
55-
}
56-
```
34+
35+
```bash
36+
npm install
37+
```
5738

5839
## Running Tests
5940

60-
To run tests in the Cypress Test Runner:
41+
### Run tests in headless mode:
6142

62-
```
63-
npm run cypress:open
43+
```bash
44+
npm run cy:run
6445
```
6546

66-
To run tests in headless mode:
47+
### Open Cypress Test Runner:
6748

68-
```
69-
npm run cypress:run
49+
```bash
50+
npm run cy:open
7051
```
7152

72-
To run tests in a specific browser:
73-
74-
```
75-
npm run cypress:run:chrome
76-
```
53+
## CI/CD
7754

78-
## Reports
55+
This project uses GitHub Actions for continuous integration. The workflow:
7956

80-
After running tests in headless mode, the Mochawesome report will be generated in the `cypress/reports` directory.
57+
1. Runs on pushes to the master branch and pull requests
58+
2. Installs dependencies
59+
3. Runs Cypress tests in headless mode
60+
4. Generates and uploads Mochawesome reports
61+
5. Uploads screenshots on test failure
8162

82-
## CI/CD
63+
## Test Reporting
8364

84-
The repository is configured with GitHub Actions to run tests on every push to the master branch.
85-
The workflow will:
86-
1. Install dependencies
87-
2. Create the necessary environment variables
88-
3. Run Cypress tests in headless mode
89-
4. Upload the test reports as artifacts
65+
The project uses cypress-mochawesome-reporter for generating HTML reports. After running tests, reports can be found in the `cypress/reports` directory.

cypress.config.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@ export default defineConfig({
44
reporter: 'cypress-mochawesome-reporter',
55
reporterOptions: {
66
charts: true,
7-
reportPageTitle: 'Sauce Demo Test Report',
7+
reportPageTitle: 'SauceDemo Tests',
88
embeddedScreenshots: true,
99
inlineAssets: true,
10-
saveAllAttempts: false,
1110
},
1211
e2e: {
13-
baseUrl: 'https://www.saucedemo.com',
14-
pageLoadTimeout: 120000,
1512
setupNodeEvents(on, config) {
1613
require('cypress-mochawesome-reporter/plugin')(on);
1714

18-
// Task for logging
15+
// Task for detailed logging
1916
on('task', {
2017
log(message) {
21-
console.log(message);
18+
console.log(`LOG: ${message}`);
2219
return null;
23-
},
20+
}
2421
});
25-
26-
return config;
2722
},
23+
baseUrl: 'https://www.saucedemo.com',
24+
viewportWidth: 1280,
25+
viewportHeight: 720,
26+
defaultCommandTimeout: 5000,
2827
},
2928
});

cypress/e2e/lockedOutUser.cy.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// <reference types="cypress" />
2+
3+
describe('Sauce Demo - Locked Out User', () => {
4+
beforeEach(() => {
5+
cy.task('log', 'Starting Locked Out User Test');
6+
cy.visit('/');
7+
});
8+
9+
it('should show error message for locked out user', () => {
10+
// Load test data
11+
cy.fixture('testData.json').then((testData) => {
12+
// Get credentials from environment variables
13+
const username = Cypress.env('users').locked.username;
14+
const password = Cypress.env('users').locked.password;
15+
16+
// Login with locked out user credentials
17+
cy.task('log', `Typing username: ${username}`);
18+
cy.get('#user-name').type(username);
19+
20+
cy.task('log', `Typing password: ${password}`);
21+
cy.get('#password').type(password);
22+
23+
cy.task('log', 'Clicking login button');
24+
cy.get('#login-button').click();
25+
26+
// Verify error message
27+
cy.get('[data-test="error"]')
28+
.should('be.visible')
29+
.and('contain.text', testData.lockedOutUser.errorMessage);
30+
31+
cy.task('log', 'Locked Out User Test Completed Successfully');
32+
});
33+
});
34+
});

cypress/e2e/sauce_demo/login_scenarios.cy.ts

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

0 commit comments

Comments
 (0)