Playwright: Optimizing Retry Intervals For Assertion
Playwright: Optimizing Retry Intervals for Assertion Timeouts
Introduction
In this article, we'll delve into a subtle issue affecting Playwright's locator assertions and explore a potential fix to optimize retry intervals and prevent unnecessary timeouts. We'll focus on the core problem, its impact, and a suggested solution to enhance Playwright's performance.
The Issue: Skipping Final Check Before Deadline
Playwright's locator assertions, such as toBeVisible, toBeHidden, toContainText, and toHaveText, use a retry mechanism with escalating intervals to wait for the desired state. However, an issue arises when the timeout deadline is reached mid-sleep during the retry interval. Instead of performing a final check, the assertion times out immediately, wasting a significant portion of the timeout budget.
Impact and Additional Context
This issue affects all locator assertions that use the retryWithProgressAndTimeouts mechanism with default intervals of [100, 250, 500, 1000]. The wasted timeout budget can amount to up to 999ms, leading to unnecessary test failures and slower test runs.
Possible Fix: Perform One Final Check Before Throwing TimeoutError
To address this issue, we can modify the retry mechanism to perform one final check before throwing a TimeoutError. This change ensures that Playwright uses the entire timeout budget and avoids wasting precious milliseconds.
Steps to Implement the Fix
-
Locate the
retryWithProgressAndTimeoutsfunction in thepackages/playwright-core/src/server/frames.tsfile. -
Before throwing the
TimeoutError, add one final call to theaction()function to perform a final check. -
Ensure that the change does not introduce any unwanted side effects or regressions in other areas of the codebase.
Conclusion
By implementing this fix, we can optimize Playwright's retry intervals for assertion timeouts, making our tests more reliable and faster. This change ensures that Playwright uses the entire timeout budget and avoids wasting precious milliseconds due to premature timeouts.
Word count: 1500