question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Notifications not available in headless mode

See original GitHub issue
  • Puppeteer version: 1.8.0
  • Platform / OS version: Windows 8.1
  • Node.js version: v10.11.0

What steps will reproduce the problem? Assuming there is a registered serviceworker and a notification:

const puppeteer = require('puppeteer');

(async() => {
    const browser = await puppeteer.launch({
        headless: true
    });

    const context = browser.defaultBrowserContext();
    const page = await context.newPage();

    await page.goto('https://notificationDomain.com', {
        waitUntil: "networkidle2"
    });

    const notifications = await page.evaluate(() => {
        return navigator.serviceWorker.ready.then(function(registration) {
            return registration.getNotifications().then(function(notifications) {
                return notifications;
            })
        })
    });

    console.log(notifications.length + ' Notifications');
    await browser.close();
})();

What is the expected result? The notifications array should contain the notification like it does when using the non-headless mode.

What happens instead? The notifications array is empty although there is a notification.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
aslushnikovcommented, Nov 1, 2018

@paul-kl can you please try enabling “notifications” using browserContext.overridePermissions?

4reactions
pk992commented, Nov 4, 2018

@aslushnikov I’m sorry, forgot that in my example but used it in my code. I hope this helps:

const puppeteer = require('puppeteer');

(async() => {
    const browser = await puppeteer.launch({
        headless: false
    });

    const domain = 'https://puush.pw'
    const context = browser.defaultBrowserContext();
    const page = await context.newPage();

    await context.overridePermissions(domain, ['notifications']);

    try {
       await context.overridePermissions(domain, ['push']);
    } catch (e) {
       // workaround because 'push' throws unknown permisson type but works anyways
    }

    await page.goto('https://puush.pw', {
        waitUntil: "networkidle2"
    });

    // Wait for the SW
    await page.waitFor(1*5000);

    const sw = await page.evaluate(() => {
      return navigator.serviceWorker.getRegistrations().then(registrations => {
        return registrations && registrations.length > 0 ? true : false;
      })
    })

    // Give me time to send the push notification
    await page.waitFor(1*15000);
    console.log('Push check...');

    if(sw){
      const notifications = await page.evaluate(() => {
          return navigator.serviceWorker.ready.then(function(registration) {
              return registration.getNotifications().then(function(notifications) {
                  return notifications;
              })
          })
      });

      console.log(notifications.length + ' Notifications');
    } else {
      console.log('SW not registered!');
    }

    await browser.close();
})();

Output non-headless:

Push check...
0 Notifications

or when I did send one (onesignal.com service running on the domain)

Push check...
1 Notifications

Output headless:

Push check...
SW not registered!

The serviceworker won’t register. If I register it non-headless and try to get push notifications headless, they won’t show up. There is something strange about the ‘push’ permission but once I added it, I was able to recive push notifications, not just normal ones.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to allow notifications on headless chrome using selenium ...
My tests work until I switch to headless, where it seems to default to an incognito browser and will not allow notifications. Allowing...
Read more >
Headless Chrome, Native Notifications on macOS ... - YouTube
With Chrome 59, you can run Chrome in an automated environment without a user interface or peripherals; notifications on macOS are shown ...
Read more >
Handle pop-ups, permissions, and notifications - BrowserStack
Learn how to manage pop-ups, permissions, and notifications in different browsers when running your Selenium tests on BrowserStack Automate.
Read more >
GUI and Headless Browser Testing - Travis CI Docs
Note that sudo is not available for builds that are running on the container-based workers. Using the Chrome addon in the headless mode...
Read more >
Handle Browser Level Notification Using Selenium - Webkul
This request is to disable notification.Right now there is no option to enable the same as above subject is not treated as a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found