tags:

views:

1184

answers:

5
+3  Q: 

Watin and PDF's

Can anyone provide and example of downloading a PDF file using Watin? I tried the SaveAsDialogHandler but I couldn't figure it out. Perhaps a MemoryStream could be used?

Thanks,

--jb

+2  A: 

Hi,

This code will do the trick. The UsedialogOnce class can be found in the WatiN.UnitTests code and will be part of the WatiN 1.3 release (which will probably be released tonigh 14 october).

FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(file.FullName); using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler)) { ie.Button("exportPdfButtonId").ClickNoWait();

fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(30);
fileDownloadHandler.WaitUntilDownloadCompleted(200);

}

HTH, Jeroen van Menen Lead developer WatiN

+4  A: 
FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(file.FullName);
using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler))
{
    ie.Button("exportPdfButtonId").ClickNoWait();

    fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(30);
    fileDownloadHandler.WaitUntilDownloadCompleted(200);
}
+1  A: 

Thank you for the answer, Jeroen, but it is still now working for me. I think the problem may be with the Adobe PDF "helper" opening up the pdf in IE instead of prompting for download. Any ideas? Here is my code:

  IE ie = new IE("http://www.sec.gov/pdf/handbook.pdf");

  string fullFileName = @"C:\test.pdf";

  FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(fullFileName);
  using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler)) {
   ie.Button("exportPdfButtonId").ClickNoWait();
   fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(30);
   fileDownloadHandler.WaitUntilDownloadCompleted(200);
  }

  ie.Close();
  ie.Dispose();

Thanks,

--jason

+1  A: 

I just ran into this same issue, except I use Foxit instead of Acrobat. I told Foxit not to run in the browser, then this code started working just fine. Here's a complete unit test that should do the trick:

     string file = Path.Combine(Directory.GetCurrentDirectory(), "test.pdf");

  using (IE ie = new IE())
  {
   FileDownloadHandler handler = new FileDownloadHandler(file);

   using (new UseDialogOnce(ie.DialogWatcher, handler))
   {
    try
    {
     ie.GoToNoWait("http://www.tug.org/texshowcase/cheat.pdf");

     //WatiN seems to hang when IE loads a PDF, so let it timeout...
     ie.WaitForComplete(5);
    }
    catch (Exception)
    {
     //Ok.
    }

    handler.WaitUntilFileDownloadDialogIsHandled(30);
    handler.WaitUntilDownloadCompleted(30);
   }

  }

  Assert.That(File.Exists(file));
DanMan