playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Bug]: Error: Target page, context or browser has been closed

Open mareru opened this issue 9 months ago • 7 comments

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

image

image

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

mareru avatar May 05 '24 14:05 mareru

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

image

mareru avatar May 05 '24 17:05 mareru

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:

image

mxschmitt avatar May 06 '24 09:05 mxschmitt

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:

image

And try it again. Thanks :)

mareru avatar May 06 '24 10:05 mareru

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.

mxschmitt avatar May 06 '24 11:05 mxschmitt

Thanks! This worked 🌮

mareru avatar May 07 '24 21:05 mareru

It is odd that the documentation doesn't mention this and it seems like an important note to this, right? @mxschmitt

mareru avatar May 07 '24 21:05 mareru

Good idea, let me add it!

mxschmitt avatar May 08 '24 17:05 mxschmitt

Added this hint in the exception in https://github.com/microsoft/playwright/commit/964fe66ccc4e705c519d0a58578be0085d1af6fc

pavelfeldman avatar May 28 '24 19:05 pavelfeldman

@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 avatar Sep 24 '24 04:09 mrmckeb

@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.

mxschmitt avatar Sep 24 '24 09:09 mxschmitt