playwright
playwright copied to clipboard
[Bug]: Error: Target page, context or browser has been closed
Version
1.43.1
Steps to reproduce
I am following the online course on Playwright and there is 3 tests in one spec file which when run ~ 5 times in a row sometimes fails with the error "Target page, context or browser has been closed". This makes the tests flaky. This is the repo https://github.com/bondar-artem/pw-apitest-app. The tests are in workingWithAPI.spec.ts (has title, delete article and create article) The repo has the Playwright version of 1.41.0, but I also tried with 1.43.1
Expected behavior
Tests should not be flaky, they should pass each time.
Actual behavior
Sometimes has title test fails with the error: Target page, context or browser has been closed
Additional context
I took a screenshot from my local test, but it is the same as the one in the repo
Environment
System:
OS: Windows 11 10.0.22631
CPU: (12) x64 13th Gen Intel(R) Core(TM) i7-1355U
Memory: 17.69 GB / 31.68 GB
Binaries:
Node: 21.1.0 - C:\Program Files\nodejs\node.EXE
npm: 10.2.0 - C:\Program Files\nodejs\npm.CMD
IDEs:
VSCode: 1.88.1 - C:\Users\MarijanaRukavina\AppData\Local\Programs\Microsoft VS Code\bin\code.CMD
Languages:
Bash: 5.2.15 - C:\cygwin64\bin\bash.EXE
npmPackages:
@playwright/test: ^1.43.1 => 1.43.1
I tried to downgrade Node to the LTS 20.12.2, but I still get an error just a different one while running the tests multiple times
I'm not able to run the reproduction, the auth.json
file is missing in the Git repository / it does not get created in the globalSetup:
Yes, sorry, I forgot to mention that. I think it is because of the "modifications" stages to the exercise. Before it was using the manual clicking in order to login and than it was switched to API. So the file was generated the first time.
For the sake of simplicity you can add this:
{"cookies":[],"origins":[{"origin":"https://angular.realworld.how","localStorage":[{"name":"jwtToken","value":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjozNjcxfSwiaWF0IjoxNzE0OTMwMjYyLCJleHAiOjE3MjAxMTQyNjJ9.ttV9F_aw6kZuvPmMc259td-U3_Tv3r9sn_a7JIJLf2Y"}]}]}
In the following path:
And try it again. Thanks :)
I see, you need to add
await page.unrouteAll({ behavior: 'ignoreErrors' })
at the end of your tests, when you are using page/context.route(), otherwise if there is a request incoming, page.route gets called, the page closes while the route.fetch() request is in progress, route.fetch() will throw.
Thanks! This worked 🌮
It is odd that the documentation doesn't mention this and it seems like an important note to this, right? @mxschmitt
Good idea, let me add it!
Added this hint in the exception in https://github.com/microsoft/playwright/commit/964fe66ccc4e705c519d0a58578be0085d1af6fc
@pavelfeldman should this work in an afterEach
block? We're hitting it intermittently, but only for Webkit with a mobile profile - and adding this in afterEach
doesn't seem to be helping.
@mrmckeb do you mind filing a new issue with a reduced test-case? Happy to look into it! In theory it should also work with afterEach, if you do unrouteAll at the end.