krishworkstech.com

Page object models

Introduction

Extensive test suites can be organized to enhance the ease of creation and upkeep. Page object models serve as one method to organize your test suite.

A page object signifies a component of your web application. For instance, an e-commerce web application may include a home page, a listings page, and a checkout page. Each of these can be represented through page object models.

Page objects facilitate the authoring process by providing a higher-level API tailored to your application, and they streamline maintenance by consolidating element selectors in a single location, thereby enabling the creation of reusable code to prevent redundancy.

PAGE OBJECT MODEL - COVER

Implementation

We will develop a helper class named PlaywrightDevPage to encapsulate the common operations performed on the playwright.dev page. Internally, this class will utilize the page object.

				
					// code snippet:
import { expect, type Locator, type Page } from '@playwright/test';

export class PlaywrightDevPage {
  readonly page: Page;
  readonly getStartedLink: Locator;
  readonly gettingStartedHeader: Locator;
  readonly pomLink: Locator;
  readonly tocList: Locator;

  constructor(page: Page) {
    this.page = page;
    this.getStartedLink = page.locator('a', { hasText: 'Get started' });
    this.gettingStartedHeader = page.locator('h1', { hasText: 'Installation' });
    this.pomLink = page.locator('li', {
      hasText: 'Guides',
    }).locator('a', {
      hasText: 'Page Object Model',
    });
    this.tocList = page.locator('article div.markdown ul > li > a');
  }

  async goto() {
    await this.page.goto('https://playwright.dev');
  }

  async getStarted() {
    await this.getStartedLink.first().click();
    await expect(this.gettingStartedHeader).toBeVisible();
  }

  async pageObjectModel() {
    await this.getStarted();
    await this.pomLink.click();
  }
}
				
			

Now we can use the PlaywrightDevPage class in our tests.

				
					// code snippet:
import { test, expect } from '@playwright/test';
import { PlaywrightDevPage } from './playwright-dev-page';

test('getting started should contain table of contents', async ({ page }) => {
  const playwrightDev = new PlaywrightDevPage(page);
  await playwrightDev.goto();
  await playwrightDev.getStarted();
  await expect(playwrightDev.tocList).toHaveText([
    `How to install Playwright`,
    `What's Installed`,
    `How to run the example test`,
    `How to open the HTML test report`,
    `Write tests using web first assertions, page fixtures and locators`,
    `Run single test, multiple tests, headed mode`,
    `Generate tests with Codegen`,
    `See a trace of your tests`
  ]);
});

test('should show Page Object Model article', async ({ page }) => {
  const playwrightDev = new PlaywrightDevPage(page);
  await playwrightDev.goto();
  await playwrightDev.pageObjectModel();
  await expect(page.locator('article')).toContainText('Page Object Model is a common pattern');
});

				
			

Playwright Essentials: A Step-by-Step Journey into Test Automation

cover - part 9

Part 01: Master Playwright Testing – Framework from Scratch Guide

cover - part 10

Part 02: How to install Playwright

cover - part 11

Part 03: Basic Playwright Test Structure

cover - part 13 (1)

Part 04: Importance of Playwright configuration file and its details to run the tests

cover - part 17

Part 05: Writing Tests in PlayWright

cover - part 19

Part 06: Playwright Actions Illustrated

cover - part 13 (1)

Part 07: Auto-waiting

cover - part 17

Part 08: Handling the Dialogs

Rectangle 26833

Part 09: Download & Handling frames

Rectangle 26834

Part 10: Locators

cover - part 17

Part 11: Pages & New Window

Rectangle 26836

Part 12: Page object models

Scroll to Top
  • Schematic design
  • PCB and schematic source files
  • Assembling drawing files
  • Providing prototype/sample and production PCB service
  • Testing and validation of designed hardware
  • HIPAA
  • Azure Key
  • Management
  • ES, Checksum,
  • MD5sum
  • AWS
  • Azure
  • GCP
  • DigitalOcean
  • Kotlin
  • Python
  • Tensorflow
  • Computer Vision
  • ECG
  • SPO2
  • Heart Rate
  • Glucometer
  • Blood Pressure
  • UX UI Process
  • Figma and FigJam
  • Adobe Suite
  • Selenium Java
  • Postman
  • Swagger
  • Jmeter
  • SQL
  • Java Scripter
  • Test ng
  • Extents Reports
  • Flutter
  • Java
  • Kotlin
  • Swift
  • Dart
  • React JS
  • Python
  • NodeJS
  • Django
  • HTML, CSS, JS
RDBMS
  • PostgreSQL
  • Oracle
  • MySQL
  • MariaDB
No SQL Based
  • MongoDB
  • GCP
  • FirestoreDB
  • DynamoDB
  • Azure
  • CosmosDB
  • AWS