Basic to Advanced โ Complete Interview Preparation Guide
| Feature | Playwright | Selenium |
|---|---|---|
| Auto-wait | โ Built-in | โ Manual waits |
| Shadow DOM | โ Auto-pierce | โ Complex workarounds |
| Network Mock | โ Built-in | โ Third-party needed |
| Parallel Tests | โ Native | โ Grid setup needed |
| Trace Viewer | โ Built-in | โ None |
| Mobile Emulation | โ Built-in devices | โ Limited |
| Speed | โก Faster | Slower |
| locator() | elementHandle() | |
|---|---|---|
| Auto-wait | โ Retries automatically | โ Snapshot, no retry |
| DOM evaluation | Lazy โ on each action | Eager โ at call time |
| Dynamic elements | โ Handles perfectly | โ Can go stale |
| Recommended | โ Always | โ Avoid in new code |
| Method | Returns | Hidden elements |
|---|---|---|
| textContent() | All raw text (including hidden) | โ Included |
| innerText() | Visible text only | โ Excluded |
page, context, browser, request.| Mode | Description | Use case |
|---|---|---|
| Headless | No browser UI, faster | CI/CD pipelines |
| Headed | Browser visible | Debugging, development |
| UI Mode | Interactive test explorer | Best for local dev |
| page.locator() | page.$() | |
|---|---|---|
| Type | Lazy Locator | ElementHandle (snapshot) |
| Auto-wait | โ Yes | โ No |
| Stale risk | None (re-queries) | High (can go stale) |
| Use | โ Always preferred | โ Legacy only |
| frameLocator() | page.frame() | |
|---|---|---|
| Selector | CSS/XPath of iframe element | name or URL attribute |
| Auto-wait | โ Yes | โ No |
| Returns | FrameLocator (chainable) | Frame object |
| Recommended | โ Modern | Legacy |
| expect().toBeVisible() | locator.isVisible() | |
|---|---|---|
| Type | Assertion (fails test) | Returns boolean |
| Auto-retry | โ Retries until timeout | โ Instant check |
| Use case | Test assertions | Conditional logic (if/else) |
| Timeout Type | Default | Config Key |
|---|---|---|
| Test timeout | 30s | timeout |
| Expect timeout | 5s | expect.timeout |
| Action timeout | same as test | use.actionTimeout |
| Navigation timeout | same as test | use.navigationTimeout |
addInitScript injects JavaScript that runs before any page scripts. Useful for mocking browser APIs, faking dates, disabling animations.| Problem | Fix |
|---|---|
| Hardcoded sleep | Use waitFor / expect assertions |
| Race condition | Wait for network/URL/element state |
| Shared state | Use fresh context per test |
| External API | Mock the API call |
| Timing issues | Use expect() auto-retry, not isVisible() |
Page in constructorlogin(), addToCart()getBy* locators for stability