playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Docs]: Is there a way to cancel response.finished() promise?

Open himat opened this issue 1 year ago • 2 comments
trafficstars

Page(s)

https://playwright.dev/docs/api/class-response#response-finished

Description

I noticed that when I do await response.finished() // wait for response to finish (inside the callback for page.on('response", onResponse)) that if the browser is closed, then this line throws an error "Target page, context or browser has been closed". It throws even after the callback is unregistered with page.off("response", onResponse), but that makes sense since any prior response is still in flight.

So is there a way though to cancel any inflight pending response.finished() promises so that they don't throw when the page is closed?

  page.on("response", onResponse)

  disposableStack.defer(() => {
    page.off("response", onResponse)
    page.off("close", onBrowserViewDestroyed)
  })

himat avatar Jun 28 '24 06:06 himat

I actually mainly want to cancel them because I may open multiple pages over time, and I don't want a request from an old page to be waiting forever if that response never finished. So if I know that I am "done" with a page even though I am keeping the page tab open, it would be nice if I can manually cancel any still in flight pending response.finished() responses.

himat avatar Jun 28 '24 06:06 himat

There isn't much we can offer at this point. You can catch that finished() call or you can add it to a list of promises that you await / mute in the end of your test manually. We are thinking about joining the event handlers in the end of the test though, so I'll leave this open.

pavelfeldman avatar Jun 28 '24 17:06 pavelfeldman