WatiN:打印对话框

时间:2009-07-14 15:51:18

标签: asp.net-mvc unit-testing watin

我有一个使用javascript打印对话框时会弹出的屏幕。

我刚刚开始使用WatiN来测试我的应用程序。此屏幕是测试的最后一步。

有时WatiN会在对话框出现之前关闭IE,有时它不会,窗口会挂起。我在测试TearDown中有ie.Close()但是如果显示打印对话框,它仍会保持打开状态。

我想避免的是拥有孤立的IE窗口。我希望它一直关闭。

我查了一下DialogHandlers并写了这个:

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

将其放在链接到页面的按钮点击之前,但没有任何改变。

我看到的示例中的代码类似于:

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

但PrintDialogHandler没有太多成员。

我最初并没有尝试测试此对话框出现(只是页面加载并检查页面上的某些值)但我想等待并测试打印对话框的存在会更完整。

3 个答案:

答案 0 :(得分:2)

不完全确定您的情况,但是我们遇到了一个弹出窗口的问题,该窗口在加载时也显示了一个打印对话框。我们的主要问题是我们忘记创建一个新的IE实例并将其附加到弹出窗口。这是工作代码:

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.

答案 1 :(得分:1)

这对我有用:

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(); 
}

答案 2 :(得分:0)

您仍可能需要查看浏览器AutoClose属性ie.AutoClose