New to Telerik Test Studio? Download free 30-day trial

How to Verify Dialog Text (Internet Explorer only)

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


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.

string dialogText;

[CodedStep(@"Navigate then verify text in popup dialog")]
public void VerifyDialogText_CodedStep()

    // Click the button to fire the Alert Dialog inside the browser
    FrameInfo myFrame = new FrameInfo("iframeResult", "", "", 0);
    Browser frame = ActiveBrowser.Frames[myFrame];
    HtmlButton tryItButton = frame.Find.ByTagIndex<HtmlButton>("button", 0);

    // Initialize custom 'Alert' dialog handler
    AlertDialog alertDialog = AlertDialog.CreateAlertDialog(ActiveBrowser, DialogButton.OK);
    alertDialog.HandlerDelegate = MyCustomAlertHandler;

    // Wait Until Dialog is Handled.

    // Validate the text that was captured by the custom dialog handler
    Assert.AreEqual<string>("I am an alert box!", dialogText);

public void MyCustomAlertHandler(IDialog dialog)
    // Capture the text displayed in the dialog. The contents will be validated by the main thread.
    dialogText = dialog.Window.AllChildren[dialog.Window.AllChildren.Count - 1].Caption;
    Log.WriteLine("Dialog text: " + dialogText);

Private dialogText As String

<CodedStep("Navigate then verify text in popup dialog")> _
Public Sub VerifyDialogText_CodedStep()

    Dim alertDialog__1 As AlertDialog = AlertDialog.CreateAlertDialog(ActiveBrowser, DialogButton.OK)
    alertDialog__1.HandlerDelegate = AddressOf MyCustomAlertHandler

    Dim tryItButton As HtmlButton = ActiveBrowser.Frames("view").Find.ByTagIndex(Of HtmlButton)("button", 0)


    Assert.AreEqual(Of String)("I am an alert box!", dialogText)
End Sub

Public Sub MyCustomAlertHandler(dialog As IDialog)

    dialogText = dialog.Window.AllChildren(dialog.Window.AllChildren.Count - 1).Caption
    Log.WriteLine(Convert.ToString("Dialog text: ") & dialogText)

    dialog.HandleCount += 1
End Sub

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

using ArtOfTest.WebAii.Win32.Dialogs;
using System.Windows.Forms;
Imports ArtOfTest.WebAii.Win32.Dialogs
Imports System.Windows.Forms
In this article