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

Log Error Dialogs

*My application can potentially display a dialog containing an error message during test execution. The dialog can appear multiple times and will not always appear at the same point in the test, therefore I cannot use a standard Dialog Handler step.

I want to capture these error messages and automatically handle these dialogs if they appear.*

Solution

This is possible with a coded solution. The stipulation is that the dialog is displayed the same way and has the same title in each browser:

Dialog

1.  Add an Assembly Reference to System.Windows.Forms. 2.  Add a coded step to the beginning of the test that creates a dialog monitor. 3.  Define a custom dialog handler that is referenced in the coded step. 4.  Override the OnAfterTestCompleted method to log whatever text the dialog handler captured. 5.  Auto reset the dialog handler in case of multiple dialogs.

Here is the full code-behind file, excluding the standard using/Imports statements and the Dynamic Pages Reference region:

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

namespace TestProject7
{
    public class HandleDialogLogText : BaseWebAiiTest
    { 
        string txt = string.Empty;
        GenericDialog errorDialog;

        public override void OnAfterTestCompleted(TestResult result)
        {
            Log.WriteLine("OnAfterTestCompleted dialog text: " + txt);
            base.OnAfterTestCompleted(result);
        }

        [CodedStep(@"New Coded Step")]
        public void HandleDialogLogText_CodedStep()
        {
            errorDialog = new GenericDialog(ActiveBrowser, "Custom Title", true);
            errorDialog.HandlerDelegate = HandleErrorDialog;
            Manager.DialogMonitor.AddDialog(errorDialog);          
        }

        public void HandleErrorDialog(IDialog dialog)
        {
            txt += "\r\n";
            txt += "Dialog " + (dialog.HandleCount + 1).ToString() + ": " + dialog.Window.AllChildren[dialog.Window.AllChildren.Count - 1].Caption;
            Manager.Desktop.KeyBoard.KeyPress(Keys.Enter);
            dialog.HandleCount++;
            Thread resetDialog = new Thread(new ThreadStart(ResetDialog));
            resetDialog.Start();
        }

        public void ResetDialog()
        {
            Thread.Sleep(500);
            errorDialog.CurrentState = DialogCurrentState.NotActive;
        }
    }
}
In this article