Top 50 Playwright Assertions Interview Questions and Answers

Assertions are one of the most frequently asked topics in Playwright interviews. They help verify whether the application behaves as expected after performing user actions. Playwright provides a powerful assertion library with auto-waiting, retry mechanisms, and rich error reporting, making tests more reliable and less flaky.

This guide covers beginner, intermediate, and advanced Playwright assertion interview questions with detailed answers and TypeScript examples.


1. What are Assertions in Playwright?

Answer

Assertions are used to verify that the actual result matches the expected result. If the expected condition is not met, the test fails.

Example

import { test, expect } from '@playwright/test';

test('Verify Page Title', async ({ page }) => {
    await page.goto('https://example.com');

    await expect(page).toHaveTitle('Example Domain');
});

2. Why are Assertions important in Automation Testing?

Answer

Assertions ensure that:

  • The application behaves correctly.
  • Test results are validated.
  • Bugs are detected automatically.
  • Expected UI and business logic are verified.

Without assertions, an automation script only performs actions without validating outcomes.


3. What assertion library does Playwright use?

Answer

Playwright Test provides a built-in expect() assertion library.

expect(value).toBe(expected);

4. What makes Playwright assertions better than traditional assertions?

Answer

Playwright assertions provide:

  • Auto-waiting
  • Automatic retries
  • Better error messages
  • Screenshot capture on failures
  • Trace support
  • Reduced flaky tests

5. What is expect() in Playwright?

Answer

expect() is used to validate expected results.

expect(10).toBe(10);

6. What is auto-waiting in Playwright assertions?

Answer

Playwright automatically waits until the expected condition becomes true or the timeout is reached.

Example:

await expect(page.locator('#login')).toBeVisible();

No explicit wait is required.


7. What happens if an assertion fails?

Answer

  • The current test fails.
  • Playwright captures useful diagnostics (such as screenshots and traces, if configured).
  • Remaining steps in the test are skipped unless soft assertions are used.

8. What is the default timeout for assertions?

Answer

By default, Playwright assertions use the configured expect timeout, which is 5 seconds unless changed in the Playwright configuration or overridden for a specific assertion.

Example:

await expect(locator).toBeVisible({
    timeout: 10000
});

9. Difference between toBe() and toEqual()?

Answer

toBe()toEqual()
Checks primitive values using strict equalityDeep comparison for objects and arrays
Best for numbers, strings, booleansBest for objects and arrays

Example:

expect(5).toBe(5);

expect({
    name: 'John'
}).toEqual({
    name: 'John'
});

10. What is not in Playwright assertions?

Answer

Used to verify negative conditions.

expect(5).not.toBe(10);

11. What is toBeTruthy()?

Answer

Checks whether a value is truthy.

expect(true).toBeTruthy();

12. What is toBeFalsy()?

Answer

Checks whether a value is falsy.

expect(false).toBeFalsy();

13. What is toBeNull()?

Answer

Checks if the value is null.

expect(null).toBeNull();

14. What is toBeDefined()?

Answer

Checks whether a variable is defined.

const username = 'Admin';

expect(username).toBeDefined();

15. What is toBeUndefined()?

Answer

Checks if a variable is undefined.

let city;

expect(city).toBeUndefined();

16. What is toContain()?

Answer

Checks whether an array or string contains a value.

expect('Playwright').toContain('wright');

17. What is toHaveLength()?

Answer

Verifies array or string length.

expect([1,2,3]).toHaveLength(3);

18. What is toBeGreaterThan()?

Answer

Checks if a value is greater than another value.

expect(100).toBeGreaterThan(50);

19. What is toBeLessThan()?

Answer

expect(10).toBeLessThan(20);

20. What is toBeCloseTo()?

Answer

Useful for floating-point numbers.

expect(0.1 + 0.2).toBeCloseTo(0.3);

21. What are Locator Assertions?

Answer

Locator assertions validate the state of UI elements.

Examples:

  • toBeVisible()
  • toBeHidden()
  • toBeEnabled()
  • toHaveText()
  • toHaveValue()

22. What is toBeVisible()?

Answer

Checks whether an element is visible.

await expect(
    page.locator('#login')
).toBeVisible();

23. What is toBeHidden()?

Answer

Checks if an element is hidden.

await expect(
    page.locator('.loader')
).toBeHidden();

24. What is toBeEnabled()?

Answer

Verifies that an element is enabled.

await expect(
    page.locator('#submit')
).toBeEnabled();

25. What is toBeDisabled()?

Answer

Checks if an element is disabled.

await expect(
    page.locator('#submit')
).toBeDisabled();

26. What is toBeChecked()?

Answer

Used for checkboxes and radio buttons.

await expect(
page.locator('#remember')
).toBeChecked();

27. What is toHaveText()?

Answer

Verifies exact text.

await expect(
page.locator('h1')
).toHaveText('Dashboard');

28. What is toContainText()?

Answer

Checks partial text.

await expect(
page.locator('.message')
).toContainText('Success');

29. Difference between toHaveText() and toContainText()?

Answer

toHaveText()toContainText()
Exact matchPartial match
Entire text must matchOnly a portion needs to match

30. What is toHaveValue()?

Answer

Checks the value of input fields.

await expect(
page.locator('#username')
).toHaveValue('Admin');

31. What is toHaveAttribute()?

Answer

Checks an HTML attribute.

await expect(
page.locator('#email')
).toHaveAttribute('type','email');

32. What is toHaveClass()?

Answer

Verifies CSS classes.

await expect(
page.locator('.active')
).toHaveClass('active');

33. What is toHaveCount()?

Answer

Checks the number of matching elements.

await expect(
page.locator('.product')
).toHaveCount(5);

34. What is toBeEditable()?

Answer

Checks if an input can be edited.

await expect(
page.locator('#username')
).toBeEditable();

35. What is toHaveCSS()?

Answer

Verifies CSS property values.

await expect(
page.locator('#title')
).toHaveCSS('color','rgb(255, 0, 0)');

36. What are Page Assertions?

Answer

Assertions used directly on the page.

Examples:

  • toHaveTitle()
  • toHaveURL()

37. What is toHaveTitle()?

Answer

Checks the page title.

await expect(page)
.toHaveTitle('Dashboard');

38. What is toHaveURL()?

Answer

Checks the current URL.

await expect(page)
.toHaveURL(/dashboard/);

39. What are Soft Assertions?

Answer

Soft assertions allow the test to continue even if an assertion fails.

expect.soft(title).toBe('Dashboard');

expect.soft(username).toBe('Admin');

40. When should you use Soft Assertions?

Answer

Use soft assertions when:

  • Validating multiple UI elements.
  • Collecting all failures in a single execution.
  • Creating comprehensive UI verification tests.

41. What is expect.poll()?

Answer

expect.poll() repeatedly executes a function until the expected result is achieved or the timeout expires.

await expect.poll(async () => {
    return await page.locator('.counter').textContent();
}).toBe('10');

42. What is the difference between expect() and expect.poll()?

Answer

expect()expect.poll()
Checks an immediate value or locatorRepeatedly evaluates a callback until the condition is met
Best for UI elements and direct valuesBest for values that change over time

43. What are Screenshot Assertions?

Answer

Used for visual regression testing.

await expect(page)
.toHaveScreenshot();

44. Can assertions be customized with messages?

Answer

Yes.

expect(
    total,
    'Total price should be greater than zero'
).toBeGreaterThan(0);

45. How do you verify API responses in Playwright?

Answer

const response = await page.request.get(
'https://reqres.in/api/users/2'
);

expect(response.status()).toBe(200);

expect(response.ok()).toBeTruthy();

46. What are common mistakes when writing assertions?

Answer

  • Using waitForTimeout() before assertions.
  • Verifying text with textContent() instead of toHaveText().
  • Writing unnecessary manual waits.
  • Using exact matches for dynamic values.
  • Overusing hard-coded timeouts.

47. Why are locator assertions preferred over manual value checks?

Answer

Locator assertions automatically wait for the expected condition and retry until the timeout expires, making tests more stable and less flaky.

Instead of:

const text = await page.locator('h1').textContent();
expect(text).toBe('Dashboard');

Prefer:

await expect(page.locator('h1')).toHaveText('Dashboard');

48. Can Playwright assertions be used with regular JavaScript variables?

Answer

Yes.

const total = 100;

expect(total).toBe(100);

49. How do you verify multiple conditions in one test?

Answer

await expect(page).toHaveTitle('Dashboard');

await expect(page).toHaveURL(/dashboard/);

await expect(page.locator('#logout'))
    .toBeVisible();

await expect(page.locator('.product'))
    .toHaveCount(5);

50. What are the best practices for Playwright Assertions?

Answer

  • Use locator assertions (toBeVisible(), toHaveText(), toHaveValue()) instead of manually reading values.
  • Avoid waitForTimeout(); rely on Playwright’s auto-waiting.
  • Use expect.soft() when multiple independent validations should run in the same test.
  • Use expect.poll() for asynchronous values that change over time.
  • Keep assertions specific and focused on a single expected outcome.
  • Use regular expressions for dynamic URLs and text where appropriate.
  • Write descriptive custom messages for critical business validations.
  • Prefer built-in Playwright assertions over custom validation logic whenever possible.

Bonus Interview Questions

1. What is the difference between expect(locator).toHaveText() and expect(await locator.textContent()).toBe()?

Answer:

toHaveText() automatically waits and retries until the expected text appears, making it more reliable for dynamic web pages. textContent() retrieves the current text immediately and does not retry.


2. Why should waitForTimeout() not be used before assertions?

Answer:

waitForTimeout() introduces unnecessary delays and can make tests flaky. Playwright assertions already include built-in waiting and retry mechanisms, so explicit sleep statements are rarely needed.


3. Which Playwright assertions are most commonly asked in interviews?

Answer:

The most frequently discussed assertions are:

  • toBe()
  • toEqual()
  • toContain()
  • toHaveText()
  • toContainText()
  • toBeVisible()
  • toBeHidden()
  • toBeEnabled()
  • toBeDisabled()
  • toHaveValue()
  • toHaveAttribute()
  • toHaveCount()
  • toHaveURL()
  • toHaveTitle()
  • expect.soft()
  • expect.poll()
  • toHaveScreenshot()

Mastering these assertions and understanding when to use each one will prepare you for most Playwright automation interviews, from beginner to advanced levels.

Leave a Comment