Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(node:13276) UnhandledPromiseRejectionWarning: Error: Cannot accept dialog which is already handled

(node:13276) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Hi guys, I am getting the warning above from the title after I am calling this function 2 times:

async function delete_page(page, page_id) {
  await page.goto('http://127.0.0.1:3000/page/' + page_id,
    { waitUntil: 'domcontentloaded' });

  const navPromise = page.waitForNavigation();
  page.on('dialog', async function(dialog) {
    await dialog.accept();
  });
  await page.click("#delete-page-button");
  await navPromise;
}

I am calling the function delete_page() here:

  after(async function() {
    this.timeout(0);
    await logout(page);
    await login(page, 'AdminTester', 'password');
    await delete_page(page, 'test');
    await delete_page(page, 'test2');
    await logout(page);
    await page.close();
  });

How can I get rid of this warning? Thanks!

like image 324
Samoila Andrei Avatar asked Nov 25 '25 06:11

Samoila Andrei


2 Answers

The error is saying that you can't do this twice.

await dialog.accept();

You are doing that twice because you are registering a new event every time you call delete_page.

You could solve that doing this inside your after function instead of inside delete_page.

page.on('dialog', async function(dialog) {
    await dialog.accept();
});

You could also use the once function which will be called only ... once. But you need to be sure that you are going to get a dialog on every call, so you don't get more than one registration.

page.once('dialog', async function(dialog) {
    await dialog.accept();
});
like image 146
hardkoded Avatar answered Nov 27 '25 20:11

hardkoded


using page.once resolved my issue. Page.once will create dialog listener only for once. In this case each time you have to handle a dialog, you have to create dialog listener with page.once.

page.once('dialog', async (dialog) => {
      expect(dialog.message()).toEqual('to what day?');
      expect(dialog.type()).toEqual("prompt");
      await page.waitForTimeout(3000);
      await dialog.accept("5");
    })
like image 25
sandeep bastola Avatar answered Nov 27 '25 18:11

sandeep bastola