Interview Questions

Playwright Interview

1. What is Playwright?

Playwright is an open-source automation testing framework developed by Microsoft for end-to-end web application testing.

It supports:

  • Chromium
  • Firefox
  • WebKit

Key Features

  • Cross-browser testing
  • Auto-wait mechanism
  • Parallel execution
  • Multi-tab and multi-window support
  • Network interception
  • API testing Mobile emulation
  • Headless and headed execution

2. Why is Playwright preferred over Selenium?

Playwright is preferred because it provides faster execution, built-in autowaiting, better handling of modern web applications, easy parallelexecution, and supports multiple browsers with a single API.

3. What is auto-wait in Playwright?

Playwright automatically waits for elements before performing actions.

It waits for:

  • Element to be visible
  • Element to be attached
  • Element to be stable
  • Element to be enabled

4. Difference between Locator and Selector

Locator is preferred because it supports auto waiting and re-querying the DOM whenever action is performed.

5. What are the different locator strategies in Playwright?

By ID

page.locator('#username')

By Text

page.getByText('Login')

By Role

page.getByRole('button', { name: 'Login' })

By Label

page.getByLabel('Email')

By Placeholder

page.getByPlaceholder('Enter username')

By Test ID

page.getByTestId('submit-btn')

Best Practice

Prefer:

  1. Role
  2. Label
  3. TestId

Avoid complex XPath.

OM whenever action is performed.

6. What is the difference between fill() and type()?

fill()                                            type()

Clears existing text                 Does not clear

Faster                                        Slower

Sets value directly                   Types character by character

7. Explain Playwright Test Runner

Playwright Test is the built-in framework for executing tests.

Features

  • Parallel execution
  • Retry mechanism
  • Fixtures
  • HTML reports
  • Screenshots/videos
  • Hooks
  • Tagging

8. What are Fixtures in Playwright?

Fixtures are reusable setup objects provided to tests.

Built-in Fixtures

  • page
  • browser
  • context

9. What is Browser Context?

Browser Context is an isolated browser session.

Each context has:

  • Separate cookies
  • Separate local storage
  • Separate session

10. Difference between Browser and Context

Browser                                          Context

Actual browser instance              Separate session inside browser

Heavyweight Lightweight            Launch once Create multiple

11. What are Hooks in Playwright?

Hooks are used for setup and teardown.

Types

  • beforeAll
  • beforeEach
  • afterEach
  • afterAll

12. Difference between beforeAll and beforeEach

beforeAll                                       beforeEach

Runs once                                    Runs before every test

Faster                                           Repeated execution

Use Cases

beforeAll:

  • Login once
  • DB setup

beforeEach:

  • Fresh setup for every test

13. How to handle dropdown in Playwright?

  • Static Dropdown
  • await page.selectOption('#country', 'India');
  • Multiple Selection
  • await page.selectOption('#country', ['India', 'USA']);

14. How to handle alerts?

  • Simple Alert
  • page.on('dialog', async dialog => {
  • console.log(dialog.message());
  • await dialog.accept();
  • });
  • Dismiss Alert
  • await dialog.dismiss();

15. How to upload files in Playwright?

  • await page.setInputFiles('#upload', 'test.pdf');
  • Multiple Files
  • await page.setInputFiles('#upload', [
  • 'a.pdf',
  • 'b.pdf'
  • ]);

16. How to handle multiple tabs/windows?

  • onst [newPage] = await Promise.all([
  • context.waitForEvent('page'),
  • page.click('#open')
  • ]);
  • await newPage.waitForLoadState();

17. What is Page Object Model (POM)?

  • POM is a design pattern where:
  1. Locators
  2. Methods
  3. Actions
  • are stored in separate classes.
  • Advantages
  1. Reusability
  2. Maintainability
  3. Reduced duplication

18. What is await in Playwright?

Playwright operations are asynchronous.

await waits until action completes.

19. Difference between synchronous and asynchronous programming

Synchronous                                          Asynchronous

Executes one by one                            Non-blocking

Waits for completion                           Continues execution

JavaScript and Playwright mainly use asynchronous operations.

20. What is Promise in JavaScript?

Promise represents future completion of an
operation.

States

  • Pending
  • Resolved
  • Rejected

21. How to take screenshot in Playwright?

Full Page

await page.screenshot({

path: 'page.png',

fullPage: true

});

Element Screenshot

await
page.locator('#logo').screenshot({

path: 'logo.png'

});

22. How to capture video?

In config:

use: {

video: 'on'

}

23. How to generate reports?

npx playwright show-report

HTML Report

Generated automatically after execution.

24. How to run tests in headed mode?

npx playwright test --headed

25. How to run a single test?

By file

npx playwright test login.spec.ts

By title

npx playwright test -g "login test"

26. How to run tests in parallel?

In config:

workers: 4

Or:

npx playwright test --workers=4

27. What is flaky test?

A flaky test:

  • Sometimes passes
  • Sometimes fails

without code changes.

Reasons

  • Hard waits
  • Timing issues
  • Dynamic elements
  • Network delays

Prevention

  • Use locators
  • Avoid waitForTimeout()
  • Use assertions properly

28. Why should we avoid waitForTimeout()?

await page.waitForTimeout(5000);

Problems

  • Slow execution
  • Flaky tests
  • Unnecessary waiting

Better Alternative

await expect(locator).toBeVisible();

20. What is Promise in JavaScript?

Promise represents future completion of an
operation.

States

  • Pending
  • Resolved
  • Rejected

29. Difference between locator() and $$

locator()                        $$

Auto waits                    No auto wait

Recommended           Old approach

Dynamic                       Static array

30. Explain assertions in Playwright

Assertions validate expected results.

31. How to handle authentication in Playwright?

Storage State Method

Save Login

await context.storageState({

path: 'auth.json'

});

Reuse Login

use: {

storageState: 'auth.json'

}

Advantage

Avoids repeated login.

32. What is network interception?

Intercepting API/network requests

33. What is API Testing in Playwright?

Playwright supports API testing using request fixture.

34. Explain retries in Playwright

Retries rerun failed tests automatically.

Config

retries: 2

Useful for unstable environments.

35. What are annotations/tags?

Used to categorize tests.

36. Explain Playwright framework structure

Typical structure:

project

├── tests

├── pages

├── utils

├── fixtures

├── playwright.config.ts

└── package.json

20. What is Promise in JavaScript?

Promise represents future completion of an
operation.

States

  • Pending
  • Resolved
  • Rejected

37. How do you avoid flaky tests?

To avoid flaky tests, I use stable locators, explicit assertions, avoid hard waits, ensure test independence, and leverage Playwright s auto-waiting capability.

38. What is trace viewer?

Trace Viewer helps debug failed tests.

Enable

use: {

trace: 'on'

}

Open

npx playwright show-trace trace.zip

It shows:

  • Actions
  • Network
  • Console
  • Screenshots

39. What is the difference between nth(), first(), and last()?

nth()

locator.nth(2)

first()

locator.first()

last()

locator.last()

Used when multiple elements match.

40. Explain soft assertion

Soft assertion continues execution even if assertion fails

41. How to handle frames in Playwright?

const frame = page.frameLocator('#frame');

await frame.locator('#email').fill('test');

42. Difference between page and locator

page                                                   locator

Represents webpage                      Represents element

Used for navigation                         Used for element actions

43. What is headless mode?

Browser runs without UI.

44. Explain CI/CD integration

GitHub Actions, Jenkins, and Azure DevOps can execute Playwright
tests automatically during deployment pipelines.

45. Explain custom fixtures

Custom fixtures are reusable setup logic

46. What is the difference between toHaveText() and toContainText()?

toHaveText()     toContainText()
Exact text           Partial text

47. Explain Promise.all() in Playwright

Runs multiple async operations together.

48. How do you debug Playwright tests?

Methods

  • page.pause()
  • Trace Viewer
  • UI Mode
  • Console logs
  • Screenshots
  • Videos

49. What is UI Mode?

Interactive mode for debugging tests.

npx playwright test --ui

50. Explain real-time challenges faced in Playwright

Common Challenges

  1. Dynamic elements
  2. iFrames
  3. Shadow DOM
  4. File uploads
  5. OTP login
  6. Captcha
  7. Flaky APIs

Solutions

  1. Stable locators
  2. Frame locators
  3. API mocking
  4. Storage state reuse
  5. Retry logic

Register Your Demo Slot

    Quick Enquiry




      Register to Achieve Your Dream Career


        Wait!! Don't skip your Dream Career

        Enroll Today & Start Your Learning Journey

          Get in Touch with us


            5 + 6 =