microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.32k stars 3.71k forks source link

[Feature] Support web push notification handling/testing #23954

Open TomasHubelbauer opened 1 year ago

TomasHubelbauer commented 1 year ago

This is a follow-up to an existing but closed ticket #3301. I was asked to open a new ticket. I specifically care about handling web push notifications. It seems that is somewhat possible in Playwright now (I would love a clarification on what is expected to work and supported and what is explicitly not expected to work [yet]), however there are some issues:

import { test } from '@playwright/test';

test('has title', async ({ page }) => {
  await page.goto(`file://${__dirname}/../index.html`);

  page.on('console', msg => console.log(msg.text()));

  page.on('dialog', async dialog => {
    console.log(dialog.type(), dialog.message());
    await dialog.accept();
  });

  await page.context().grantPermissions(['notifications']);
  await page.click('button');
  //expect(await page.evaluate(() => Notification.permission)).toBe('granted');
  await page.locator('body.shown').waitFor();
});

My page's script:

const button = document.querySelector('button');

button.addEventListener('click', async () => {
  alert('test');

  console.log('requesting');
  const notificationPermission = await Notification.requestPermission();
  console.log('requested', notificationPermission, Notification.permission);
  const notification = new Notification('test');
  notification.onshow = () => {
    document.body.classList.toggle('shown', true);
    console.log('shown');
  }

  console.log('notification', notification);
});

When running headless, this gets stuck waiting for the notification's show event. It eventually times out. The notification is never shown. The test fails.

When running headed (or as I prefer, headful), the show event gets invoked but no actual notifications appears. Even when I tack on a timeout at the end of the test to give the notification time to appear, it never does. The test passes.

The notification doesn't trigger the dialog event in any case so I cannot be asserted that way.

Also Notification.permission always evaluates to denied regardless of await page.context().grantPermissions(['notifications']);.

kenanAlhindi97 commented 1 year ago

This would be indeed a great feature to add. Our team manages a push service and being able to test the web push part of the flow.

My usecase is a bit different though, since playwright launches in incognito mode only and there's no way to change that behavior, I'm stuck with the following issue :

Chrome currently does not support the Push API in incognito mode (https://crbug.com/401439). There is deliberately no way to feature-detect this, since incognito mode needs to be undetectable by websites.

But maybe working on this ticket/feature would have the side effect of somehow fixing this ? in either case, there's definite interest.

TomasHubelbauer commented 1 year ago

@kenanAlhindi97 I am not 100 % sure but I think using launchPersistentContext might help you some - AFAIK that type of context is not incognito. Thought you'll still end up where I ended up even if this removes the incognito problem for you, so it is not a full solution.