Page Object Model (POM) with Playwright

Page Object Model (POM) with Playwright: A Beginner's Guide

The Page Object Model (POM) is a popular design pattern used in test automation to make code cleaner, reusable, and maintainable. When combined with modern tools like Playwright, POM becomes even more powerful for building scalable end-to-end test frameworks.


In this blog, you’ll learn how to implement POM using Playwright with practical examples.


๐Ÿง  What is Page Object Model (POM)?

POM is a design pattern where each web page (or component) of the application is represented by a separate class or module. This class contains:


Locators for web elements


Methods that perform actions on those elements


✅ Benefits of POM:

Better code organization


Easier to maintain when UI changes


Promotes reusability of code


Separates test logic from UI interaction logic

⚙️ Setting Up Playwright

To get started with Playwright and POM:


bash

Copy

Edit

npm init -y

npm i -D @playwright/test

npx playwright install

๐Ÿ—‚️ Folder Structure for POM in Playwright

plaintext

Copy

Edit

project/

├── tests/

│   └── login.spec.ts

├── pages/

│   └── loginPage.ts

├── playwright.config.ts

└── package.json

๐Ÿ“„ Example: Creating a Page Object for a Login Page

ts

Copy

Edit

// pages/loginPage.ts

import { Page } from '@playwright/test';


export class LoginPage {

  readonly page: Page;

  readonly usernameInput;

  readonly passwordInput;

  readonly loginButton;


  constructor(page: Page) {

    this.page = page;

    this.usernameInput = page.locator('#username');

    this.passwordInput = page.locator('#password');

    this.loginButton = page.locator('button[type="submit"]');

  }


  async login(username: string, password: string) {

    await this.usernameInput.fill(username);

    await this.passwordInput.fill(password);

    await this.loginButton.click();

  }

}

๐Ÿงช Writing a Test Using the Page Object

ts

Copy

Edit

// tests/login.spec.ts

import { test, expect } from '@playwright/test';

import { LoginPage } from '../pages/loginPage';


test('Valid login', async ({ page }) => {

  const loginPage = new LoginPage(page);

  await page.goto('https://example.com/login');


  await loginPage.login('admin', 'admin123');

  await expect(page).toHaveURL('https://example.com/dashboard');

});

๐Ÿ” Best Practices for POM with Playwright

Create one class per page or component.


Keep locators and methods encapsulated within page objects.


Use async/await for all Playwright operations.


Reuse page object methods across multiple test cases.


✅ Conclusion

Using the Page Object Model with Playwright allows you to build robust, readable, and maintainable automated test suites. It separates test logic from UI details, making your automation more scalable and easier to manage as your project grows.

Learn Playwright Training Course in Hyderabad

Read More

Handling Different Selectors in Playwright

Understanding Locators in Playwright

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Tosca for API Testing: A Step-by-Step Tutorial

Waits in Playwright: Explicit, Implicit, and Auto