How to Verify Dialog Text (Internet Explorer only)

PROBLEM

I would like to verify the text of a Dialog that is fired from the browser during test execution.

SOLUTION

This is possible with a coded solution. The code serves two purposes. It handles the dialog and verifies the text within it. The first part is the standard way to handle dialogs in code as seen here. The second part uses a delegate to implement a custom handler for the dialog.

 

Note: This code requires an assembly reference to System.Windows.Forms. Here is an article on how to add an assembly reference in the Standalone version.

 

C#

public void MyCustomAlertHandler(IDialog dialog)
{
    string txt = dialog.Window.AllChildren[dialog.Window.AllChildren.Count - 1].Caption;
    Assert.AreEqual<string>("Hello! I am an alert box!", txt);
    Log.WriteLine("Dialog text: " + txt);
     
    Manager.Desktop.KeyBoard.KeyPress(Keys.Enter);
    dialog.HandleCount++;   
}
     
[CodedStep(@"New Coded Step")]
public void VerifyDialogText_CodedStep()
{
    ActiveBrowser.NavigateTo("http://www.w3schools.com/JS/tryit.asp?filename=tryjs_alert");
     
    // Initialize custom 'Alert' dialog handler
    AlertDialog alertDialog = AlertDialog.CreateAlertDialog(ActiveBrowser, DialogButton.OK);
    alertDialog.HandlerDelegate = MyCustomAlertHandler;
    Manager.DialogMonitor.AddDialog(alertDialog);
      
    // Click the button to fire the Alert Dialog inside the browser
    ActiveBrowser.Frames["view"].Find.ByExpression<HtmlInputButton>("value=Show alert box", "tagname=input").Click();
        
    // Wait Until Dialog is Handled.
    alertDialog.WaitUntilHandled(5000);
}

 

Visual Basic

Public Sub MyCustomAlertHandler(dialog As IDialog)
    Dim txt As String = dialog.Window.AllChildren(dialog.Window.AllChildren.Count - 1).Caption
    Assert.AreEqual(Of String)("Hello! I am an alert box!", txt)
    Log.WriteLine("Dialog text: " + txt)
 
    Manager.Desktop.KeyBoard.KeyPress(Keys.Enter)
    dialog.HandleCount += 1
End Sub
 
<CodedStep("New Coded Step")> _
Public Sub VerifyDialogText_CodedStep()
    ActiveBrowser.NavigateTo("http://www.w3schools.com/JS/tryit.asp?filename=tryjs_alert")
 
    ' Initialize custom 'Alert' dialog handler
    Dim alertDialog__1 As AlertDialog = AlertDialog.CreateAlertDialog(ActiveBrowser, DialogButton.OK)
    alertDialog__1.HandlerDelegate = MyCustomAlertHandler
    Manager.DialogMonitor.AddDialog(alertDialog__1)
 
    ' Click the button to fire the Alert Dialog inside the browser
    ActiveBrowser.Frames("view").Find.ByExpression(Of HtmlInputButton)("value=Show alert box", "tagname=input").Click()
 
    ' Wait Until Dialog is Handled.
    alertDialog__1.WaitUntilHandled(5000)
End Sub

 

Ensure you add the following using or Imports statements to the top of the code-behind file. Click the View Class button, scroll to the top of the code, and add these lines:

 

C#

using ArtOfTest.WebAii.Win32.Dialogs;
using System.Windows.Forms;

 

Visual Basic

Imports ArtOfTest.WebAii.Win32.Dialogs
Imports System.Windows.Forms