Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gorshkov-leonid/899070dd08cfe848fba3e60e1a472f98 to your computer and use it in GitHub Desktop.
Save gorshkov-leonid/899070dd08cfe848fba3e60e1a472f98 to your computer and use it in GitHub Desktop.
puppeteer-page-switching-to-try-avoid-freezing-tests-on-background-tabs.md
await switchTabsPeriodically(headless, webSocketUri, puppeteerConfig);
fixme: failed variant but maybe it can be rethinked:
const puppeteer = require("puppeteer");
async function switchTabsPeriodically(headless, webSocketUri, puppeteerConfig) {
    // I recommend to run only one test in headless mode or use only one runner. Or else you would have the problem with hanged test on inactive tabs.
     if (!headless) {
         let browser = await puppeteer.connect({
             browserWSEndpoint: webSocketUri,
             /* the same viewport to avoid collisions with other connection: */
             ...(puppeteerConfig.connect.defaultViewport ? {defaultViewport: puppeteerConfig.connect.defaultViewport} : {})
         });
         /*
            - https://github.com/puppeteer/puppeteer/issues/4131
            - https://github.com/puppeteer/puppeteer/issues/10639
            Tests on non-active pages hang.
            Switching tabs is the only one found way to wake them up...
            But dnd stops on switching tabs that is why it does not solve the problem, but in turn decreases probability of failures by this reason.

            This problem was reproduced on few couple of tests:
               testMatch: [
                   "**floor-component/src/it/abc.ts",
                   "**floor-component/src/it/load-tiles-by-url.it-test.ts",
                   "**floor-component/src/it/tiles-and-content-origin.it-test.ts",
               ]
         */
         global.setTimeout(function () {
             const intervalId = global.setInterval(async function () {
                 const pages = await browser.pages();
                 if (!pages.length) {
                     return;
                 }
                 let activePage
                 for (const p of pages) {
                     if (await p.evaluate(() => { return document.visibilityState === 'visible' })) {
                         activePage = p;
                         break;
                     }
                 }
                 let activePageIndex = activePage ? pages.indexOf(activePage) : -1;
                 activePageIndex = (activePageIndex + 1) % pages.length;
                 let trueIndex = activePageIndex;
                 while ((trueIndex + 1) % pages.length !== activePageIndex && pages[trueIndex].url().startsWith('chrome:'/* e. g. 'chrome://new-tab-page/' */)) {
                     trueIndex = (trueIndex + 1) % pages.length
                 }

                 activePageIndex = trueIndex
                 await pages[activePageIndex].bringToFront();
             }, 60_000)
             process.env.PAGE_SWITCHER_INTERVAL_ID = `${intervalId}`
         }, 20_000)
     }
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment