4

部署后,我正在使用 WatiN 库在网站上进行快速冒烟测试。除其他外,我想确保当我单击页面上的特定链接时,会在浏览器中打开 PDF。单击链接很容易,但是如何检测 Acrobat Reader 是否在浏览器窗口中成功打开?我想捕捉诸如 404、服务器错误或超时之类的情况......

4

2 回答 2

6

擦,

我在这个问题上找到了一份错误报告和一份功能请求。错误和功能请求都已关闭,但我仍然无法附加到托管 PDF 文档的 IE 窗口。

事实上,托管 PDF 文件的窗口甚至不是 IE.InternetExplorers() 或 InternetExplorersNoWait() 集合中的项目。

我在 Windows XP 上使用 WatiN 2.0.10.928 和 IE7。

我的解决方案是使用向 Interop.ShDocVw.dll 添加 COM 引用并自己检查窗口,因为托管 PDF 查看器的 IE 窗口由于某种原因不是 WatiN.IE.InternetExplorers() 集合中的项目。

不要使用 Add Reference -> COM -> Microsoft Internet Controls v1.1 添加对 Interop.ShDocVw.dll 的引用。如果你这样做,你很有可能会收到这个例外:

System.IO.FileLoadException:无法加载文件或程序集 'Interop.SHDocVw,Version=1.1.0.0,Culture=neutral ... 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配(HRESULT 异常:0x80131040)

相反,您需要引用随 WatiN 分发的 ShDocVw.dll。查看“WatiN 错误无法加载程序集”StackOverflow 问题以获取更多信息: WatiN 错误无法加载程序集

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." ); 
}
于 2009-09-21T22:25:54.657 回答
0

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.

于 2011-09-12T14:21:08.107 回答