I use evaluate to inject a fetch request in my app that requests a non-existing path, which in normal scenarios will just throw a 404 error. When I intercept that request THE FIRST TIME, I can successfully mock the response data, but when doing it the second time, the browser catches it.
I'm not that good with networking in the browser but it seems that there might be some race conditions happening.
After the 1st and 2nd requests the browser catches all of them mostly, but sometimes playwright does catch some of them if I do it fast enough, sometimes if I send those fetch requests rapidly, playwright can catch twice in a row.
I might be missing something, so here is how I intercept the requests — this is the only interception I have in my test:
tab.route(/nonExistingRoute/g, async (route: Route) => {
console.log("intercepted."); // this doesn't get logged consistently
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({ foo: "bar" }),
});
})
This thread is trying to answer question "Is there an alternative solution to consistently intercept network requests in Playwright without setting up multiple instances of the same network event listener?"
Oh, now I'm 90% sure that it's a race condition: when I setup 3 instances of the same network event listener, I can intercept most of the requests.
I can catch 100% of the requests (~6 requests per second) with 4 listeners, I don't know much about how playwright handles all of this, but it feels like I'm going to face a lot of bugs with this approach, is there an alternative solution?.
I couldn't find an option to increase the frequency at which the listener checks for the network request in the browser.
(the number is an ID for the listener instance)
intercepted 1
intercepted 2
intercepted 1
intercepted 3
intercepted 1
intercepted 2
intercepted 1
intercepted 4
intercepted 1
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 [email protected].