MCPcopy
hub / github.com/codeceptjs/CodeceptJS / _after

Method _after

lib/helper/Playwright.js:691–755  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

689 }
690
691 async _after() {
692 if (!this.isRunning) return
693
694 // Clear popup state to prevent leakage between tests
695 popupStore.clear()
696
697 if (this.isElectron) {
698 try {
699 this.browser.close()
700 this.electronSessions.forEach(session => session.close())
701 } catch (e) {
702 console.warn('Warning during electron cleanup:', e.message)
703 }
704 return
705 }
706
707 if (restartsSession()) {
708 return refreshContextSession.bind(this)()
709 }
710
711 if (restartsBrowser()) {
712 // Close browser completely for restart strategy
713 if (this.isRunning) {
714 try {
715 // Close all pages first to release resources
716 if (this.browserContext) {
717 const pages = await this.browserContext.pages()
718 await Promise.allSettled(pages.map(p => p.close().catch(() => {})))
719 }
720 // Use timeout to prevent hanging (10s should be enough for browser cleanup)
721 await Promise.race([this._stopBrowser(), new Promise((_, reject) => setTimeout(() => reject(new Error('Browser stop timeout')), 10000))])
722 } catch (e) {
723 console.warn('Warning during browser restart in _after:', e.message)
724 // Force cleanup even on timeout
725 this.browser = null
726 this.browserContext = null
727 this.isRunning = false
728 }
729 }
730 return
731 }
732
733 // close other sessions with timeout protection, but only if restartsContext() is true
734 if (restartsContext()) {
735 try {
736 if ((await this.browser)?._type === 'Browser') {
737 const contexts = await Promise.race([this.browser.contexts(), new Promise((_, reject) => setTimeout(() => reject(new Error('Get contexts timeout')), 3000))])
738 const currentContext = contexts[0]
739 if (currentContext && (this.options.keepCookies || this.options.keepBrowserState)) {
740 try {
741 this.storageState = await currentContext.storageState()
742 } catch (e) {
743 console.warn('Warning during storage state save:', e.message)
744 }
745 }
746
747 await Promise.race([Promise.all(contexts.map(c => c.close())), new Promise((_, reject) => setTimeout(() => reject(new Error('Close contexts timeout')), 5000))])
748 }

Callers

nothing calls this directly

Calls 5

_stopBrowserMethod · 0.95
restartsSessionFunction · 0.90
restartsBrowserFunction · 0.90
restartsContextFunction · 0.90
clearMethod · 0.45

Tested by

no test coverage detected