Skip to content

Commit 63594b2

Browse files
committed
Add Cypress tests for SauceDemo website
1 parent 6393ecd commit 63594b2

19 files changed

+398
-226
lines changed

.github/workflows/cypress.yml

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

1616
- name: Setup Node.js
17-
uses: actions/setup-node@v4
17+
uses: actions/setup-node@v3
1818
with:
19-
node-version: '20.x'
19+
node-version: 18
2020

21-
- name: Install dependencies
21+
- name: Install Dependencies
2222
run: npm ci
2323

2424
- name: Create env file
2525
run: |
2626
echo '{
27-
"standardUser": "standard_user",
28-
"lockedOutUser": "locked_out_user",
27+
"standardUsername": "standard_user",
28+
"lockedOutUsername": "locked_out_user",
2929
"password": "secret_sauce"
3030
}' > cypress.env.json
3131
3232
- name: Cypress run
33-
uses: cypress-io/github-action@v6
33+
uses: cypress-io/github-action@v5
3434
with:
3535
browser: chrome
36-
headless: true
36+
headed: false
3737

38-
- name: Upload test results
38+
- name: Upload Test Results
39+
uses: actions/upload-artifact@v3
3940
if: always()
40-
uses: actions/upload-artifact@v4
4141
with:
42-
name: cypress-reports
43-
path: cypress/reports
42+
name: cypress-report
43+
path: cypress/reports/html
4444
retention-days: 30
45-
45+
46+
deploy-report:
47+
needs: cypress-run
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Checkout
51+
uses: actions/checkout@v3
52+
53+
- name: Download Cypress Reports
54+
uses: actions/download-artifact@v3
55+
with:
56+
name: cypress-report
57+
path: cypress/reports/html
58+
4659
- name: Deploy to GitHub Pages
47-
if: success()
48-
uses: peaceiris/actions-gh-pages@v3
60+
uses: JamesIves/github-pages-deploy-action@v4
4961
with:
50-
github_token: ${{ secrets.GITHUB_TOKEN }}
51-
publish_dir: ./cypress/reports/html
52-
enable_jekyll: true
62+
folder: cypress/reports/html
63+
branch: gh-pages

.gitignore

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1+
# Dependencies
12
node_modules/
2-
cypress.env.json
3+
4+
# Cypress
35
cypress/videos/
46
cypress/screenshots/
5-
cypress/reports/
7+
cypress.env.json
8+
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
24+
25+
# IDE files
26+
.idea/
27+
.vscode/
28+
*.sublime-project
29+
*.sublime-workspace
30+
31+
# macOS files
32+
.DS_Store
33+
.AppleDouble
34+
.LSOverride

README.md

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,66 @@
1-
# Sauce Demo Cypress Tests
1+
# SauceDemo Cypress Test Automation
22

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

55
## Project Structure
66

77
```
88
├── cypress/
9-
│ ├── e2e/ # Test files
10-
│ ├── fixtures/ # Test data
11-
│ ├── pages/ # Page objects
12-
│ └── support/ # Support files and commands
13-
├── cypress.config.ts # Cypress configuration
14-
├── cypress.env.json # Environment variables (gitignored)
15-
├── package.json # Project dependencies
16-
└── tsconfig.json # TypeScript configuration
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
1720
```
1821

19-
## Prerequisites
22+
## Test Scenarios
2023

21-
- Node.js (v14 or higher)
22-
- npm (v6 or higher)
24+
1. **Valid Login Test** - Tests successful login with standard user
25+
2. **Locked Out User Test** - Tests error message for locked out user
2326

24-
## Installation
27+
## Setup
2528

2629
1. Clone the repository
2730
2. Install dependencies:
28-
```bash
29-
npm install
30-
```
31+
32+
```bash
33+
npm install
34+
```
35+
3136
3. Create a `cypress.env.json` file with the following content:
32-
```json
33-
{
34-
"standardUser": "standard_user",
35-
"lockedOutUser": "locked_out_user",
36-
"password": "secret_sauce"
37-
}
38-
```
37+
38+
```json
39+
{
40+
"standardUsername": "standard_user",
41+
"lockedOutUsername": "locked_out_user",
42+
"password": "secret_sauce"
43+
}
44+
```
3945

4046
## Running Tests
4147

42-
To run tests in headless mode:
48+
### Headless Mode
49+
50+
To run all tests in headless mode:
51+
52+
```bash
53+
npm test
54+
```
55+
56+
### Interactive Mode
57+
58+
To open Cypress in interactive mode:
59+
4360
```bash
44-
npm run cy:run
61+
npm run cy:open
4562
```
4663

47-
## Test Reports
64+
## Reports
4865

49-
Test reports are generated using cypress-mochawesome-reporter and can be found in the `cypress/reports/html` directory after test execution.
66+
Test reports are generated using cypress-mochawesome-reporter and can be found in the `cypress/reports/html` directory after running the tests.

cypress.config.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
import { defineConfig } from 'cypress'
1+
import { defineConfig } from 'cypress';
22

33
export default defineConfig({
44
reporter: 'cypress-mochawesome-reporter',
5+
reporterOptions: {
6+
charts: true,
7+
reportPageTitle: 'SauceDemo Test Report',
8+
embeddedScreenshots: true,
9+
inlineAssets: true,
10+
},
511
e2e: {
12+
baseUrl: 'https://www.saucedemo.com',
613
setupNodeEvents(on, config) {
714
require('cypress-mochawesome-reporter/plugin')(on);
815

9-
// Custom task for logging
16+
// Task for detailed logging
1017
on('task', {
1118
log(message) {
12-
console.log(message)
13-
return null
14-
},
15-
})
19+
console.log(message);
20+
return null;
21+
}
22+
});
1623
},
17-
baseUrl: 'https://www.saucedemo.com',
18-
supportFile: 'cypress/support/e2e.ts',
19-
specPattern: 'cypress/e2e/**/*.cy.ts',
20-
viewportWidth: 1280,
21-
viewportHeight: 720
2224
},
23-
})
25+
});

cypress/e2e/locked-out-user.cy.ts

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

cypress/e2e/lockedOutUser.cy.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import LoginPage from '../pages/LoginPage';
2+
3+
describe('Locked Out User Test', () => {
4+
it('should show error message for locked out user', () => {
5+
cy.logAction('Starting Locked Out User Test');
6+
7+
// Get credentials from environment variables
8+
const username = Cypress.env('lockedOutUsername');
9+
const password = Cypress.env('password');
10+
11+
// Login with locked out user credentials
12+
LoginPage.visit()
13+
.login(username, password);
14+
15+
// Verify error message is displayed
16+
LoginPage.verifyErrorMessage('Epic sadface: Sorry, this user has been locked out');
17+
18+
cy.logAction('Completed Locked Out User Test');
19+
});
20+
});

cypress/e2e/valid-login.cy.ts

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

cypress/e2e/validLogin.cy.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import LoginPage from '../pages/LoginPage';
2+
import InventoryPage from '../pages/InventoryPage';
3+
4+
describe('Valid Login Test', () => {
5+
it('should login successfully with standard user', () => {
6+
cy.logAction('Starting Valid Login Test');
7+
8+
// Get credentials from environment variables
9+
const username = Cypress.env('standardUsername');
10+
const password = Cypress.env('password');
11+
12+
// Login with valid credentials
13+
LoginPage.visit()
14+
.login(username, password);
15+
16+
// Verify successful login
17+
InventoryPage.isLoaded();
18+
19+
// Logout to avoid cache issues
20+
InventoryPage.logout();
21+
22+
cy.logAction('Completed Valid Login Test');
23+
});
24+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lockedOutErrorMessage": "Epic sadface: Sorry, this user has been locked out.",
3+
"inventoryPageTitle": "Swag Labs",
4+
"loginPageTitle": "Swag Labs"
5+
}

cypress/pages/InventoryPage.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class InventoryPage {
2+
// Selectors
3+
private menuButton = '#react-burger-menu-btn';
4+
private logoutLink = '#logout_sidebar_link';
5+
private appLogo = '.app_logo';
6+
7+
// Methods
8+
isLoaded() {
9+
cy.get(this.appLogo).should('be.visible');
10+
cy.logAssertion('Verified inventory page is loaded');
11+
return this;
12+
}
13+
14+
clickMenuButton() {
15+
cy.get(this.menuButton).click();
16+
cy.logAction('Clicked menu button');
17+
return this;
18+
}
19+
20+
clickLogout() {
21+
cy.get(this.logoutLink).click();
22+
cy.logAction('Clicked logout link');
23+
return this;
24+
}
25+
26+
logout() {
27+
this.clickMenuButton();
28+
this.clickLogout();
29+
return this;
30+
}
31+
}
32+
33+
export default new InventoryPage();

0 commit comments

Comments
 (0)