7

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

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130

4 Answers4

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

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

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

1

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));
Matt Honeycutt
  • 1,009
  • 11
  • 16
0

Take a look at this post:

How to check if PDF was successfully opened in the browser using WatiN?

Community
  • 1
  • 1
MoMo
  • 8,149
  • 3
  • 35
  • 31