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

Method _session

lib/helper/Puppeteer.js:421–474  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

419 }
420
421 _session() {
422 return {
423 start: async (name = '') => {
424 this.debugSection('Incognito Tab', 'opened')
425 this.activeSessionName = name
426
427 const bc = await this.browser.createBrowserContext()
428 await bc.newPage()
429
430 // Create a new page inside context.
431 return bc
432 },
433 stop: async () => {
434 // is closed by _after
435 },
436 loadVars: async context => {
437 const existingPages = context.targets().filter(t => t.type() === 'page')
438 this.sessionPages[this.activeSessionName] = await existingPages[0].page()
439 return this._setPage(this.sessionPages[this.activeSessionName])
440 },
441 restoreVars: async session => {
442 this.withinLocator = null
443
444 if (!session) {
445 this.activeSessionName = ''
446 } else {
447 this.activeSessionName = session
448 }
449
450 const defaultCtx = this.browser.defaultBrowserContext()
451 if (!defaultCtx) {
452 this.debug('Cannot restore session vars: default browser context is undefined')
453 return
454 }
455
456 try {
457 const existingPages = defaultCtx.targets().filter(t => t.type() === 'page')
458 if (existingPages && existingPages.length > 0) {
459 await this._setPage(await existingPages[0].page())
460 // Reset context-related variables to ensure clean state after session
461 this.context = await this.page
462 this.contextLocator = null
463 } else {
464 this.debug('Cannot restore session vars: no pages available')
465 }
466 } catch (err) {
467 this.debug(`Failed to restore session vars: ${err.message}`)
468 return
469 }
470
471 return this._waitForAction()
472 },
473 }
474 }
475
476 /**
477 * Use Puppeteer API inside a test.

Callers 1

sessionFunction · 0.45

Calls 5

_setPageMethod · 0.95
_waitForActionMethod · 0.95
filterMethod · 0.80
debugMethod · 0.80
typeMethod · 0.45

Tested by

no test coverage detected