Menu

Page Object Model in Playwright with TypeScript: a minimal example (and a baseURL trap)
📰

Page Object Model in Playwright with TypeScript: a minimal example (and a baseURL trap)

DEV Community: typescript·AutomationDataCamp·about 14 hours ago
#jvRTlgrS
#devtotypescript#devto#dev#test#todo
Reading 0:00
15s threshold

A Page Object Model (POM) puts the selectors and actions of one page in one class, so your tests read like intentions and a UI change is fixed in one place. Here is the smallest version that is still useful, written for Playwright Test with TypeScript. I ran every file below against the public Playwright demo app, TodoMVC , with Playwright 1.65.

Project layout

playwright . config . ts pages / todo - page . ts tests / todo . spec . ts

Enter fullscreen mode

Exit fullscreen mode

The config, and a trap

// playwright.config.ts import { defineConfig } from '@playwright/test' ;

export default defineConfig ( { testDir : './tests' , use : { baseURL : 'https://demo.playwright.dev/todomvc/' } , } ) ;

Enter fullscreen mode

Exit fullscreen mode

Note the trailing slash. Playwright resolves page.goto() paths with the standard URL rules. With baseURL: '.../todomvc' (no slash), goto('/') goes to https://demo.playwright.dev/ , not to the app, and the first fill() times out waiting for an input that is not there.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.