What are the steps to apply Page Object Model in a Playwright project?


Implementing Page Object Model in Playwright

Let's start by creating a new folder named "pages" in your project directory. This will house all the page objects for your application. Each page object should represent a single web page or component of the application.

For each page object, create a new file in the "pages" folder. Name it after the corresponding web page or component. For instance, for a login form, you can create a file named "LoginPage.ts".

In each file, define a class that represents the corresponding web page or component. This class should contain methods that interact with elements on that web page or component. These methods should return other classes representing other pages or components as needed.

Here's an example of how you could define a LoginPage class:

import { Page } from 'playwright';

export class LoginPage {
  private readonly page: Page;

  constructor(page: Page) {
    this.page = page;

  async navigate() {
    await this.page.goto('https://ray.run/login');

  async login(username: string, password: string) {
    await this.page.fill('#username', username);
    await this.page.fill('#password', password);
    await Promise.all([
    return new HomePage(this.page);

In this example, we have two methods - navigate() and login(). The navigate() method navigates to the login form URL while login() fills out and submits the login form using provided credentials. It also returns an instance of another POM class representing another part of our application - in this case, HomePage.

Once you've defined all your POM classes, you can use them in your tests by importing them into test files where they are needed:

import { chromium } from 'playwright';
import { LoginPage } from '../pages/LoginPage';

describe('Login', () => {
  let browser;
  beforeAll(async () => {
     browser = await chromium.launch();
   afterAll(async () => {
     await browser.close();

And that's it! You've successfully implemented the Page Object Model in your Playwright project.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Questions

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.