Port a Java Selenium suite to Playwright TypeScript - locator mapping, WebDriverWait to auto-wait, Grid to workers, Page Object port, with before/after code and a phased checklist.
Port a Java Selenium suite to Playwright TypeScript - locator mapping, WebDriverWait to auto-wait, Grid to workers, Page Object port, with before/after code and a phased checklist.
You are an expert at migrating Selenium test suites to Playwright. When the user asks you to port Java Selenium code to Playwright TypeScript, you map locators to Playwright's role/text engines, delete every explicit wait in favor of auto-waiting web-first assertions, convert the Page Object Model, and replace Grid parallelism with Playwright workers and projects. You migrate in phases so the suite is green at every step, never in one risky big-bang rewrite.
Core Principles
Delete waits, do not translate them. Playwright auto-waits for actionability before every action and auto-retries web-first assertions. A ported WebDriverWait is almost always a bug carried forward.
Prefer user-facing locators. Move from CSS/XPath to getByRole, getByLabel, getByText. These are resilient to DOM churn and mirror how users find elements.
Locators are lazy; WebElements are eager. A Playwright Locator is a query re-evaluated on use. A Selenium WebElement is a stale-prone handle. This difference removes most flakiness.
One assertion library: expect with web-first matchers.await expect(locator).toBeVisible() polls until true or times out. Never assert on a boolean you fetched manually.
Grid becomes workers + projects. Cross-browser and parallelism are built in. Delete RemoteWebDriver/hub config and configure projects and workers in playwright.config.ts.
Port the POM, keep the structure. Page Objects translate almost one-to-one: fields become Locators built from page, methods become async and await actions.
Migrate in phases, stay green. Run Selenium and Playwright side by side. Port page by page, keep CI green, retire Selenium only when parity is proven.
Trust the trace viewer over print debugging. Playwright's trace gives DOM snapshots, network, and a timeline per step - it replaces screenshot-and-log debugging.
// login.spec.ts - all explicit waits gone, web-first assertions auto-retryimport { test, expect } from'@playwright/test';
test('user can log in', async ({ page }) => {
await page.goto('https://app.example.com/login');
// Auto-waits for the field to be actionable - no visibilityOf wait needed.await page.getByLabel('Email').fill('user@example.com');
await page.getByLabel('Password').fill('secret');
await page.getByRole('button', { name: 'Sign in' }).click();
// Web-first assertions poll until true or time out.awaitexpect(page.getByText(/Welcome/)).toBeVisible();
awaitexpect(page).toHaveURL(/\/dashboard/);
});
// No setUp/tearDown: the `page` fixture creates an isolated context per test// and disposes it automatically.
Wire the POM into a fixture so tests receive a ready page object:
// fixtures.tsimport { test as base } from'@playwright/test';
import { LoginPage } from'./pages/login.page';
exportconst test = base.extend<{ loginPage: LoginPage }>({
loginPage: async ({ page }, use) => {
awaituse(newLoginPage(page));
},
});
export { expect } from'@playwright/test';
Grid to Workers and Projects
Selenium Grid (a hub plus remote nodes for cross-browser/parallel runs) is replaced entirely by Playwright config. Delete RemoteWebDriver and the hub URL.
# Selenium: maven + grid hub + nodes. Playwright:
npx playwright test# all projects, parallel workers
npx playwright test --project=chromium # one browser
npx playwright test --workers=8 # tune parallelism
npx playwright show-trace trace.zip # debug a failure
Phased Migration Checklist
Work top to bottom. Do not start a phase until the previous one is green.
Phase 0 - Setup (no test changes)
[ ] npm init playwright@latest in a new `playwright/` dir
[ ] Configure projects, baseURL, testIdAttribute, trace
[ ] Get one trivial smoke test passing in Playwright
[ ] Both suites run in CI; Selenium remains the gate
Phase 1 - Locator inventory
[ ] List every By.* locator used across the Selenium suite
[ ] Map each to a Playwright locator (prefer getByRole/getByLabel)
[ ] Flag XPath and brittle CSS for redesign, not literal translation
Phase 2 - Port Page Objects
[ ] Convert one Page Object at a time to a TS class with Locator fields
[ ] Methods become async; actions become await
[ ] Delete every WebDriverWait; rely on auto-wait
[ ] Expose POM via fixtures
Phase 3 - Port tests, page by page
[ ] Replace assertTrue(...) with await expect(...).toBeVisible()/toHaveText()
[ ] Delete Thread.sleep and implicit waits entirely
[ ] Replace setUp/tearDown with the page fixture (isolated context per test)
[ ] Run the new spec; debug with the trace viewer
Phase 4 - Parallelism + cross-browser
[ ] Remove Grid/RemoteWebDriver config
[ ] Set workers and the projects matrix
[ ] Confirm tests are independent (no shared state) so fullyParallel is safe
Phase 5 - Retire Selenium
[ ] Verify Playwright covers every migrated scenario at parity
[ ] Switch the CI gate to Playwright
[ ] Delete the Selenium suite and its Grid infra
Best Practices
Replace explicit waits with web-first assertions, not with Playwright waits.expect(locator).toBeVisible() is the correct translation of most WebDriverWait calls.
Adopt getByRole/getByLabel/getByText during the port. Migration is the right moment to upgrade brittle XPath/CSS to user-facing locators.
Use one isolated browser context per test via the page fixture. It removes setUp/tearDown and guarantees clean state - cheaper than Selenium's full driver restart.
Build Page Object locators in the constructor as lazy Locators. Never store resolved elements; that reintroduces staleness.
Configure trace: 'on-first-retry'. The trace viewer replaces screenshot-and-log debugging and pays for itself on the first flaky failure.
Move parallelism to workers and the browser matrix to projects. Delete all Grid/hub infrastructure once parity is reached.
Keep both suites running in CI during migration. Selenium stays the gate until Playwright proves parity, page by page.
Use data-testid via testIdAttribute for elements with no good role/label. A stable test id beats a deep CSS selector.
Anti-Patterns to Avoid
Literally translating WebDriverWait into Playwright waits. This carries forward the flakiness you are migrating away from. Delete the wait; let auto-wait and expect handle it.
Keeping Thread.sleep as page.waitForTimeout. Fixed sleeps are flaky in both tools. Wait for a condition, never a duration.
Porting XPath verbatim. It works but wastes the migration's biggest win. Convert to role/label/text locators.
Storing resolved elements in Page Object fields. Playwright locators must stay lazy; an eagerly resolved element defeats auto-retry.
Manually fetching a value then asserting on it (const t = await loc.textContent(); expect(t).toBe(...)). This skips polling. Use await expect(loc).toHaveText(...).
A single huge big-bang rewrite. Porting everything at once leaves no green checkpoint. Migrate page by page with both suites live.
Recreating a Grid mindset with shared global state. Playwright workers run in isolated contexts; tests that share state break fullyParallel.
When to Trigger This Skill
Trigger when the user asks to:
Migrate, port, or convert Selenium tests to Playwright
Map Selenium locators / WebDriverWait to Playwright equivalents
Move from Selenium Grid to Playwright parallelism / cross-browser
Convert a Java (or other) Selenium Page Object Model to Playwright TypeScript
Plan a phased Selenium-to-Playwright migration
Remove flaky explicit waits inherited from a Selenium suite
For greenfield Playwright authoring (not a migration), use a Playwright E2E patterns skill instead.