This repository contains a Playwright-based test automation framework designed for robust and scalable testing of web applications and APIs. It supports multiple environments, custom fixtures, and integrates essential libraries for enhanced functionality.
- Features
- Project Structure
- Setup Instructions
- Running Tests
- Environment Configuration
- Custom Fixtures
- Reporting
- Best Practices
-
Playwright Integration:
- Multi-browser testing: Chromium, Firefox, WebKit.
- Integrated test runner and debugging UI.
-
API Testing:
APIClient
class for reusable API interactions.- Token-based authentication support.
-
UI Testing:
- Page Object Model (POM) structure for scalable UI tests.
UIClient
for common UI actions.
-
Environment Flexibility:
.env
files for local, development, and production setups.- Dynamic environment-based test execution.
-
Custom Fixtures:
token
,apiClient
,uiClient
for reusable test dependencies.
-
Advanced Utilities:
- Faker for dynamic test data.
- Date utilities via
date-fns
. - Encrypted storage using
crypto-js
.
-
Reporting:
- Integrated Playwright HTML reporter.
src/
├── actions/
│ ├── apiActions/
│ └── uiActions/
├── lib/
│ ├── api/
│ ├── generic/
│ └── ui/
├── resources/
│ ├── consts/
│ └── uploads/
tests/
├── api/
├── ui/
playwright.config.ts
.env.local
.env.dev
.env.production
actions/
: API and UI-specific actions organized by feature.lib/
: Core utilities, fixtures, and helper classes.resources/
: Constants, test data, and upload files.tests/
: Test case organization by type (API, UI).
-
Clone the repository:
git clone https://github.com/your-repo/playwright-automation.git cd playwright-automation
-
Install dependencies:
npm install
-
Set up environment variables:
- Create
.env.local
,.env.dev
, and.env.production
files based on the template:AUTO_DEALER_EMAIL=user@example.com AUTO_DEALER_PWD=secure-password BASE_URL=https://your-base-url.com API_BASE_URL=https://your-api-url.com ORGANIZATION_ID=12345
- Create
-
Verify installation:
npx playwright doctor
npx playwright test
@ContractType
:npx playwright test --grep @tag
Defined in package.json
:
-
Local:
npm run test:local
-
Development:
npm run test:dev
-
Debug Mode: Launch Playwright's test UI for debugging:
npm run debug
Environment variables are dynamically loaded using dotenv.
.env.local
: Configuration for local development..env.dev
: Configuration for the development environment.
Example .env
:
AUTO_DEALER_EMAIL=user@example.com
AUTO_DEALER_PWD=secure-password
BASE_URL=https://demo.example.com
API_BASE_URL=https://api.demo.example.com
ORGANIZATION_ID=12345
The framework includes reusable fixtures to simplify test dependencies:
token
: Dynamically generates a Bearer token for authenticated API requests.apiClient
: Reusable API client for interacting with REST APIs.uiClient
: Encapsulates common UI actions for consistency and reusability.
These fixtures are defined in fixtures.ts
and injected into tests automatically.
-
HTML Reporter: Generates detailed reports of test execution.
npx playwright show-report
-
Trace Viewer: Captures execution traces for debugging.
npx playwright test --trace on npx playwright show-trace trace.zip
-
TRX Reports: Uses
playwright-trx-reporter
for generating.trx
reports compatible with CI systems.
- Use Tags: Tag tests with meaningful labels (e.g.,
@ContractType
,@CleanUp
) for easy filtering. - Organize Tests: Follow the Page Object Model (POM) for scalable UI test structure.
- Avoid Hardcoding: Use environment variables for sensitive or dynamic values.
- Debug Effectively: Leverage Playwright's debug tools and trace viewer for troubleshooting.
- Optimize Tests: Ensure tests are independent and avoid redundant actions.
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
- Commit changes:
git commit -m "Add feature description"
- Push to the branch:
git push origin feature-name
- Create a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.