最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Service worker installation in Headless Chrome (via puppeteer) - Stack Overflow

programmeradmin2浏览0评论

I have a service worker that is registered with

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/service-worker.bundle.js').then(registration => {
        console.log('Service worker was registered');
    }).catch(error => {
        console.log('Registration failed: ', error);
    });
}

The actual service worker is logging its install and activate events with regular console.log() calls, all of this is working as expected.

However, when it came to the testing automation, the Headless Chrome / puppeteer solution is not working as it was expected, the service worker is not installed (the install event does not happen). So, the question is, is there any special way of testing pages with service workers with Headless Chrome / puppeteer?

The puppeteer code:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ args: ['--no-sandbox']});
  const page = await browser.newPage();
  page.on('console', msg => console.log('PAGE LOG:', msg.text()));

  await page.goto('https://***', { waitUntil: 'networkidle0' });
  await page.waitFor(1*4000);
  console.log('Before reload');
  await page.reload({ waitUntil: 'networkidle0' });
  await page.screenshot({path: 'public/vidi.png'});
  await browser.close();
})();

Links:

  • GitHub issue
  • Headless Chrome
  • Puppeteer
  • Chrome DevTools Protocol

I have a service worker that is registered with

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/service-worker.bundle.js').then(registration => {
        console.log('Service worker was registered');
    }).catch(error => {
        console.log('Registration failed: ', error);
    });
}

The actual service worker is logging its install and activate events with regular console.log() calls, all of this is working as expected.

However, when it came to the testing automation, the Headless Chrome / puppeteer solution is not working as it was expected, the service worker is not installed (the install event does not happen). So, the question is, is there any special way of testing pages with service workers with Headless Chrome / puppeteer?

The puppeteer code:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ args: ['--no-sandbox']});
  const page = await browser.newPage();
  page.on('console', msg => console.log('PAGE LOG:', msg.text()));

  await page.goto('https://***.', { waitUntil: 'networkidle0' });
  await page.waitFor(1*4000);
  console.log('Before reload');
  await page.reload({ waitUntil: 'networkidle0' });
  await page.screenshot({path: 'public/vidi.png'});
  await browser.close();
})();

Links:

  • GitHub issue
  • Headless Chrome
  • Puppeteer
  • Chrome DevTools Protocol
Share Improve this question edited Apr 30, 2018 at 8:21 Md. Abu Taher 18.9k5 gold badges58 silver badges78 bronze badges asked Apr 28, 2018 at 5:51 AleksandrAleksandr 2,3312 gold badges22 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Yes,

const browser = await puppeteer.launch({
    args: ['--enable-features=NetworkService'],
    headless: true,
    ignoreHTTPSErrors: true,
  });

--enable-features=NetworkService enables service worker (experimental) and ignoreHTTPSErrors is required to overe the https requirement of service workers when in the puppeteer context (served over file:///)

发布评论

评论列表(0)

  1. 暂无评论