appendix-e

Appendix E. Playwright

 

This chapter covers

  • Using Playwright to test web components
  • Creating and using Locator objects
  • Making assertions about expected state
  • Defining, generating, and running tests
  • Emulating devices to test operations across a variety of them

Playwright is an excellent tool for implementing end-to-end tests for web applications, including those that use web components. These are automated tests that drive a web application the way a user would: opening pages, clicking buttons, typing into inputs, checking rendered state, and verifying behavior across browsers and devices.

Created by Microsoft, Playwright is a fast browser-testing tool with built-in parallel execution, reliable cross-browser support, and native handling of multiple tabs and windows. It is an alternative to its competitor Cypress.

To begin learning about Playwright, let’s discuss more of its capabilities, at a high level.

E.1 Deeper into Playwright

Playwright tests can be run in all the major browser engines, including Chromium (used by Chrome and Microsoft Edge), Firefox, and WebKit (used by Safari). This raises confidence that applications will work correctly regardless of the web browser users choose.

You can emulate specific kinds of mobile devices. This includes configuring the screen size, user agent string, and display scale factor, as well as enabling touch support. This is useful for applications that are expected to run in mobile device web browsers.

E.2 Initial project

E.3 Playwright setup

E.4 Test actions

E.5 Locators

E.6 Assertions

E.7 Adding tests

E.8 Generating tests

E.9 Running tests

E.10 Emulating devices

E.11 Screenshot tests

E.12 One more example

E.13 Summary