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?"
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 ๐
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 [email protected].