| 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. |