-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Description
System info
- Playwright Version: v1.33
- Operating System: Windows, Ubuntu22
- Browser: All
- Other info:
Source code
- I provided exact source code that allows reproducing the issue locally.
Sorry, but I deviated a bit from the issue template.
So, I am interested in page.unroute method.
I had a look at the playwright tests
playwright/tests/page/page-route.spec.ts
Line 42 in 52feff3
it('should unroute', async ({ page, server }) => { |
what I see it is only tested with strings as url.
What I want to achieve is to unroute registered routes after each test.
We are using regex for that.
In our POM
we have this
registerRoutes(page: Page): Promise<void> {
return page.route(/(test\.local\/)|(domain2)/, this.testRouteHandler.bind(this));
}
async destroy(): Promise<void> {
await this.page.unroute(/(test\.local\/)|(domain2)/);
}
Looking at the implementation in pw
async unroute(url: URLMatch, handler?: RouteHandlerCallback): Promise<void> { |
it compares urls by strict equality, and then filters out page._routes.
So I guess for pure regexes it wont work.
What I did is that I saved the url regex into an instance field, so I can pass in a reference
Like this:
readonly routeUrl = /(test\.local\/)|(domain2)/;
registerRoutes(page: Page): Promise<void> {
return page.route(this.routeUrl, this.testRouteHandler.bind(this));
}
async destroy(): Promise<void> {
await this.page.unroute(this.routeUrl);
}
After awaiting the destroy method, page object still has a length of one in _routes, and points to the registered handlers.
Our goal is to get rid for all registered routes on the page in destroy method.
How to achieve it in regex?
can unroute also return a new length or something, or a boolean that confirms that routes are indeed unregistered?
Another thing we have is that sometimes there is a request in route handler that takes some time, and eventually throws
It happens that at that time another test is running, and route handler from previous test throws in another test, and then urelated test fails.
What I thought is to collect pending promises from route handles into a local array on POM instance, since we use POM as a fixture, and after use(POMfixture)
to await that all promises from that array are resolved before fixture can be teared down.
Not sure if you guys have a better advice on how to approach it?