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

Conditionally Handling Dialog

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.

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
Manager.ActiveBrowser.Frames["iframeResult"].Find.ByContent<HtmlButton>("Try it").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");
In this article