Search code examples
c#alertplaywright

Playwright c# how to verify alerts


Im trying to verify that an alert has appeared and then verify its text content using playwright c#

The alert contains text from two fields in the app and is displayed on pressing a save button

enter image description here

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

auto accepts the dialogue but id like to confirm it actually appears, then verify some of the text contained within it, then accept it with a button click on OK.

Ive tried using various methods on the message option in the IDialog interface. for instance

Page.Dialog += (_, dialog) =>  dialog.Message.Contains("Test");

This causes the test to hang, ( because nothing is closing the alert)

I also tried wrapping the above in an await Expect() statement but this did not work for me

Does anyone know a way to validate the text then move on to another action like clicking ok in order to dismiss the alert?


Solution

  • I eventually found a way to do this:

    string message;
    Page.Dialog += (_, dialog) =>
    {
    message = dialog.Message;
    Assert.That(message, Is.SameAs("expected string"));
    dialog.AcceptAsync();};