Accept a Dialog with Playwright Sharp

1.5k views Asked by At

How do I Accept a Dialog like the one below using Playwright Sharp?

Confirm Dialog

I can't seem to find anything in Playwright Sharp's documentation, however Playwright's (JS) documentation describes how to add an event handler for a dialog:

page.on('dialog', dialog => dialog.accept());

Is there a similar way to do this in Playwright Sharp?

4

There are 4 answers

0
Alastairm On

page.Dialog triggers dialog events when they appear, it's just a matter of registering an event handler to it as below:

page.Dialog += (_, x) => x.Dialog.AcceptAsync();

0
Poy Chang On

You can preset page.WaitForEventAsync for dialog action and timeout, default timeout is 30s. When dialog activate, that will trigger your preset action and wait for timeout to continue the rest code.

// Preset dialog react
var dialogTask = page.WaitForEventAsync(PageEvent.Dialog, args =>
{
    args.Dialog.DismissAsync();
    return true;
}, 1000);

// Activate alert dialog
await Page.EvaluateAsync("alert('yo');");

await dialogTask;

You can find playwright-sharp page dialog unit test here and try to figure out how to use that.

0
user23384025 On

By default, Playwright will dismiss all (Alert, Popup) dialogues. To accept the alert, place the below code before the alert trigger code:

PageInTest.Dialog += (_, dialog) => dialog.AcceptAsync();

This will register the Page.Dialog in Playwright.

2
user23821396 On
--await btnAdd.ClickAsync();
await wlsWorkType.ClickAsync();