Alastairm
Alastairm

Reputation: 85

Accept a Dialog with Playwright Sharp

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?

Upvotes: 1

Views: 1952

Answers (4)

user23821396
user23821396

Reputation: 1

--await btnAdd.ClickAsync();
await wlsWorkType.ClickAsync();

Upvotes: 0

user23384025
user23384025

Reputation: 11

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.

Upvotes: 1

Poy Chang
Poy Chang

Reputation: 1156

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.

Upvotes: 0

Alastairm
Alastairm

Reputation: 85

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

Upvotes: 1

Related Questions