Microsoft’s Playwright quietly released in May of 2020. Written by a group of engineers who originally wrote the Puppeteer library, Playwright is intentionally similar to Puppeteer. While Microsoft has provided an example of a page object within Playwright’s documentation, the example is fairly simple and does not delve into the use of page properties or helper methods.

What is a Page Object?

A page object is an abstraction of a web page using a programming language. The intention is to represent all of the page within code so as to take action against specific elements. Page objects are routinely used in the field of test automation where a Quality Engineer creates objects and tests for the purpose of testing application user journeys.

This tutorial will focus on building a page object for the DemoQA Bookstore application login page. The login page features a username input, a password input, a login button, and a new user button.

Getting Started

In order to begin, we need to install our dependencies. The following packages should be installed:

  • pytest
  • pytest-playwright
  • playwright

Simply install the above using pip (or the dependency manager of your choosing).

The pytest library is included as it is the most popular test runner for Python. The pytest-playwright package provides simple fixtures for use within tests, such as the page fixture which automates browser setup. It can also be configured to take screenshots upon failure which is helpful in debugging flakey or failing tests.

#automation-testing #python #quality-assurance #pytest

Page Object Modeling with Python and Playwright
4.65 GEEK