Search code examples
playwrightdebouncing

How to test expected delay time on page element (fixed wait, debounce) with Playwright


How to test implemented fixed delay on webpage with Playwright (exactly: debounce)?

I have simple scenario. User, after entering input need to wait fixed time for response i.e. 1000ms. How to test that exact wait with Plawright?

Looked at https://github.com/microsoft/playwright/issues/4405 but I wonder if there is more elegant way to do this?


Solution

  • Well usually there is a better way than to hardcode a waiting time.

    • Wait for certain API calls
    • Wait for network idle (= wait for started calls to finish)
    • Wait for an event

    However there are times when hardcoding is inevitable. What I've used is page.waitForTimeout()

    Wait for 2 seconds:

    await page.waitForTimeout(2000);
    

    Official docs: https://playwright.dev/docs/api/class-page#page-wait-for-timeout

    "Waits for the given timeout in milliseconds.

    Note that page.waitForTimeout() should only be used for debugging. Tests using the timer in production are going to be flaky. Use signals such as network events, selectors becoming visible and others instead."