HaveSpacesuit
HaveSpacesuit

Reputation: 4004

Handle dialog immediately opened in new tab

I'm writing a Playwright test for the case in which a link opens an application via a new tab. The user clicks a link, and a new tab is opened with a custom application url.

The browser normally asks "Do you want to open this link," and the user must click accept or cancel.

In Playwright, the new page is blocked until after the dialog is confirmed. If I don't have access to the page, is it somehow possible to listen for the dialog event?

const context = page.context();

async function newPageOpened(): Promise<void> {
  const newPage = await context.waitForEvent('page')
  // I won't have access to newPage unless I manually click the Accept button 🙁

  newPage.on('dialog', (dialog) => {
    void dialog.accept();
  });
}

async function action(): Promise<void> {
  // Click the link
}

await Promise.all([newPageOpened, action()])

enter image description here

Upvotes: 0

Views: 893

Answers (1)

hardkoded
hardkoded

Reputation: 21695

The dialog is a Chrome (only) implementation. Playwright doesn't know how to handle them. So, you won't be able to dismiss it using the dialog event.
You can follow this issue here.

Upvotes: 1

Related Questions