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

Method _session

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

Source from the content-addressed store, hash-verified

902 }
903
904 _session() {
905 const defaultContext = this.browserContext
906 return {
907 start: async (sessionName = '', config) => {
908 this.debugSection('New Context', config ? JSON.stringify(config) : 'opened')
909 this.activeSessionName = sessionName
910
911 let browserContext
912 let page
913 if (this.isElectron) {
914 const browser = await playwright._electron.launch(this.playwrightOptions)
915 this.electronSessions.push(browser)
916 browserContext = browser.context()
917 page = await browser.firstWindow()
918 } else {
919 try {
920 // Check if browser is still available before creating context
921 if (!this.browser) {
922 throw new Error('Browser is not available for session context creation')
923 }
924 browserContext = await Promise.race([this.browser.newContext(Object.assign(this.contextOptions, config)), new Promise((_, reject) => setTimeout(() => reject(new Error('New context timeout')), 10000))])
925 page = await Promise.race([browserContext.newPage(), new Promise((_, reject) => setTimeout(() => reject(new Error('New page timeout')), 5000))])
926 } catch (e) {
927 console.warn('Warning during context creation:', e.message)
928 if (this.playwrightOptions.userDataDir) {
929 browserContext = await playwright[this.options.browser].launchPersistentContext(`${this.userDataDir}_${this.activeSessionName}`, this.playwrightOptions)
930 this.browser = browserContext
931 page = await browserContext.pages()[0]
932 } else {
933 throw e
934 }
935 }
936 }
937
938 if (this.options.trace) await browserContext.tracing.start({ screenshots: true, snapshots: true })
939 await targetCreatedHandler.call(this, page)
940 await this._setPage(page)
941 // Create a new page inside context.
942 return browserContext
943 },
944 stop: async () => {
945 // is closed by _after
946 },
947 loadVars: async context => {
948 if (context) {
949 this.browserContext = context
950 const existingPages = await context.pages()
951 this.sessionPages[this.activeSessionName] = existingPages[0]
952 return this._setPage(this.sessionPages[this.activeSessionName])
953 }
954 },
955 restoreVars: async session => {
956 this.withinLocator = null
957 this.browserContext = defaultContext
958
959 if (!session) {
960 this.activeSessionName = ''
961 } else {

Callers

nothing calls this directly

Calls 5

_setPageMethod · 0.95
_waitForActionMethod · 0.95
pushMethod · 0.80
debugMethod · 0.80
startMethod · 0.45

Tested by

no test coverage detected