2009-07-14 105 views
0

我有一個使用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沒有多少成員。

我最初並沒有試圖測試這個對話框出現(只是頁面加載和檢查頁面上的一些值),但我想這將是更完整的等待和測試存在的打印對話框。

回答

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

你仍然可能要檢查你的瀏覽器AutoClose財產ie.AutoClose

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