What are the different ways to configure expect in Playwright?


To configure expect in Playwright, you can use the expect.configure() method to set a timeout for your assertions. Create a slowExpect object with a 10-second timeout and use it for soft assertions:

const slowExpect = expect.configure({ timeout: 10000 });
await slowExpect(locator).toHaveText('Submit');

For asynchronous polling, use the expect.poll() method to convert synchronous expect into asynchronous polling. The following code polls a function until it returns HTTP status 200:

await expect.poll(async () => {
  const response = await page.request.get('https://ray.run');
  return response.status();
}, {
  message: 'make sure API eventually succeeds',
  timeout: 10000,

You can also set test configuration options in your Playwright config file. Set the default timeout for web first assertions like expect(locator).toHaveText() by setting the testConfig.expect option:

testConfig.expect = {timeout: 5000};

To add custom matchers, use the expect.extend() method. Define a custom matcher called "toBeWithinRange" in your configuration file:

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

  toBeWithinRange(received: number, floor: number, ceiling: number) {
    const pass = received >= floor && received <= ceiling;
    if (pass) {
      return { message: () => 'passed', pass: true };
    } else {
      return { message: () => 'failed', pass:false };

Now, use the new matcher in your tests:

test('numeric ranges', () => {
Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.