Conditional Dialog Handling
PROBLEM
I would like to conditionally handle a dialog. If it appears, I'd like to handle it normally. If it doesn't, I'd like to continue with test execution.
SOLUTION
This is possible with a coded solution. The action that potentially triggers the dialog must be included in the code.
C#
ActiveBrowser.NavigateTo("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
//Create the dialog and add it to the monitor
AlertDialog ad = AlertDialog.CreateAlertDialog(ActiveBrowser, DialogButton.OK);
Manager.DialogMonitor.AddDialog(ad);
//Trigger the dialog
//Comment out the following line to see the code work when the dialog isn't fired
ActiveBrowser.Frames["view"].Find.ByExpression<HtmlInputButton>("value=Show alert box", "tagname=input").Click();
//If the dialog fires, it's handled
//If not, the exception is silently disposed
try
{
ad.WaitUntilHandled(2000);
}
catch (TimeoutException)
{
}
//Remove the dialog from the monitor
Manager.DialogMonitor.RemoveDialog(ad);
ActiveBrowser.NavigateTo("http://www.bing.com");
Visual Basic
ActiveBrowser.NavigateTo("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert")
'Create the dialog and add it to the monitor
Dim ad As AlertDialog = AlertDialog.CreateAlertDialog(ActiveBrowser, DialogButton.OK)
Manager.DialogMonitor.AddDialog(ad)
'Trigger the dialog
'Comment out the following line to see the code work when the dialog isn't fired
ActiveBrowser.Frames("view").Find.ByExpression(Of HtmlInputButton)("value=Show alert box", "tagname=input").Click()
'If the dialog fires, it's handled
'If not, the exception is silently disposed
Try
ad.WaitUntilHandled(2000)
Catch generatedExceptionName As TimeoutException
End Try
'Remove the dialog from the monitor
Manager.DialogMonitor.RemoveDialog(ad)
ActiveBrowser.NavigateTo("http://www.bing.com")
Ensure you add the following using or Imports statement to the top of the code-behind file. Click the View Class button, scroll to the top of the code, and add this line:
C#
using ArtOfTest.WebAii.Win32.Dialogs;
Visual Basic
Imports ArtOfTest.WebAii.Win32.Dialogs