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.*


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:


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); 
        [CodedStep(@"New Coded Step")] 
        public void HandleDialogLogText_CodedStep() 
            errorDialog = new GenericDialog(ActiveBrowser, "Custom Title", true); 
            errorDialog.HandlerDelegate = HandleErrorDialog; 
        public void HandleErrorDialog(IDialog dialog) 
            txt += "\r\n"; 
            txt += "Dialog " + (dialog.HandleCount + 1).ToString() + ": " + dialog.Window.AllChildren[dialog.Window.AllChildren.Count - 1].Caption; 
            Thread resetDialog = new Thread(new ThreadStart(ResetDialog)); 
        public void ResetDialog() 
            errorDialog.CurrentState = DialogCurrentState.NotActive; 
Imports System.Threading 
Imports System.Windows.Forms 
Imports ArtOfTest.WebAii.Win32.Dialogs 
Namespace TestProject7 
    Public Class HandleDialogLogText 
        Inherits BaseWebAiiTest 
        Private txt As String = String.Empty 
        Private errorDialog As GenericDialog 
        Public Overrides Sub OnAfterTestCompleted(result As TestResult) 
            Log.WriteLine("OnAfterTestCompleted dialog text: " + txt) 
        End Sub 
        <CodedStep("New Coded Step")> _ 
        Public Sub HandleDialogLogText_CodedStep() 
            errorDialog = New GenericDialog(ActiveBrowser, "Custom Title", True) 
            errorDialog.HandlerDelegate = HandleErrorDialog 
        End Sub 
        Public Sub HandleErrorDialog(dialog As IDialog) 
            txt += vbCr & vbLf 
            txt += "Dialog " + (dialog.HandleCount + 1).ToString() + ": " + dialog.Window.AllChildren(dialog.Window.AllChildren.Count - 1).Caption 
            dialog.HandleCount += 1 
            Dim resetDialog__1 As New Thread(New ThreadStart(ResetDialog)) 
        End Sub 
        Public Sub ResetDialog() 
            errorDialog.CurrentState = DialogCurrentState.NotActive 
        End Sub 
    End Class 
End Namespace 
In this article
Not finding the help you need? Improve this article