()
| 4749 | } |
| 4750 | |
| 4751 | async function refreshContextSession() { |
| 4752 | // close other sessions with timeout protection, but preserve active session contexts |
| 4753 | try { |
| 4754 | const contexts = await Promise.race([this.browser.contexts(), new Promise((_, reject) => setTimeout(() => reject(new Error('Get contexts timeout')), 3000))]) |
| 4755 | |
| 4756 | // Keep the first context (default) and any contexts that belong to active sessions |
| 4757 | const defaultContext = contexts.shift() |
| 4758 | const activeSessionContexts = new Set() |
| 4759 | |
| 4760 | // Identify contexts that are still in use by active sessions |
| 4761 | if (this.sessionPages) { |
| 4762 | for (const sessionName in this.sessionPages) { |
| 4763 | const sessionPage = this.sessionPages[sessionName] |
| 4764 | if (sessionPage && sessionPage.context) { |
| 4765 | activeSessionContexts.add(sessionPage.context) |
| 4766 | } |
| 4767 | } |
| 4768 | } |
| 4769 | |
| 4770 | // Only close contexts that are not in use by active sessions |
| 4771 | const contextsToClose = contexts.filter(context => !activeSessionContexts.has(context)) |
| 4772 | |
| 4773 | if (contextsToClose.length > 0) { |
| 4774 | await Promise.race([Promise.all(contextsToClose.map(c => c.close())), new Promise((_, reject) => setTimeout(() => reject(new Error('Close contexts timeout')), 5000))]) |
| 4775 | } |
| 4776 | } catch (e) { |
| 4777 | console.warn('Warning during context cleanup:', e.message) |
| 4778 | } |
| 4779 | |
| 4780 | if (this.page) { |
| 4781 | try { |
| 4782 | const existingPages = await this.browserContext.pages() |
| 4783 | await this._setPage(existingPages[0]) |
| 4784 | } catch (e) { |
| 4785 | console.warn('Warning during page setup:', e.message) |
| 4786 | } |
| 4787 | } |
| 4788 | |
| 4789 | if (this.options.keepBrowserState) return |
| 4790 | |
| 4791 | if (!this.options.keepCookies) { |
| 4792 | this.debugSection('Session', 'cleaning cookies and localStorage') |
| 4793 | try { |
| 4794 | await this.clearCookie() |
| 4795 | } catch (e) { |
| 4796 | console.warn('Warning during cookie cleanup:', e.message) |
| 4797 | } |
| 4798 | } |
| 4799 | |
| 4800 | try { |
| 4801 | if (!this.page || !this.browserContext) { |
| 4802 | this.debugSection('Session', 'Skipping storage cleanup - no active page/context') |
| 4803 | return |
| 4804 | } |
| 4805 | |
| 4806 | const currentUrl = await this.grabCurrentUrl() |
| 4807 | |
| 4808 | if (currentUrl.startsWith('http')) { |
nothing calls this directly
no test coverage detected