Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Playwright browser contexts opened manually need to be closed?

Tags:

playwright

If I am not using the context test fixture, but instead browser and calling browser.newContext(), should I close these contexts at the end of the test? Or can they be garbage-collected and closed automatically?

If I need to close them, I'd expect the documentation or examples to say so somewhere but couldn't find it.

like image 336
Alexey Romanov Avatar asked Feb 11 '26 05:02

Alexey Romanov


1 Answers

Terminology:

  • Browser: a whole browser
  • Context: a window within the browser (has its own properties like viewport size, whether it's incognito, etc)
  • Page: a tab within the window (has properties like current URL, access to a particular DOM and network requests, etc)

If you close the browser, all pages and contexts within the browser are closed too.

Very likely, you're closing the browser after each test (this is the default behavior if you use preset fixtures, keeping tests isolated). In that case, there's no need to close contexts or pages.

But if you're keeping the browser open across tests, and you're creating contexts on a short-term basis, then, yes, close those contexts when they're no longer needed (likely afterEach).

like image 136
ggorlen Avatar answered Feb 16 '26 15:02

ggorlen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!