New to Telerik Test Studio Dev EditionStart a 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.

Solution

This is possible with a coded solution. The code will both handle the dialog and verify the text within it. The second part uses a delegate to implement a custom handler for the dialog.

C#
    string dialogText;
    
    [CodedStep(@"Navigate then verify text in popup dialog")]
    public void VerifyDialogText_CodedStep()
    {
        ActiveBrowser.NavigateTo("http://www.w3schools.com/JS/tryit.asp?filename=tryjs_alert");
    
        // 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);
        Assert.IsNotNull(tryItButton);
        
        
        // Initialize custom 'Alert' dialog handler
        AlertDialog alertDialog = AlertDialog.CreateAlertDialog(ActiveBrowser, DialogButton.OK);
        Manager.DialogMonitor.Start();
        alertDialog.HandlerDelegate = MyCustomAlertHandler;
        Manager.DialogMonitor.AddDialog(alertDialog);
        tryItButton.Click();
        
        // Wait Until Dialog is Handled.
        alertDialog.WaitUntilHandled(20000);

        // 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);
    
        Manager.Desktop.KeyBoard.KeyPress(Keys.Enter);
        dialog.HandleCount++;
    }

Ensure you add the following using or Imports statements to the top of the code-behind file.

C#
    using ArtOfTest.WebAii.Win32.Dialogs;
    using System.Windows.Forms;
In this article
Solution
Not finding the help you need?
Contact Support