ionic-team / stencil

A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
https://stenciljs.com
Other
12.52k stars 782 forks source link

Ability to create an incognito page for E2E tests #2536

Open simonhaenisch opened 4 years ago

simonhaenisch commented 4 years ago

I want to do some e2e testing related to service workers, so I register it using

const page = await newE2EPage({ url: '/' });
await page.evaluate(async () => navigator.serviceWorker.register('/sw.js'));

I want to do it in an incognito page because I don't want the service worker to be registered for the rest of the tests. Ideally there would be an incognito: boolean option as part of NewE2EPageOptions, that creates a page in a separate incognito browser context.


BTW using browserArgs: ['--incognito'] doesn't actually work because it only makes the initial empty window incognito, but then each test file creates its own browser window with a normal (non-incognito) context.

const page = await newE2EPage({ url: '/' });

console.log(page.browserContext().isIncognito()); // => false
simonhaenisch commented 4 years ago

The tests broke in our CI because the service worker doesn't actually get generated for testing builds (fair enough), and I just had a left-over from a prod build locally. So I have this work-around for now:

import { writeFile } from 'fs/promises';

const page = await newE2EPage({ url: '/' });

// a basice service worker for testing purposes
const sw = `
  self.addEventListener('install', () => {
    if (!self.registration.active) {
      self.skipWaiting();
    }
  });

  self.addEventListener('activate', (e) => {
    e.waitUntil(clients.claim());
  });
`;

const swFilePath = join(__dirname, '..', '..', '..', 'www', 'sw.js');

// write intial service worker
await writeFile(swFilePath, sw, 'utf-8');

// register the service worker
await page.evaluate(async () => navigator.serviceWorker.register('/sw.js'));

// modify the service worker
await writeFile(swFilePath, sw + '\n', 'utf-8');

// update the service worker
await page.evaluate(async () => (await navigator.serviceWorker.getRegistration())?.update());

// emulate Stencil's `swUpdate` event
await page.evaluate(() => window.dispatchEvent(new Event('swUpdate')));
ionitron-bot[bot] commented 4 years ago

Thanks for the issue! This issue is being closed due to inactivity. If this is still an issue with the latest version of Stencil, please create a new issue and ensure the template is fully filled out.

Thank you for using Stencil!