Attaching to PDF Window in IE using WatiN

posted by Bryan on

I was trying to automate some tests that included a PDF loaded inside an IE 7 window. Using conventional methods, I could not attach to this new window, thus couldn't control whether it was done loading, or close it for that matter.

I was trying to automate some tests that included a PDF loaded inside an IE 7 window. Using conventional methods, I could not attach to this new window, thus couldn't control whether it was done loading, or close it for that matter.

I tried attaching by title/url to no avail. So, I looped through all IE windows open and it wasn't there.

I found this post (PDF with WatiN).

It allows me to get information from that window and close it without any problems. I can also check to see if the window is still busy (aka loading). WatiN uses the SHDocVw dll, but doesn't wrap these other "IE" windows into the IECollection, thus not being able to access it. Bypass the IECollection to get to the window you need.

public static void closePDFInIE(string partialPDFUrl)
        {
            ShellWindows shellWindows = new ShellWindowsClass();

            string filename;

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

                if (filename.Equals("iexplore") && ie.LocationURL.Contains(partialPDFUrl))
                {
                    int i = 0;
                    bool hasLoaded = false;
                    do
                    {
                        if (!ie.Busy)
                        {
                            hasLoaded = true;
                        }
                        else
                        {
                            i++;
                            Thread.Sleep(1000);
                        }
                    } while (!hasLoaded || i == 25); //only wait 25 seconds, then move on.
                    ie.Quit();
                }
            }
        }

Leave a comment

blog comments powered by Disqus