tags:

views:

486

answers:

1

I am using WatiN library to do a quick smoke test on a web site after deployment. Among other things, I want to make sure that when I click on a specific link on my page, a PDF is opened in the browser. Clicking the link is easy, but how can I detect if Acrobat Reader was successfully opened in the browser window? I would like to catch situations like 404, server errors, or time-outs...

+1  A: 

Rub,

I found one bug report and one feature request on this issue. Both the bug and feature request are closed but I am still not able to attach to a IE window that is hosting a PDF document.

In fact the window that is hosting the PDF file is not even an item in the IE.InternetExplorers() or the InternetExplorersNoWait() collections.

I'm using WatiN 2.0.10.928 and IE7 on Windows XP.

My solution was to use add a COM reference to Interop.ShDocVw.dll and check for the window myself since IE windows that host PDF viewers are not items in the WatiN.IE.InternetExplorers() collection for some reason.

Do NOT add a reference to Interop.ShDocVw.dll by using Add Reference -> COM -> Microsoft Internet Controls v1.1. If you do theres a good chance you'll recieve this exception:

System.IO.FileLoadException: Could not load file or assembly 'Interop.SHDocVw, Version=1.1.0.0, Culture=neutral ... or one of its dependencies. The located assembly's manifest definition does not match the assembly reference (Exception from HRESULT: 0x80131040)

Instead you need to reference the ShDocVw.dll thats distributed with WatiN. Check out the 'WatiN Error Could not Load Assembly' StackOverflow question for more info: http://stackoverflow.com/questions/1457635/watin-error-could-not-load-assembly/1462675#1462675

using System.IO;
using SHDocVw; 

namespace PDF_SPIKE
{
    class Program
    [STAThread]
    static void Main(string[] args)
    {
        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); 

        string pdfViewerURL = "http://YourURL/document.pdf";
        bool pdfOpened = false;
        string filename; 

        foreach ( SHDocVw.InternetExplorer ie in shellWindows ) 
        {
            filename = Path.GetFileNameWithoutExtension( ie.FullName ).ToLower(); 

            if ( filename.Equals("iexplore") && ie.LocationURL.Equals(pdfViewerURL)) 
               pdfOpened = true;
        }

        if ( pdfOpened )
           Console.WriteLine( "Your PDF file is OPENED" ); 
        else
           Console.WriteLine( "Your PDF file was NOT found." ); 
}
MoMo