Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Page Object Model (POM)

Playwright Page Object Model (POM)

This chapter introduces the design philosophy and implementation of the Page Object Model to help you organize large test suites.


What is POM

The Page Object Model is a test design pattern that encapsulates a page's elements and operations into separate classes.

Each page object represents a page or component in a web application, containing that page's locators and operation methods.


Advantages of POM

Advantage Description
Simplify test writing Test code uses high-level APIs (such as loginPage.login('user', 'pass')) instead of raw Locator operations
Reduce maintenance cost Changes to page structure only require modifying the Page Object class, not every test
Improve Readability Test code reads more like business language, understandable even to non-technical people
Avoid duplication The same locators and operations only need to be defined once

Create a Page Object class

Below, using the RUNOOB website as an example, create a Page Object for a login page.

Example

// File path: pages/LoginPage.ts

import

{

type Page

,

type Locator

,

expect

}

from

'@playwright/test'

;

export

class

LoginPage

{

  readonly page

:

Page

;

 

// Declare all locators as read-only properties

  readonly usernameInput

:

Locator

;

  readonly passwordInput

:

Locator

;

  readonly loginButton

:

Locator

;

  readonly errorMessage

:

Locator

;

  constructor

(

page

:

Page

)

{

   

this

.

page

=

page

;

   

// Initialize all locators in the constructor

   

this

.

usernameInput

=

page.

getByLabel

(

'Username'

)

;

   

this

.

passwordInput

=

page.

getByLabel

(

'Password'

)

;

   

this

.

loginButton

=

page.

getByRole

(

'button'

,

{

name

:

'Login'

}

)

;

   

this

.

errorMessage

=

page.

getByTestId

(

'login-error'

)

;

 

}

 

// Navigate to the login page

  async

goto

(

)

{

    await

this

.

page

.

goto

(

'/login'

)

;

 

}

 

// Perform the login operation

  async login

(

username

:

string

,

password

:

string

)

{

    await

this

.

usernameInput

.

fill

(

username

)

;

    await

this

.

passwordInput

.

fill

(

password

)

;

    await

this

.

loginButton

.

click

(

)

;

 

}

 

// Verify the error message for login failure

  async expectLoginError

(

message

:

string

)

{

    await expect

(

this

.

errorMessage

)

.

toBeVisible

(

)

;

    await expect

(

this

.

errorMessage

)

.

toContainText

(

message

)

;

 

}

}


Use the Page Object in tests

Example

// File path: tests/login.spec.ts

import

{

test

,

expect

}

from

'@playwright/test'

;

import

{

LoginPage

}

from

'../pages/LoginPage'

;

test.

describe

(

'Login feature'

,

(

)

=>

{

  test

(

'Logged in successfully with correct username and password'

,

async

(

{

page

}

)

=>

{

   

const

loginPage

=

new

LoginPage

(

page

)

;

   

// Using the Page Object approach

    await loginPage.

goto

(

)

;

    await loginPage.

login

(

'runoob_user'

,

'correct_password'

)

;

   

// Verify redirect after successful login

    await expect

(

page

)

.

toHaveURL

(

'/dashboard'

)

;

 

}

)

;

  test

(

'Login failed with wrong password'

,

async

(

{

page

}

)

=>

{

   

const

loginPage

=

new

LoginPage

(

page

)

;

    await loginPage.

goto

(

)

;

    await loginPage.

login

(

'runoob_user'

,

'wrong_password'

)

;

   

// Using the Page Object assertion method

    await loginPage.

expectLoginError

(

'Incorrect username or password'

)

;

 

}

)

;

}

)

;


Multi-level page objects

In real projects, you can create multiple Page Objects, which can be associated with each other.

Example

// File path: pages/DashboardPage.ts

import

{

type Page

,

type Locator

,

expect

}

from

'@playwright/test'

;

export

class

DashboardPage

{

  readonly page

:

Page

;

  readonly welcomeText

:

Locator

;

  readonly logoutButton

:

Locator

;

  constructor

(

page

:

Page

)

{

   

this

.

page

=

page

;

   

this

.

welcomeText

=

page.

getByText

(

'Welcome back'

)

;

   

this

.

logoutButton

=

page.

getByRole

(

'button'

,

{

name

:

'Exit'

}

)

;

 

}

  async expectWelcomeMessage

(

username

:

string

)

{

    await expect

(

this

.

welcomeText

)

.

toContainText

(

username

)

;

 

}

  async logout

(

)

{

    await

this

.

logoutButton

.

click

(

)

;

 

}

}

Combined use of page objects:

Example

test

(

'Complete login and logout flow'

,

async

(

{

page

}

)

=>

{

 

const

loginPage

=

new

LoginPage

(

page

)

;

 

const

dashboardPage

=

new

DashboardPage

(

page

)

;

 

// Login

  await loginPage.

goto

(

)

;

  await loginPage.

login

(

'runoob_user'

,

'password'

)

;

 

// Verify dashboard

  await dashboardPage.

expectWelcomeMessage

(

'runoob_user'

)

;

 

// Logout

  await dashboardPage.

logout

(

)

;

  await expect

(

page

)

.

toHaveURL

(

'/login'

)

;

}

)

;


POM best practices

Practice Description
Locators centralized in the constructor Define all locators in the constructor; do not create them dynamically in methods
Methods return a Page Object Action methods can return a new Page Object (e.g., returning DashboardPage after login)
Encapsulate assertions but do not overuse them Encapsulate commonly used assertion combinations as methods; write simple assertions directly in the test
Use composition instead of inheritance Avoid deep inheritance; use composition to associate multiple Page Objects
One set of objects covers one page/component Keep granularity moderate; do not create a separate class for every small element

POM is not mandatory.

For small projects with only a few tests, writing Locators directly in the tests is also clearer. When there are more than 5 test files, consider introducing POM.