Playwright Basic Interview Practice
Prepare for your next SDET or QA interview with curated questions and answers.
Q1: What is Playwright and why is it used in automation testing?
Answer: Playwright is an open-source end-to-end automation testing framework developed by Microsoft. It is used to automate web applications across multiple browsers.
Why it is used:
- Fast and reliable UI testing
- Auto-waiting for elements
- API testing support
- Parallel execution
- Mobile/device emulation
- Supports Chromium, Firefox, WebKit browsers
Example
javascript
const { chromium } = require('@playwright/test');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://google.com');
console.log(await page.title());
await browser.close();
})();Q2: How Playwright different from Selenium or Cypress?
Answer:
| Feature | Playwright | Selenium | Cypress |
|---|---|---|---|
| Multi-browser support | Yes | Yes | Limited |
| Auto wait | Yes | No | Yes |
| Multi-tab support | Yes | Yes | Weekly |
| Fast execution | High | Medium | High |
| Mobile emulation | Yes | Limited | No |
| Network interception | Yes | Limited | Yes |
Summary:
- Playwright = Modern + Fast + Powerful
- Selenium = Old + Large ecosystem
- Cypress = Frontend focused
Q3: Which browsers are supported by Playwright?
Answer:
Playwright supports:
- Chromium (Chrome, Edge)
- Firefox
- WebKit (Safari engine)
Example Test
javascript
const { chromium, firefox, webkit } = require('@playwright/test');Q4: How to install Playwright and set it up for a new project?
Answer:
Step 1: Create project
Bash
npm init playwright@latestStep 2: Install dependencies
Bash
npm installStep 3: Run test
Bash
npx playwright testFolder structure:
Bash
tests/
playwright.config.js
package.jsonQ5: What is locators in Playwright?
Answer: Locators is used to find web elements on the page.
Common locators:
- id
- text
- CSS
- XPath
- Role-based
Example:
javascript
page.locator('#username');
page.locator('text=Login');
page.getByRole('button', { name: 'Submit' });Q6: What's the difference between page.click() and locator.click() in playwright?
Answer:
| page.click() | locator.click() |
|---|---|
| Direct click using selector | Uses locator object |
| Older style | Recommended |
| Less reusable | Reusable |
Example:
javascript
await page.click('#login');
const btn = page.locator('#login');
await btn.click();Best Practice:
- Use locator.click()
Q7: How does Playwright handle waits automatically?
Answer:
Playwright automatically waits for::
- Element visible
- Enabled
- Stable
- Attached to DOM
No need for many sleep() commands.
Example:
javascript
await page.locator('#submit').click();Playwright waits until button is ready.
Q8: How to handle dropdowns and input fields?
Answer:
Input field:
javascript
await page.fill('#username', 'admin');Dropdown:
javascript
await page.selectOption('#country', 'India');Multi-select:
javascript
await page.selectOption('#skills', ['NodeJS', 'JavaScript']);Q9: How to take screenshots and record videos in Playwright?
Answer:
Screenshot
javascript
await page.screenshot({ path: 'page.png' });Full page screenshot:
javascript
await page.screenshot({ path: 'full.png', fullPage: true });Video recording:
javascript
const context = await browser.newContext({
recordVideo: { dir: 'videos/' }
});Q10: How to open multiple browser tabs or contexts in playwright?
Answer:
New tab:
javascript
const page2 = await context.newPage();New context:
javascript
const context2 = await browser.newContext();Use case:
- Multiple users login
- Separate sessions
Q11: What's the difference between page, browser, and browserContext?
Answer:
| Term | Meaning |
|---|---|
| Browser | Full browser instance |
| BrowserContext | Separate session/incognito |
| Page | Single tab |
Example
javascript
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();Q12: How to handle alerts and pop-ups in playwright?
Answer:
JavaScript alert:
javascript
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.accept();
});Dismiss alert:
javascript
await dialog.dismiss();Q13: How to emulate mobile devices or geolocation?
Answer:
Mobile emulation:
javascript
const { devices } = require('@playwright/test');
const context = await browser.newContext({
...devices['iPhone 13']
});Geolocation
javascript
const context = await browser.newContext({
geolocation: { latitude: 18.5204, longitude: 73.8567 },
permissions: ['geolocation']
});Q14: How to handle different browser windows in Playwright?
Answer: When new window opens, use context.waitForEvent('page')
Mobile emulation:
javascript
const [newPage] = await Promise.all([
context.waitForEvent('page'),
page.click('text=Open Window')
]);
await newPage.waitForLoadState();
console.log(await newPage.title());Q15: What command is used to generate Playwright tests automatically?
Answer: Use Codegen command.
Command:
Bash
npx playwright codegen https://example.comPurpose:
- Opens browser
- Records actions
- Generates automation script automatically
Example Output:
javascript
await page.goto('https://example.com');
await page.click('text=Login');
await page.fill('#user', 'admin');