Skip to content

wojdaa/playwright-swaglabs-playground

Repository files navigation

playwright-playground-swaglabs

Comprehensive end-to-end test suite for the SauceDemo e-commerce application, built with Playwright Test and TypeScript.

This project serves as a realistic automation playground, demonstrating:

  • clean and scalable Page Object Model (POM),
  • tagged smoke / regression / accessibility / api / network / security / seo / visual suites,
  • strong use of helpers + utilities,
  • CI integration (GitHub Actions + local Jenkins),
  • best practices for configuration & environment separation,
  • example visual regression workflow,
  • example accessibility testing with Axe.

πŸš€ Tech Stack

  • Playwright Test (@playwright/test)
  • TypeScript
  • axe-core (@axe-core/playwright)
  • dotenv
  • GitHub Actions
  • Jenkins (optional)

πŸ“ Project Structure

.
β”œβ”€ .github/
β”‚  └─ workflows/
β”‚     └─ playwright.yml            # GitHub Actions pipeline
β”œβ”€ pages/                          # Page Object Model classes
β”‚  β”œβ”€ base.page.ts
β”‚  β”œβ”€ login.page.ts
β”‚  β”œβ”€ inventory.page.ts
β”‚  β”œβ”€ cart.page.ts
β”‚  β”œβ”€ checkout-step-one.page.ts
β”‚  β”œβ”€ checkout-step-two.page.ts
β”‚  β”œβ”€ checkout-complete.page.ts
β”‚  └─ ...
β”œβ”€ tests/
β”‚  β”œβ”€ e2e/
β”‚  β”‚  β”œβ”€ authentication/
β”‚  β”‚  β”œβ”€ checkout/
β”‚  β”‚  β”œβ”€ navigation/
β”‚  β”‚  β”œβ”€ performance-user/
β”‚  β”‚  β”œβ”€ problem-user/
β”‚  β”‚  β”œβ”€ product-browsing/
β”‚  β”‚  └─ shopping-cart/
β”‚  β”œβ”€ accessibility/               # WCAG compliance tests
β”‚  β”œβ”€ api/                         # API endpoint tests
β”‚  β”œβ”€ network/                     # Network resilience tests
β”‚  β”œβ”€ security/                    # OWASP security tests
β”‚  β”œβ”€ seo/                         # SEO metadata tests
β”‚  └─ visual/                      # Visual regression tests
β”œβ”€ test-data/
β”‚  └─ users.json
β”œβ”€ utils/
β”‚  β”œβ”€ config.ts                    # env + global config
β”‚  β”œβ”€ test-helpers.ts              # login helpers, visual snapshot utils, etc.
β”‚  └─ ...
β”œβ”€ IMPLEMENTATION-SUMMARY.md       # summary of scripts & tags
β”œβ”€ TEST-TAGS.md                    # full tag documentation
β”œβ”€ playwright.config.ts
β”œβ”€ package.json
└─ README.md

βš™οΈ Configuration & Environment Variables

This project uses environment variables for configuration.

Local development

Create a .env file in project root:

BASE_URL=https://www.saucedemo.com
PASSWORD=your_password_here
API_KEY=your_api_key_here

utils/config.ts loads these values via dotenv.

CI (GitHub Actions / Jenkins)

Environment variables are injected via:

  • GitHub Secrets β†’ PASSWORD: ${{ secrets.SWAGLABS_PASSWORD }} and API_KEY: ${{ secrets.API_KEY }}
  • Jenkins Credentials β†’ PASSWORD = credentials('swaglabs_password') and API_KEY = credentials('api_key')

No secrets stored in repository.

▢️ Running Tests

Install dependencies

npm install
npx playwright install

Run all tests

npm test
# or
npx playwright test

Tagged suites

# Smoke tests
npm run test:smoke

# Full regression
npm run test:regression

# Accessibility (axe-core)
npm run test:accessibility

# API tests
npm run test:api

# Network resilience
npm run test:network

# Security tests (OWASP)
npm run test:security

# SEO validation
npm run test:seo

# Visual regression
npm run test:visual

Browser / execution modes

npm run test:firefox
npm run test:webkit

npm run test:ui        # Playwright UI mode
npm run test:headed    # headful tests
npm run test:debug     # with debugger

HTML report

npm run report

🏷️ Test Tags

All tag documentation is stored in:

  • TEST-TAGS.md
  • IMPLEMENTATION-SUMMARY.md

Short summary:

  • @smoke β€” critical path
  • @regression β€” extended workflow coverage
  • @accessibility β€” axe-core WCAG checks
  • @api β€” API endpoint validation
  • @network β€” network resilience and error handling
  • @security β€” OWASP security testing
  • @seo β€” SEO metadata validation
  • @visual β€” visual regression tests

Usage:

npx playwright test --grep @smoke

πŸ–ΌοΈ Visual Regression Testing

Visual snapshots live next to the test:

tests/visual/tablet-view.spec.ts-snapshots/
  visual-tablet-order-complete.png
  ...

Visual helper:

await takeVisualSnapshot(page, 'visual-tablet-order-complete', {fullPage: true});

Updating snapshots

If UI changes intentionally:

npx playwright test tests/visual/tablet-view.spec.ts --update-snapshots

Tip: Regenerate snapshots on Linux (same as GitHub Actions runner) for consistent results.

β™Ώ Accessibility Testing (axe-core)

Accessibility tests use @axe-core/playwright.

Example:

const accessibilityScanResults = await new AxeBuilder({ page }).analyze();
expect(accessibilityScanResults.violations).toEqual([]);

Execute with:

npm run test:accessibility

πŸ”Œ API Tests (Reqres.in)

  • folder: /tests/api
  • stack: Playwright APIRequestContext + TypeScript
  • includes: GET /users, POST /users, POST /login (negative)

In order to use POST requests, configure your API key in the .env file or CI/CD credentials.

🌐 Network Resilience Testing

Network tests validate application behavior under various network conditions using Playwright's route mocking capabilities:

  • API Failure Handling - Testing graceful degradation when API endpoints return errors (500, 503)
  • Image Loading Failures - Verifying UI remains functional when resource loading fails
  • Maintenance Scenarios - Simulating maintenance pages and service unavailability
  • Error Messages - Validating user-friendly error messages are displayed
  • Fallback Behavior - Ensuring proper fallback mechanisms are in place

Test coverage uses route interception and mocking:

// Example: Mock API failure
await page.route('**/api/inventory', async (route) => {
    await route.fulfill({
        status: 500,
        contentType: 'application/json',
        body: JSON.stringify({ error: 'Internal Server Error' }),
    })
})

// Example: Mock maintenance page
await page.route('**/inventory.html', async (route) => {
    await route.fulfill({
        status: 200,
        contentType: 'text/html',
        body: '<h1>Maintenance in progress</h1>',
    })
})

Execute with:

npm run test:network

All network tests are tagged with @network.

πŸ”’ Security Testing (OWASP)

Security tests follow OWASP Top 10 guidelines and include:

  • SQL Injection - Testing injection prevention in login and input fields
  • XSS (Cross-Site Scripting) - Validating proper encoding/escaping of user inputs
  • Broken Access Control - Verifying authentication and authorization controls
  • Session Management - Testing session fixation, termination, and handling
  • Authentication Security - Checking password field types, autocomplete, form methods
  • Security Misconfiguration - Validating HTTPS, security headers, cookie attributes
  • Input Validation - Testing special characters, null bytes, long strings

Test coverage:

// Example: XSS Testing
await attemptLogin(page, "<script>alert('XSS')</script>", 'test')
await verifyNoXssExecution(page)

// Example: SQL Injection Testing
await attemptLogin(page, "' OR '1'='1", 'test')
await verifyLoginFailed(page)

Execute with:

npm run test:security

All security tests are tagged with @security and use helper functions from utils/security-helpers.ts.

πŸ” SEO Testing

SEO tests validate basic metadata and page structure:

  • Title Tags - Verifying page titles are present and descriptive
  • Meta Descriptions - Checking meta description tags
  • Charset Declaration - Validating UTF-8 charset
  • Canonical Links - Testing canonical URL implementation
  • Metadata Consistency - Ensuring consistent metadata across pages

Test coverage includes Login, Inventory, and Cart pages.

Example:

await verifyBasicSeoMetadata(page)
const metaCharset = page.locator('meta[charset]')
await expect(metaCharset).toHaveAttribute('charset', 'utf-8')

Execute with:

npm run test:seo

All SEO tests are tagged with @seo.

πŸ”„ CI / CD

GitHub Actions

Workflow file: .github/workflows/playwright.yml

Pipeline:

  • Checkout repo
  • Install Node LTS
  • npm ci
  • Install browsers + Linux deps
  • Run full Playwright test suite
  • Upload HTML report as artifact

Secrets:

  • SWAGLABS_PASSWORD β†’ mapped to PASSWORD env

Jenkins (optional)

Local Jenkins in Docker using jenkins/jenkins:lts-jdk17.

Setup:

# Pull and run Jenkins
docker run -d -p 8080:8080 -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  --name jenkins jenkins/jenkins:lts-jdk17

# Get initial admin password
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Configuration:

  1. Access Jenkins at http://localhost:8080
  2. Install suggested plugins + HTML Publisher Plugin
  3. Configure credentials (Manage Jenkins β†’ Credentials):
    • Add Secret Text: swaglabs_password with your password value
    • Add Secret Text: api_key with your API key value
  4. Create new Pipeline job pointing to your repository

Pipeline stages:

  • Checkout repository
  • Install dependencies with npm ci
  • Install Playwright browsers with npx playwright install --with-deps
  • Run tagged test suites:
    • Smoke tests (all branches)
    • Regression tests (main/master only)
    • Accessibility tests (main/master only)
    • Security tests (main/master only)
    • API tests (all branches)
    • Network tests (all branches)
    • SEO tests (all branches)
  • Publish results:
    • JUnit XML report
    • Archived artifacts (screenshots, traces, videos)
    • HTML test report

This integration is included as a learning/demo setup.

🀝 Contributing & Extending

When adding new tests:

Follow POM conventions in pages/.

Add tags (@smoke, @regression, etc.).

Prefer helpers for shared flows (login, addToCart, checkout).

Keep snapshot baseline names consistent.

Update README if adding new suites, commands or tags.

πŸ“„ License

ISC

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •