Rayrun
โ† Back to Discord Forum

Close all available {browser} contexts

alexniculaeposted in #help-playwright
Open in Discord

Hi, Does anybody have a solution to close all browser contexts in one shot? (multiple contexts created for a test with multiple logins)

I tried looking into browser.contexts(), but this cannot be used as it's not a promise.

Using playwright test.

Thank you!

This thread is trying to answer question "How can all browser contexts be closed in one shot when multiple contexts are created for a test with multiple logins?"

4 replies

Hi, try this await browser.close() or await browser.contexts().map(c => c.close());

Hi Marble, thanks for that ๐Ÿ™‚ tried it already, but the await has no effect on the browser object (i think it doen't return promises)

given that, it will try to close the contexts before the test finished running.

also, browser.close() does not handle closing the contexts, so it's not a solution in this case ๐Ÿ™‚

Yeah, I have multiple companies with multiple users logging in on certain test scenarios. I also found during the test I needed to kill user context to free up resources when no longer used. I don't have a true solution, but I ended up writing some context management in my framework.

First I track the active sessions that I create per user per company. This allows me to close all session at the company or user level. This company level close all makes my afterEach() tear down code tidier.

Second I have an issue that in some cases the user context will also have a request context, but this seems to work at the higher context close().

Third I have had an issue were the timeout will kill the session prior to me, and this results in an exception that the context was previously closed. I think there is a ticket raised to verify the close or add a isActive property. Currently, I use a try catch so I don't mask the timeout which was the root cause.

I could add some code, but the solution probably would not fit your implementation, but happy to share. My solution is managing 1-5 companies interacting on our website to test integration and event scenarios.

async closeSession(): Promise<void> { if (this.activeSession) { if (this.page) { await this.page.close({ runBeforeUnload: false }); } if (this.context) { try { await this.context.close(); this.context = undefined; } catch (e) { // eslint-disable-next-line no-console console.log(e); } } } this.activeSession = false; }

Again, this probably is not what you are looking for.

NZCarnage, thank you for your answer and the details ๐Ÿ™

our use cases are quite similar, although youโ€™re right and mine seem a bit simpler; nice solution and good idea.

what i did in the end today after looking into replicating a context management solution, was to finally use fixtures. iโ€™m coming from the world of Cypress, and fixtures seemed too much of an abstraction, but i ended up loving it ๐Ÿ˜

iโ€™ll mark this as done and thank you again for the idea ๐Ÿ™

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.