Home / Companies / Mergify / Blog / Post Details
Content Deep Dive

Playwright route handlers fire only on requests they were registered before

Blog post from Mergify

Post Details
Company
Date Published
Author
-
Word Count
1,165
Language
English
Hacker News Points
-
Summary

In Playwright testing, the timing of route handler registration is crucial for deterministically intercepting network requests, as handlers can only intercept requests made after they are registered. A common issue arises when a handler is registered after a `page.goto()` call, causing it to miss the initial request and fail to intercept the desired API response, often leading to flaky tests. To ensure consistent results, the route handler should be registered before navigation or any action that triggers the request. This principle is critical when mocking requests and can be managed using specific strategies like registering handlers per page or using a counter in the handler for varying responses. Mergify Test Insights identifies these "route-handler-too-late" failures, categorizing them to highlight the order issue without rerunning tests, thus maintaining the test suite's efficiency and reliability.