Skip to content

Commit 8503f47

Browse files
committed
Add Cypress tests with Page Object Model for SauceDemo
1 parent eb02a2f commit 8503f47

21 files changed

+206
-2039
lines changed

.github/workflows/cypress.yml

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

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

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

24-
# Create cypress.env.json file using GitHub secrets
25-
- name: Create Cypress environment file
24+
- name: Create env file
2625
run: |
2726
echo '{
2827
"users": {
@@ -37,22 +36,8 @@ jobs:
3736
}
3837
}' > cypress.env.json
3938
40-
- name: Cypress run
41-
uses: cypress-io/github-action@v4
39+
- name: Run Cypress tests
40+
uses: cypress-io/github-action@v5
4241
with:
4342
browser: chrome
44-
headed: false
45-
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
55-
if: always()
56-
with:
57-
name: cypress-report
58-
path: cypress/reports
43+
headless: true

.gitignore

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
# Dependencies
1+
# Dependency directories
22
node_modules/
33

4-
# Cypress generated files
4+
# TypeScript compiled files
5+
*.js
6+
*.map
7+
8+
# Cypress
59
cypress/videos/
610
cypress/screenshots/
711
cypress/downloads/
8-
cypress/reports/
9-
10-
# Environment variables (contains credentials)
1112
cypress.env.json
1213

1314
# OS specific files
1415
.DS_Store
1516
Thumbs.db
1617

17-
# IDE specific files
18+
# Editor directories and files
1819
.idea/
1920
.vscode/
20-
21-
# Log files
22-
*.log
23-
npm-debug.log*
21+
*.swp
22+
*.swo

README.md

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,73 @@
11
# SauceDemo Cypress Tests
22

3-
This project contains automated tests for the SauceDemo website using Cypress with TypeScript and follows the Page Object Model pattern.
4-
5-
## Test Scenarios
6-
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
3+
This project contains Cypress end-to-end tests for the SauceDemo website using TypeScript and the Page Object Model pattern.
94

105
## Project Structure
116

127
```
138
├── 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
9+
│ ├── e2e/ # Test files
10+
│ ├── fixtures/ # Test data
11+
│ ├── pages/ # Page Object Model files
12+
── support/ # Custom commands and utilities
13+
│ └── tsconfig.json # TypeScript configuration
14+
├── cypress.config.ts # Cypress configuration
15+
├── cypress.env.json # Environment variables (not committed to git)
16+
├── package.json # Project dependencies and scripts
17+
└── README.md # Project documentation
2318
```
2419

2520
## Prerequisites
2621

2722
- Node.js (v14 or later)
2823
- npm (v6 or later)
2924

30-
## Installation
25+
## Setup
3126

3227
1. Clone the repository
3328
2. Install dependencies:
34-
35-
```bash
36-
npm install
37-
```
29+
```
30+
npm install
31+
```
32+
3. Create a `cypress.env.json` file with the following content:
33+
```json
34+
{
35+
"users": {
36+
"standard": {
37+
"username": "standard_user",
38+
"password": "secret_sauce"
39+
},
40+
"locked": {
41+
"username": "locked_out_user",
42+
"password": "secret_sauce"
43+
}
44+
}
45+
}
46+
```
3847

3948
## Running Tests
4049

41-
### Run tests in headless mode:
50+
### Headless Mode
51+
52+
To run tests in headless mode:
4253

43-
```bash
54+
```
4455
npm run cy:run
4556
```
4657

47-
### Open Cypress Test Runner:
58+
### Interactive Mode
59+
60+
To open Cypress in interactive mode:
4861

49-
```bash
62+
```
5063
npm run cy:open
5164
```
5265

53-
## CI/CD
54-
55-
This project uses GitHub Actions for continuous integration. The workflow:
66+
## Test Cases
5667

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
68+
1. **Valid Login**: Tests successful login using standard_user
69+
2. **Locked Out User**: Tests error handling with locked_out_user
6270

63-
## Test Reporting
71+
## Logging
6472

65-
The project uses cypress-mochawesome-reporter for generating HTML reports. After running tests, reports can be found in the `cypress/reports` directory.
73+
The tests include a custom task for logging which displays detailed information in the terminal during test runs.

cypress.config.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
import { defineConfig } from 'cypress';
22

33
export default defineConfig({
4-
reporter: 'cypress-mochawesome-reporter',
5-
reporterOptions: {
6-
charts: true,
7-
reportPageTitle: 'SauceDemo Tests',
8-
embeddedScreenshots: true,
9-
inlineAssets: true,
10-
},
114
e2e: {
125
setupNodeEvents(on, config) {
13-
require('cypress-mochawesome-reporter/plugin')(on);
14-
15-
// Task for detailed logging
166
on('task', {
177
log(message) {
18-
console.log(`LOG: ${message}`);
8+
console.log(message);
199
return null;
2010
}
2111
});
2212
},
2313
baseUrl: 'https://www.saucedemo.com',
24-
viewportWidth: 1280,
25-
viewportHeight: 720,
26-
defaultCommandTimeout: 5000,
14+
chromeWebSecurity: false,
15+
video: false
2716
},
2817
});

cypress/e2e/lockedOutUser.cy.ts

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

cypress/e2e/login.cy.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/// <reference types="cypress" />
2+
3+
import LoginPage from '../pages/LoginPage';
4+
import ProductsPage from '../pages/ProductsPage';
5+
6+
describe('SauceDemo Login Tests', () => {
7+
beforeEach(() => {
8+
cy.task('log', 'Starting a new test');
9+
LoginPage.visit();
10+
});
11+
12+
it('should login successfully with valid credentials', () => {
13+
cy.task('log', 'Running test: should login successfully with valid credentials');
14+
15+
// Get test data from environment
16+
cy.fixture('testData.json').then((testData) => {
17+
// Login with valid credentials
18+
LoginPage.login(
19+
Cypress.env('users').standard.username,
20+
Cypress.env('users').standard.password
21+
);
22+
23+
// Verify successful login
24+
ProductsPage.getProductsHeader()
25+
.should('be.visible')
26+
.and('have.text', testData.pageTexts.productsHeader);
27+
});
28+
});
29+
30+
it('should show error message for locked out user', () => {
31+
cy.task('log', 'Running test: should show error message for locked out user');
32+
33+
cy.fixture('testData.json').then((testData) => {
34+
// Login with locked out user
35+
LoginPage.login(
36+
Cypress.env('users').locked.username,
37+
Cypress.env('users').locked.password
38+
);
39+
40+
// Verify error message
41+
LoginPage.getErrorMessage()
42+
.should('be.visible')
43+
.and('have.text', testData.errorMessages.lockedOutUser);
44+
});
45+
});
46+
});

cypress/e2e/validLogin.cy.ts

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

cypress/fixtures/testData.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
{
2+
"urls": {
3+
"baseUrl": "https://www.saucedemo.com"
4+
},
25
"errorMessages": {
36
"lockedOutUser": "Epic sadface: Sorry, this user has been locked out."
47
},
5-
"pageTitle": {
6-
"products": "Products"
7-
},
8-
"validLogin": {
9-
"expectedUrl": "/inventory.html",
10-
"headerText": "Products"
11-
},
12-
"lockedOutUser": {
13-
"errorMessage": "Epic sadface: Sorry, this user has been locked out."
8+
"pageTexts": {
9+
"productsHeader": "Products"
1410
}
1511
}

0 commit comments

Comments
 (0)