Compatibility
The Playwright team says most Puppeteer APIs can be used as they are, and the guide covers moving to both the Playwright Library and Playwright Test. Beyond Chromium, Playwright also drives Firefox and WebKit, which Puppeteer does not support.
Before you switch
- Import the browser explicitly:
const { chromium } = require('playwright'), thenchromium.launch().firefoxandwebkitwork the same way. - Replace
browser.createIncognitoBrowserContext(...)withbrowser.newContext(...), and use browser contexts for state isolation. - Rename the calls the cheat sheet lists:
page.setViewportbecomespage.setViewportSize,page.waitForNetworkIdle(...)becomespage.waitForLoadState('networkidle'), andwaitUntil: 'networkidle2'becomeswaitUntil: 'networkidle'. - Move element actions onto locators:
page.click(selector)becomespage.locator(selector).click(), and likewise forfocus,hoverandtap.page.selectbecomesselectOption,page.typebecomesfill, and file uploads throughelementHandle.uploadFile(...)becomesetInputFiles(...). - Cookies move from the page to the context:
browserContext.cookies,browserContext.addCookiesandbrowserContext.clearCookies. - For tests, the guide recommends Playwright Test over Jest plus Puppeteer. Import
testandexpectfrom@playwright/test, takepagefrom the test’s parameters, and replacepage.$eval()checks with web-first assertions such asexpect(locator).toContainText(...).
Pitfalls
- Locators are strict. Any action on a locator that targets a DOM element throws if more than one element matches the selector.
ElementHandleis discouraged.page.$(...)andpage.$x(...)have no direct replacement in the cheat sheet: the guide points at locators instead.- The cheat sheet maps
page.deleteCookie(...cookies)tobrowserContext.clearCookies(), so check deletions of specific cookies against the Playwright API reference. - Playwright Test creates an isolated
Pagefor each test. To share one page across tests, create it intest.beforeAll()and close it intest.afterAll(). page.waitForNavigationandpage.waitForSelectorstill exist, but auto-waiting makes them unnecessary in many cases, and the guide saysnetworkidleis rarely useful for the same reason.- To intercept and change requests, use
page.route().