Complete preparation guide โ Basic to Advanced
| Feature | Playwright | Selenium |
|---|---|---|
| Auto-wait | โ Built-in | โ Manual waits needed |
| Browsers | Chromium, Firefox, WebKit | All browsers via WebDriver |
| Speed | Faster | Slower |
| Network Mock | โ Built-in | โ 3rd party needed |
| Trace/Debug | โ Trace Viewer | โ Limited |
| Language | JS/TS, Python, Java, C# | Same + more |
| Feature | locator() | elementHandle() |
|---|---|---|
| Auto-wait | โ Yes โ retries automatically | โ No โ snapshot at that moment |
| Lazy | โ Evaluated on action | โ Resolved immediately |
| Recommended | โ Yes (modern) | โ Legacy, avoid |
| Use case | All normal interactions | Only if you need JS handle |
locator() over elementHandle() in modern Playwright.click(), Playwright checks: visible stable enabled not obscured
| Method | What it returns | Hidden elements |
|---|---|---|
| textContent() | Raw text including hidden elements | โ Included |
| innerText() | Visible text only (human-readable) | โ Excluded |
page.$() is a shorthand for page.querySelector() โ returns an ElementHandle immediately (snapshot). page.locator() is lazy and auto-waits.
| page.locator() | page.$() | |
|---|---|---|
| Returns | Locator (lazy) | ElementHandle (snapshot) |
| Auto-wait | โ Yes | โ No |
| Recommended | โ Always | โ Avoid |
locator('.list').locator('.item')expect() assertions natively| frameLocator() | page.frame() | |
|---|---|---|
| Selector | CSS/XPath of iframe element | name or URL attribute |
| Returns | FrameLocator (lazy, chainable) | Frame object |
| Auto-wait | โ Yes | โ No |
| Recommended | โ Modern approach | Legacy |
| expect().toBeVisible() | locator.isVisible() | |
|---|---|---|
| Type | Assertion (test fails if wrong) | Returns boolean |
| Auto-retry | โ Yes โ retries until timeout | โ No โ instant check |
| Use case | Assertions in test | Conditional logic |
| Hook | Runs | Use case |
|---|---|---|
| beforeEach | Before EVERY test | Navigate to page, login, reset state |
| afterEach | After EVERY test | Cleanup, logout, close modals |
| beforeAll | Once before all tests in describe | DB setup, create test data |
| afterAll | Once after all tests | DB cleanup, close connections |
beforeAll shares state between tests โ use carefully! beforeEach is safer for isolated tests.--with-deps in CI to install browser OS dependencies on Linux.storageState is a major time-saver โ login once, skip login in all other tests!