问题
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:
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." );
}
回答2:
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.
来源:https://stackoverflow.com/questions/1191897/how-to-check-if-pdf-was-successfully-opened-in-the-browser-using-watin