Slide 1

Slide 1 text

Playwright: Wachablöse für Cypress? Rainer Hahnekamp 23.6.2022

Slide 2

Slide 2 text

About Me... ● Rainer Hahnekamp ANGULARarchitects.io ● Trainings and Consulting @RainerHahnekamp Professional NgRx https://www.ng-news.com https://www.youtube.com /c/RainerHahnekamp

Slide 3

Slide 3 text

Agenda ● History, Context, Landscape ● Playwright Architecture ● Live-Coding ○ A first test ○ Selectors and actions ○ Tooling ● Advanced features

Slide 4

Slide 4 text

2010 2005 2015 2020 2022 Selenium RC Selenium 2 CDP Cypress Playwright

Slide 5

Slide 5 text

E2E Framework Selenium: Outside browser testing Test Webdriver Webdriver Webdriver Webdriver

Slide 6

Slide 6 text

Cypress: Inside browser testing E2E Framework Browser

Slide 7

Slide 7 text

Two extremes Selenium / Webdriver ✅ Full browser access ✅ W3C Standard ✅ Scalable infrastructure ✅ Cross browser testing ⛔ Developer Experience improvable ⛔ Flakiness Cypress ✅ Superior developer experience ✅ Stable ⛔ Limited to CDP ⛔ Limited functionalities ● Workarounds available ● Not applicable

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Playwright: Middle way ● Outside browser testing ● Everything from one hand ○ Browsers, Testing Framework, Visual Regression ● Limited to CDP, incl. "Safari" ● Developer experience ● Access existing Selenium Grid via CDP ● Backed by Microsoft

Slide 10

Slide 10 text

Playwright Architecture

Slide 11

Slide 11 text

First test import { test, expect } from "@playwright/test"; test.describe("lectio", () => { test("greeting on landing page", async ({ page }) => { await page.goto("http://localhost:3000"); const greeting = page.locator("main div p"); await expect(greeting).toContainText("Welcome to Lectio"); }); });

Slide 12

Slide 12 text

Playwright's powerhorse: page.locator ● Returns a promise of a DOM query ● Chainable for actions ● Argument for assertions (expect) ● Different selectors (not just CSS) ○ Text selectors ○ Data-testid ○ Location-based ○ XPath ○ React/Vue experimental ● Nestable ● Shadow DOM "piercing"

Slide 13

Slide 13 text

Actionability build-in ● Based on action, Playwright runs checks before the action ● Checks ○ Attached ○ Visible ○ Stable ○ Receive Events (no overlay) ○ Enabled ○ Editable

Slide 14

Slide 14 text

Tools

Slide 15

Slide 15 text

Code Generator

Slide 16

Slide 16 text

Debugging mode

Slide 17

Slide 17 text

VSCode integration

Slide 18

Slide 18 text

Trace View

Slide 19

Slide 19 text

Advanced features - visual regression import { expect, test } from "@playwright/test"; test.describe("Visual Regression", () => { test("talk card", async ({ page }) => { await page.goto("http://localhost:3000"); await page.locator("data-testid=mnu-talks").click(); const screenshot = await page .locator("data-testid=talk-card") .first() .screenshot(); await expect(screenshot).toMatchSnapshot("talk-card.png"); }); });

Slide 20

Slide 20 text

Advanced features - Network stubbing test("visually regress the talk card with stubbed network", async ({page}) => { await page.goto("/"); page.route("/talks**", (route) => { route.fulfill({ contentType: "application/json", body: JSON.stringify(mockedTalks), }); }); await page.locator('[data-testid="mnu-talks"]').click(); const card = await page.locator("data-testid=talk-card").first().screenshot(); expect(card).toMatchSnapshot("talk-card.png"); });

Slide 21

Slide 21 text

Further features ● Parallel execution incl. sharding ● Session storage for authentication ● Video recording, screenshotting ● Beta ○ Component Testing ○ Android Testing ● Out-of-browser features ○ Down- and uploading ○ Dialog handling ○ Drag & Drop ○ Multiple Tabs ○ Domain Switching

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Summary ✅ Out-of-browser testing without flakiness ✅ Young but already mature framework ✅ iOS & MacOS support on Windows or Linux ✅ Backed by Microsoft ✅ Powerful Traceviewer ⛔ Multiple support tools instead of central UI ⛔ Little IDE support (expect VSCode)