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

非 Y 不嫁゛ 提交于 2019-12-01 19:07:40
MoMo

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: WatiN Error Could not Load Assembly

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." ); 
}

Thanks, MoMo, for this answer. It was very helpful to me as well.

I would just like to give an additional solution to this answer just in case anyone else runs into same problem I did.

I initially received an error implementing this line:

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

"SHDocVw.ShellWindowsClass cannot be embedded, use applicable interface instead"

The solution can be found here: http://www.debugging.com/bug/24258

You need to right click on the reference Interop.ShDocVW.dll and set the embed interop types to false.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!