Handle IE8 Download Dialogs in Code

PROBLEM

I would like to handle a File Download Dialog in code (the coded equivalent of a Handle Download Dialog step in Test Studio) in Internet Explorer 8. This solution is not suitable for other browsers.

SOLUTION

A Download Dialog in IE8 is a separate entity from a Save As dialog. Handling a Download Dialog is more in-depth because it's three different dialogs rolled into one:

  • Download Dialog
  • SaveAsDialog
  • Download Complete Dialog

 

In order to successfully handle this, you need to handle each of these separately in code. A Download Dialog sample can be seen in this Telerik demo page. Below is a code sample which triggers and handles the Download Dialog seen in this demo. Notice that three Dialog Handlers are added to the Dialog monitor before the Download Dialog is triggered. This is an important detail - adding a Dialog Handler to the Monitor after the intended Dialog has already been triggered inside of the browser will not work.

 

C#

  
//Create first dialog
IEDownloadDialog part1 = new IEDownloadDialog(ActiveBrowser, DialogButton.SAVE, Manager.Desktop);
 
//Create second dialog
SaveAsDialog part2 = SaveAsDialog.CreateSaveAsDialog(ActiveBrowser, DialogButton.SAVE, "c:\\abc", Manager.Desktop);
  
//Create third dialog
IEDownloadCompleteDialog part3 = new IEDownloadCompleteDialog(ActiveBrowser, DialogButton.CLOSE, Manager.Desktop);
               
//Add each dialog to the monitor
Manager.DialogMonitor.AddDialog(part1);
Manager.DialogMonitor.AddDialog(part2);
Manager.DialogMonitor.AddDialog(part3);
  
//Trigger the download dialog in the browser
Find.ByExpression<HtmlDiv>(new HtmlFindExpression("id=RadFileExplorer1_grid_ctl00__4", "|", "tagIndex=div:0")).MouseClick(MouseClickType.LeftDoubleClick);
  
//Dialog handlers work consecutively - the order is important
part1.WaitUntilHandled(20000);
part2.WaitUntilHandled(20000);
part3.WaitUntilHandled(20000);

 

Visual Basic

 
'Create first dialog
Dim part1 As New IEDownloadDialog(ActiveBrowser, DialogButton.SAVE, Manager.Desktop)
 
'Create second dialog
Dim part2 As SaveAsDialog = SaveAsDialog.CreateSaveAsDialog(ActiveBrowser, DialogButton.SAVE, "c:\abc", Manager.Desktop)
 
'Create third dialog
Dim part3 As New IEDownloadCompleteDialog(ActiveBrowser, DialogButton.CLOSE, Manager.Desktop)
 
'Add each dialog to the monitor
Manager.DialogMonitor.AddDialog(part1)
Manager.DialogMonitor.AddDialog(part2)
Manager.DialogMonitor.AddDialog(part3)
 
'Trigger the download dialog in the browser
Find.ByExpression(Of HtmlDiv)(New HtmlFindExpression("id=RadFileExplorer1_grid_ctl00__4", "|", "tagIndex=div:0")).MouseClick(MouseClickType.LeftDoubleClick)
 
'Dialog handlers work consecutively - the order is important
part1.WaitUntilHandled(20000)
part2.WaitUntilHandled(20000)
part3.WaitUntilHandled(20000)

 

  • This code will can be inserted in a coded step in Test Studio, or in a unit test using Telerik Testing Framework.
  • Ensure you add an assembly reference (and a using or Imports statement) to ArtOfTest.WebAii.Win32.DialogsHere is how to add an assembly reference in Test Studio Standalone version.
  • Also see our article on Handling Download Dialogs.

 

Note: This solution is specific to Internet Explorer 8. Each browser (or browser version) has its own dialog implementation. Thus the same code will not work for IE9, Firefox, Chrome, or Safari.