views:

499

answers:

3

Hello,

I have a screen that pops up on load with a print dialog using javascript.

I've just started using WatiN to test my application. This screen is the last step of the test.

What happens is sometimes WatiN closes IE before the dialog appears, sometimes it doesn't and the window hangs around. I have ie.Close() in the test TearDown but it still gets left open if the print dialog is showing.

What I'm trying to avoid is having the orphaned IE window. I want it to close all the time.

I looked up DialogHandlers and wrote this:

var printDialogHandler = new PrintDialogHandler(PrintDialogHandler.ButtonsEnum.Cancel);
ie.DialogWatcher.Add(printDialogHandler);

And placed it before the button click that links to the page, but nothing changed.

The examples I saw had code that would do something like:

someDialogHandler.WaitUntilExists() // I might have this function name wrong...

But PrintDialogHandler has no much member.

I initially wasn't trying to test that this dialog comes up (just that the page loads and checking some values on the page) but I guess it would be more complete to wait and test for the existence of the print dialog.

+2  A: 

Not exactly sure about your situation, but we had a problem with a popup window that also displayed a print dialog box when loaded. Our main problem was that we forgot to create a new IE instance and attach it to the popup. Here is the working code:

btnCoverSheetPrint.Click(); //Clicking this button will open a new window and a print dialog
IE iePopup = IE.AttachToIE(Find.ByUrl(new Regex(".+_CoverPage.aspx"))); //Match url ending in "_CoverPage.aspx"

WatiN.Core.DialogHandlers.PrintDialogHandler pdhPopup = new WatiN.Core.DialogHandlers.PrintDialogHandler(WatiN.Core.DialogHandlers.PrintDialogHandler.ButtonsEnum.Cancel);
using (new WatiN.Core.DialogHandlers.UseDialogOnce(iePopup.DialogWatcher, pdhPopup)) //This will use the DialogHandler once and then remove it from the DialogWatcher
{
    //At this point the popup window will be open, and the print dialog will be canceled
    //Use the iePopup object to manage the new window in here.
}

iePopup.Close(); // Close the popup once we are done.
Greg Bray
A: 

You still might want to check your browser AutoClose property ie.AutoClose

Shady M. Najib
A: 

This worked for me......

private void Print_N_Email(Browser ie) { //Print and handle dialog. ie.Div(Find.ById("ContentMenuLeft")).Link(Find.ByText(new Regex("Print.*"))).Click();//orig Browser ie2 = Browser.AttachTo(typeof(IE), Find.ByUrl(new Regex(".Print."))); System.Threading.Thread.Sleep(1000);

PrintDialogHandler pdh = new PrintDialogHandler(PrintDialogHandler.ButtonsEnum.Cancel); new UseDialogOnce(ie2.DialogWatcher, pdh); ie2.Close(); }

AutoTester