Real-World Playwright Framework

Real-World Playwright Framework

Building a few Playwright tests is relatively simple. Building an automation framework that hundreds of tests and multiple team members can maintain is a completely different challenge. In this guide, we’ll walk through a real-world Playwright framework used in enterprise projects. You’ll learn how the different layers work together and how to structure your project for long-term maintainability.


What is an Automation Framework?

An automation framework is a structured collection of code, configuration, utilities, and best practices that makes writing, executing, and maintaining automated tests easier.

A well-designed framework provides:

  • Reusable code
  • Consistent project structure
  • Easy maintenance
  • Better reporting
  • Faster execution
  • Team collaboration

Instead of each tester writing automation differently, the framework provides a standard approach.


High-Level Framework Flow

A typical Playwright automation flow looks like this:

                Developer
                    │
                    ▼
              Test Case (.spec.ts)
                    │
                    ▼
          Page Object Classes
                    │
                    ▼
         Utility / Helper Classes
                    │
                    ▼
          Playwright Test Runner
                    │
                    ▼
         Browser & Application
                    │
                    ▼
       Assertions & Validations
                    │
                    ▼
       Reports / Screenshots
                    │
                    ▼
             CI/CD Pipeline

Each layer has a clear responsibility, making the framework easier to understand and maintain.


Recommended Enterprise Project Structure

playwright-framework
├── tests/
├── pages/
├── components/
├── fixtures/
├── utils/
├── helpers/
├── test-data/
├── constants/
├── config/
├── reports/
├── screenshots/
├── videos/
├── traces/
├── playwright.config.ts
├── package.json
└── README.md

This structure keeps responsibilities separated and supports framework growth.


Folder-by-Folder Explanation

tests/

Contains your actual test scenarios.

Example:

tests
├── login.spec.ts
├── checkout.spec.ts
├── search.spec.ts

These files describe business flows and assertions—not implementation details.


pages/

Contains Page Object Model (POM) classes.

Example:

pages
├── LoginPage.ts
├── HomePage.ts
├── CartPage.ts

Each page class stores:

  • Locators
  • Navigation methods
  • Page actions

components/

Many applications have reusable UI components such as:

  • Navigation bars
  • Side menus
  • Headers
  • Footers
  • Date pickers
  • Search panels

Instead of duplicating these across page objects, place them in reusable component classes.


fixtures/

Fixtures prepare the test environment.

Typical examples:

  • Browser setup
  • Login users
  • API clients
  • Database connections
  • Test cleanup

Fixtures reduce repeated setup code.


utils/

Utility classes contain generic reusable functions.

Examples:

  • Date utilities
  • Random data generation
  • JSON readers
  • Screenshot helpers
  • Wait helpers
  • File utilities

Utilities should not contain business logic.


helpers/

Helpers perform higher-level operations.

Examples:

  • Login helper
  • API helper
  • Email helper
  • Database helper

Unlike utilities, helpers usually interact with external systems.


test-data/

Store test data separately from test scripts.

Example:

test-data
├── users.json
├── products.json
├── orders.json

Keeping test data external makes tests easier to maintain.


constants/

Store reusable constants.

Example:

export const TIMEOUT = 30000;
export const BASE_URL = "https://example.com";

Avoid hard-coded values throughout the framework.


config/

Configuration files may include:

  • Environment settings
  • URLs
  • Browser configuration
  • Credentials
  • Timeouts

Different environments (Development, QA, Staging, Production) should have separate configurations.


Execution Flow

Let’s see what happens when a test starts.

Run Test
Fixture Starts Browser
Page Object Created
Navigate to Application
Interact with UI
Assertions
Capture Screenshot (if needed)
Generate Report
Close Browser

This predictable flow improves reliability and simplifies debugging.


Framework Layers

Think of the framework as layered architecture:

Business Tests
Page Objects
Components
Utilities
Playwright APIs
Browser

Each layer should communicate only with the layer directly below it.


Integrating API Testing

Many modern frameworks combine UI and API automation.

Example flow:

Create User (API)
Login (UI)
Verify Dashboard

This approach speeds up tests by preparing data through APIs instead of the user interface.


Reporting Layer

After execution, the framework should generate:

  • HTML Report
  • Screenshots
  • Videos
  • Trace files
  • Console logs

These artifacts help investigate failures quickly.


CI/CD Integration

A mature framework integrates seamlessly with CI/CD.

Example workflow:

Developer Push
GitHub Actions
Install Dependencies
Run Playwright Tests
Generate Reports
Publish Results

This enables automated validation for every code change.


Best Practices

  • Follow the Page Object Model.
  • Keep tests independent.
  • Avoid duplicate locators.
  • Use semantic locators (getByRole, getByLabel, getByTestId).
  • Separate test data from test logic.
  • Store configuration outside the code.
  • Capture traces and screenshots on failures.
  • Use environment variables for secrets.
  • Review reports after each pipeline execution.

Common Mistakes

Putting Everything in One File

Large test files become difficult to maintain. Separate concerns into pages, utilities, and helpers.


Hard-Coding Values

Store URLs, credentials, and timeouts in configuration files or environment variables.


Mixing Business Logic with Utility Methods

Keep utilities generic. Business workflows belong in page objects or dedicated service classes.


Ignoring Folder Structure

A consistent project structure makes onboarding new team members much easier.


Frequently Asked Questions

Is the Page Object Model enough for a framework?

No. POM is an important design pattern, but a complete framework also includes fixtures, utilities, configuration, reporting, test data management, and CI/CD integration.


How should I organize Playwright projects?

Use separate folders for tests, pages, components, fixtures, utilities, configuration, and reports to keep responsibilities clear.


Should test data be stored inside test files?

No. Keeping test data in external JSON, YAML, or CSV files improves maintainability and reusability.


Can API testing be part of the same framework?

Yes. Combining API and UI automation is a common enterprise practice that speeds up test execution and simplifies test setup.


What is the biggest benefit of a well-designed framework?

It improves maintainability, reduces code duplication, enables team collaboration, and supports long-term project growth.


Conclusion

A successful Playwright automation framework is much more than a collection of test scripts. It combines clear project structure, reusable page objects, fixtures, utilities, configuration management, reporting, and CI/CD integration into a scalable solution.

By following the architecture and practices outlined in this guide, you can build a framework that is easier to maintain, faster to execute, and capable of supporting both small projects and enterprise-scale automation initiatives.


Related Articles

Playwright Cloud Execution BrowserStack & LambdaTest

✓ Playwright Architecture Explained

✓ Playwright Setup with TypeScript

✓ Playwright Setup with Java

✓ Playwright Locator Strategies

✓ Playwright Auto-Waiting Mechanism

✓ Complete Playwright Tutorial


Discover more from Rotebit

Subscribe to get the latest posts sent to your email.

6 Comments

Leave a Reply