Selenium to Playwright Migration Part 3: The Master Cheat Sheet — Every Pattern Mapped
This is part of the Selenium to Playwright Migration Series. Follow the complete 7-part tutorial to migrate your test suite from Selenium Java to Playwright TypeScript.
The complete side-by-side reference: every Selenium Java pattern mapped to its Playwright TypeScript equivalent across assertions, locators, interactions, waits, navigation, and configuration.
Contents
Assertions
| Selenium (Java) | Playwright (TypeScript) |
|---|---|
assertEquals(expected, actual) | await expect(locator).toHaveText('expected') |
assertTrue(element.isDisplayed()) | await expect(locator).toBeVisible() |
assertFalse(element.isEnabled()) | await expect(locator).toBeDisabled() |
assertThat(element).hasAttribute("x","y") | await expect(locator).toHaveAttribute('x','y') |
assertThat(driver.getTitle()).contains("x") | await expect(page).toHaveTitle(/x/) |
assertThat(driver.getCurrentUrl()).contains("/dash") | await expect(page).toHaveURL(/dash/) |
Locators
| Selenium (Java) | Playwright (TypeScript) |
|---|---|
driver.findElement(By.id("x")) | page.locator('#x') |
driver.findElement(By.name("x")) | page.locator('[name="x"]') |
driver.findElement(By.className("x")) | page.locator('.x') |
driver.findElement(By.xpath("//btn")) | page.getByRole('button') |
driver.findElement(By.linkText("x")) | page.getByRole('link', {name:'x'}) |
driver.findElements(By.css(".item")) | page.locator('.item') (auto-multiple) |
@FindBy(id="x") WebElement el; | readonly el = page.locator('#x') |
Interactions
| Selenium (Java) | Playwright (TypeScript) |
|---|---|
element.click() | await locator.click() |
element.sendKeys("text") | await locator.fill('text') |
element.clear() | await locator.clear() |
Select(el).selectByVisibleText("x") | await locator.selectOption({label:'x'}) |
element.sendKeys(Keys.ENTER) | await locator.press('Enter') |
Actions(driver).moveToElement(el).perform() | await locator.hover() |
Actions(driver).doubleClick(el).perform() | await locator.dblclick() |
Actions(driver).dragAndDrop(src,tgt).perform() | await src.dragTo(tgt) |
Waits
| Selenium (Java) | Playwright (TypeScript) |
|---|---|
WebDriverWait(driver,10).until(visible(el)) | await expect(locator).toBeVisible() |
Thread.sleep(3000) | Remove entirely (auto-wait) |
FluentWait with polling | await expect(locator).toBeVisible({timeout:30000}) |
implicitlyWait(10, SECONDS) | Not needed (auto-wait built in) |
ExpectedConditions.alertIsPresent() | page.on('dialog', handler) |
Key pattern: Remove ALL explicit waits. Playwright’s expect() auto-retries until the condition is met or timeout occurs. This single change eliminates 80% of flaky test failures.
Next: Part 4: Page Objects and Test Conversion — transforming your POM, eliminating base classes, and introducing the Module layer.
