Interview questions with answers
Playwright interview questions and answers
Locators, auto-waiting, fixtures, tracing and cross-browser runs. Open any question to read its model answer; the multiple-choice set marks the right option and explains it.
- Subjective
- 30
- Objective
- 30
Subjective questions 30
1What is Playwright and how is it different from Selenium?Asked almost every time
Playwright is Microsoft's end-to-end testing framework for Chromium, Firefox and WebKit from one API, in TypeScript/JavaScript, Python, Java and .NET. It talks to browsers over their debugging protocols rather than WebDriver, auto-waits for elements to be actionable, isolates each test in a fresh browser context, and ships a test runner with parallelism, retries, tracing, screenshots and video.
2How does auto-waiting work?Asked almost every time
Before an action such as click, Playwright waits until the element is attached, visible, stable (not animating), enabled and able to receive events. Web-first assertions such as expect(locator).toBeVisible() retry until they pass or time out. This removes most manual sleeps and the flakiness they cause.
3Which locators should you prefer and why?Asked almost every time
User-facing ones: getByRole with an accessible name, getByLabel, getByPlaceholder, getByText, then getByTestId for elements with no good accessible handle. They match how users and assistive technology find elements, so tests survive markup and CSS changes. Avoid long CSS or XPath chains.
4How do you structure a large Playwright test suite?Asked almost every time
Group tests by feature, share setup through fixtures rather than helpers in beforeEach, keep test data independent per test, use projects for browsers and setup steps, store auth state once, tag tests (@smoke) for subsets, and run them in parallel with sharding in CI.
5What are web-first assertions?Asked almost every time
Assertions such as await expect(locator).toHaveText('Saved') that retry until the condition passes or times out. They wait for the app to reach the expected state, unlike checking a value once, which is the main way to avoid flaky timing problems.
6How do you test against a backend in different states?Asked almost every time
Seed data through API calls or database scripts in fixtures, mock specific responses with page.route, or use HAR recordings. Each test should create or isolate its own data so tests can run in parallel without interfering.
7What are fixtures?Very often asked
Fixtures provide each test with what it needs, such as page, context, browser or request, and you can define your own with test.extend (a logged-in page, a seeded database, a page object). They set up on demand, tear down automatically, and can be scoped per test or per worker.
8How do you handle authentication across tests?Very often asked
Log in once in a setup project, save the cookies and local storage with context.storageState({ path }), and configure other projects to load that state with use: { storageState }. Tests start already signed in. For several roles, save a state file per role.
9How do you debug a failing test, especially in CI?Very often asked
Turn on traces (trace: 'on-first-retry') and open them with npx playwright show-trace to see each action, DOM snapshots, console logs and network calls. Locally use --debug, --ui mode or page.pause(). Screenshots and videos on failure help too.
10How do you mock or intercept network requests?Very often asked
Use page.route(url, handler) to fulfil requests with fixture data, modify them, or abort them. page.waitForResponse waits for a specific call. HAR files can record and replay real traffic. The request fixture calls APIs directly for setup or API tests.
11How do you reduce flaky tests?Very often asked
Use role-based locators and web-first assertions, never fixed timeouts, keep tests independent with fresh contexts, control test data, mock unstable third parties, wait for the specific network response or UI state you depend on, and investigate retries with traces instead of hiding them.
12How do you handle iframes, new tabs and popups?Very often asked
Use page.frameLocator('iframe#pay') to work inside an iframe. For a new tab, wait for it with const popup = page.waitForEvent('popup') (or context.waitForEvent('page')) before the click, then work with the new page object.
13How do you test file uploads and downloads?Very often asked
Upload with locator.setInputFiles(path). For downloads, start waiting with page.waitForEvent('download') before clicking, then read download.path() or saveAs() and assert on the file.
14How do you run Playwright in CI?Very often asked
Install browsers with npx playwright install --with-deps, run tests headless with retries and trace on first retry, shard across machines, cache dependencies, and upload the HTML report and traces as artifacts. The official Docker image includes all browser dependencies.
15How do you test accessibility with Playwright?Very often asked
Use role-based locators (which already exercise the accessibility tree), add automated scans with @axe-core/playwright, and use toMatchAriaSnapshot to assert the accessible structure of a component.
16What is webServer in the config?Very often asked
It starts your app (for example npm run dev) before tests and waits for a URL to respond, then shuts it down afterwards. reuseExistingServer lets local runs use an already running server.
17How do you test responsive layouts and mobile?Very often asked
Define projects with device descriptors such as devices['iPhone 15'], which set viewport, user agent, touch and device scale factor, or set viewport per test. This emulates mobile browsers; real devices still need a device cloud.
18What is the Page Object Model and is it still useful?Often asked
A class per page or component that wraps locators and actions (loginPage.signIn(user)), so tests read like scenarios and a UI change is fixed in one place. With Playwright it is often combined with fixtures that create page objects for each test.
19How does Playwright run tests in parallel?Often asked
Test files run in parallel across worker processes; tests within a file run in order unless fullyParallel or test.describe.configure({ mode: 'parallel' }) is set. Each worker has its own browser, and each test its own context. In CI, --shard=1/4 splits the suite across machines.
20What are projects in the Playwright config?Often asked
Named configurations that run the same tests with different settings: browsers (chromium, firefox, webkit), devices such as devices['iPhone 15'], base URLs, or a setup project other projects depend on. One run can cover the whole matrix.
21How do you do visual regression testing?Often asked
await expect(page).toHaveScreenshot() compares with a stored baseline and fails on differences beyond a threshold. Keep fonts, viewport and data stable, mask dynamic areas, and update baselines deliberately with --update-snapshots, ideally generated on the same OS as CI.
22How do you handle time and dates in tests?Often asked
Use the Clock API (page.clock.install, setFixedTime, fastForward) to control Date and timers in the page, so tests for countdowns or expiring sessions are fast and deterministic.
23What are soft assertions?Often asked
expect.soft(...) records a failure but lets the test continue, so you see several problems in one run. The test still fails at the end.
24How do you test as different user roles?Often asked
Create one storage state per role in setup projects, then use test.use({ storageState: 'admin.json' }) per file or describe block, or create browser contexts with different states in one test.
25How do you do component testing?Often asked
Playwright's experimental component testing mounts React, Vue or Svelte components in a real browser with mount(). Many teams instead use Vitest browser mode or Storybook tests for components and Playwright for full flows.
26What does test.step do?Often asked
It groups actions under a named step in reports and traces, making long tests easier to read and failures easier to locate.
27What is codegen?Sometimes asked
npx playwright codegen <url> opens a browser and writes test code as you click and type, choosing resilient locators. It is a fast way to start a test or find a locator; the generated code still needs clean-up and assertions.
28Can Playwright test APIs?Sometimes asked
Yes: the request fixture (APIRequestContext) sends HTTP requests with the same assertions and reporting, useful for API tests or for setting up data before UI tests.
29How do you handle dialogs like alert and confirm?Sometimes asked
Register page.on('dialog', d => d.accept()) (or dismiss) before the action that opens it. By default Playwright dismisses dialogs automatically.
30How do you emulate geolocation, locale and permissions?Sometimes asked
Set geolocation, locale, timezoneId and permissions in the context options or with test.use, so you can test location-based or localised behaviour.
Objective questions 30
1Which locator is recommended first?Asked almost every time
Why: Role locators mirror how users find elements.
2Which assertion retries until the element is visible?Asked almost every time
Why: Web-first assertions auto-retry; checking a boolean once does not.
3Which browsers does Playwright support?Asked almost every time
Why: One API drives all three engines.
4Web-first assertions…Asked almost every time
Why: They remove timing flakiness.
5How do you work inside an iframe?Asked almost every time
Why: Locators then resolve inside it.
6What must you do before clicking a link that opens a new tab?Asked almost every time
Why: Otherwise you may miss the event.
7How do you reuse a signed-in session?Very often asked
Why: Saved storage state loads cookies and storage into new contexts.
8Which option records a trace only when a test is retried?Very often asked
Why: It keeps CI fast while capturing traces for failures.
9Which API intercepts network requests?Very often asked
Why: page.route lets you fulfil, modify or abort requests.
10What does each test get by default for isolation?Very often asked
Why: Contexts are like new incognito profiles.
11How do you upload a file?Very often asked
Why: Works on file inputs.
12Which config option starts your app before tests?Very often asked
Why: It waits for the URL.
13Which package adds axe accessibility scans?Very often asked
Why: Scans the rendered page.
14How do you emulate an iPhone?Very often asked
Why: Sets viewport, touch and UA.
15Which command installs browsers with system dependencies?Very often asked
Why: Needed on CI machines.
16How do you split a suite across 4 CI machines?Often asked
Why: Sharding divides tests between machines.
17Which command opens the interactive UI mode?Often asked
Why: UI mode lets you watch, filter and time-travel through tests.
18What is test.extend used for?Often asked
Why: Custom fixtures provide reusable setup and teardown.
19Why avoid page.waitForTimeout(3000)?Often asked
Why: Wait for a condition, not a duration.
20Which API controls Date and timers in the page?Often asked
Why: Makes time-based tests deterministic.
21What does expect.soft do?Often asked
Why: The test still fails at the end.
22How are dialogs handled by default?Often asked
Why: Add a handler to accept.
23Which assertion checks the accessible structure?Often asked
Why: Compares the ARIA tree.
24What does test.step add?Often asked
Why: Easier to read failures.
25How do you wait for a download?Often asked
Why: Start waiting before the click.
26Which command generates code by recording actions?Sometimes asked
Why: Codegen records interactions as test code.
27Which assertion compares a page with a baseline image?Sometimes asked
Why: toHaveScreenshot does visual comparison.
28Which fixture sends API requests in tests?Sometimes asked
Why: The request fixture is an APIRequestContext.
29Which options set a test's language and time zone?Sometimes asked
Why: Context options.
30How do you run only tests tagged @smoke?Sometimes asked
Why: Tags are matched in titles.
Track what you have practised and search every bank in your dashboard.
Start learning