Prev Next

Testing / Playwright Interview questions

How do you implement the Page Object Model in Playwright?

The Page Object Model (POM) wraps a page's locators and interactions inside a class, so tests read like a sequence of intentions rather than raw selector calls scattered throughout.

// login-page.ts
export class LoginPage {
  constructor(private page: Page) {}
  async goto() { await this.page.goto('/login'); }
  async login(email: string, password: string) {
    await this.page.getByLabel('Email').fill(email);
    await this.page.getByLabel('Password').fill(password);
    await this.page.getByRole('button', { name: 'Log in' }).click();
  }
}

// test file
test('logs in successfully', async ({ page }) => {
  const loginPage = new LoginPage(page);
  await loginPage.goto();
  await loginPage.login('a@b.com', 'secret');
  await expect(page.getByText('Welcome')).toBeVisible();
});

Locators are typically defined as class fields built from the page passed into the constructor, so if the login form's markup changes, only the LoginPage class needs updating rather than every test file that logs in. Playwright doesn't require any special support for this pattern - it's a plain class wrapping the existing API, which keeps it easy to combine with custom fixtures for auto-instantiating page objects per test.

A Page Object class primarily wraps:
The main maintenance benefit of POM is:

More Related questions...

What is Playwright? What are the supported browsers in Playwright? What are the supported programming languages in Playwright? How do you install Playwright? What is the purpose of the Playwright Test Runner? What are locators in Playwright? How do you use the page.goto() method? Define auto-waiting in Playwright? Describe the Playwright Inspector? List the different types of locators in Playwright? What is a fixture in Playwright? How do you take a screenshot in Playwright? What is the Trace Viewer in Playwright? How do you use codegen in Playwright? What are assertions in Playwright? What is the difference between Playwright and Selenium? How does Playwright handle multiple tabs and windows? What is the difference between page.click() and locator().click()? How do you handle iframes in Playwright? What is the difference between expect().toBeVisible() and isVisible()? How does Playwright differentiate retry behavior between actions and assertions? What is the purpose of browser contexts in Playwright? How do you mock API responses in Playwright? What is the difference between soft assertions and hard assertions in Playwright? How do you run tests in parallel in Playwright? What is the difference between test.beforeEach and test.beforeAll? How do you handle file uploads in Playwright? How do you handle authentication state across tests in Playwright? What is the difference between waitForSelector and waitForLoadState? How do you generate an HTML report in Playwright? What is the difference between headless and headed mode? How do you debug a failing Playwright test? What are Playwright's API testing capabilities? How do you handle elements that load dynamically or asynchronously? What is the difference between Playwright's expect and Jest's expect? Explain the internal working of Playwright's browser automation? Explain the execution flow of a Playwright test? Why is Playwright generally faster than Selenium? When should you use component testing in Playwright? How can you optimize Playwright test execution time? How do you troubleshoot flaky tests in Playwright? Explain the lifecycle of a browser context in Playwright? What happens when a Playwright locator doesn't match any element within the timeout? How does Playwright implement network interception and request mocking internally? Why doesn't Playwright rely on the WebDriver protocol? How do you implement the Page Object Model in Playwright? What is the difference between sharding and parallel workers in Playwright? How does Playwright handle elements inside Shadow DOM? Explain the internal working of Playwright's auto-waiting and actionability checks? How can you integrate Playwright tests into a CI/CD pipeline?
Show more question and Answers...

API

Comments & Discussions