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

javascript - (node:13276) UnhandledPromiseRejectionWarning: Error: Cannot accept dialog which is already handled - Stack Overflo

programmeradmin0浏览0评论

(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 .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!

(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/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!

Share Improve this question edited Feb 13, 2020 at 17:38 webprogrammer 2,4773 gold badges22 silver badges27 bronze badges asked Feb 13, 2020 at 17:13 Samoila AndreiSamoila Andrei 3482 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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();
});

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");
    })
发布评论

评论列表(0)

  1. 暂无评论