如何检查使用WatiN在浏览器中是否成功打开PDF? [英] How to check if PDF was successfully opened in the browser using WatiN?

查看:101
本文介绍了如何检查使用WatiN在浏览器中是否成功打开PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

推荐答案

Rub,

我在此问题上发现了一个错误报告和一个功能请求.错误和功能请求都已关闭,但是我仍然无法附加到承载PDF文档的IE窗口.

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.

实际上,承载PDF文件的窗口甚至都不是IE.InternetExplorers()或InternetExplorersNoWait()集合中的项目.

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

我正在Windows XP上使用WatiN 2.0.10.928和IE7.

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

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

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.

请勿通过使用添加引用-> COM-> Microsoft Internet控件v1.1添加对Interop.ShDocVw.dll的引用.如果这样做,您很有可能会收到此异常:

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:无法加载文件或程序集'Interop.SHDocVw,版本= 1.1.0.0,区域性=中性...或其依赖项之一.找到的程序集的清单定义与程序集引用不匹配(HRESULT的异常:0x80131040)

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)

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

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

这篇关于如何检查使用WatiN在浏览器中是否成功打开PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆