WatiN——Web自动化测试(三)【弹出窗口处理】
public static void CaptureAlertDialog(this Browser browser, Action<AlertDialogHandler> operation, int waitTimeInSeconds)
{
??var handler = new AlertDialogHandler();
??using (new UseDialogOnce(browser.DialogWatcher, handler))
??{
????operation(handler);
????handler.WaitUntilExists(waitTimeInSeconds);
????if (handler.Exists())
??????handler.OKButton.Click();
??}
}
context.Browser.CaptureAlertDialog((AlertDialogHandler handler) => { btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout); }, 5);
btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout); 為button的點擊事件。
2、Confirm Dialog public static void CaptureConfirmDialog(this Browser browser, Action<ConfirmDialogHandler> operation, int waitTimeInSeconds)
{
??var handler = new ConfirmDialogHandler();
??using (new UseDialogOnce(browser.DialogWatcher, handler))
??{
????operation(handler);
????handler.WaitUntilExists(waitTimeInSeconds);
????if (handler.Exists())
????{
??????handler.OKButton.Click();//確認按鈕 handler.CancelButton.Click();取消按鈕
????}
??}
}
context.Browser.CaptureConfirmDialog((ConfirmDialogHandler handler) =>
{
btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout);
}, 5);
btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout);為Button點擊事件
var btn = "獲取button按鈕";
var fileName = System.Windows.Forms.Application.StartupPath + "保存路徑文件名";
FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(fileName);
using (new UseDialogOnce(context.Browser.DialogWatcher, fileDownloadHandler))
{
??btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout);
??context.Browser.WaitUntil(5);
??fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(60);
??fileDownloadHandler.WaitUntilDownloadCompleted(200);
}
首先需要將主瀏覽器對象進行保存,讓這個browser對象再打開新窗口,從中獲取窗口頁面的URL
如下代碼:
var orginBrowser = context.Browser;//context.Browser瀏覽器對象
try
{
??Div.Button(btn => btn.ClassName == "ButtonStyle").WaitUntilExistsAndClick(context.TestConfig.Timeout);//Button按鈕,進行onclick事件
??context.Browser.WaitUntil(3);//等待3秒
??context.Browser = WatiN.Core.Browser.AttachTo(context.Browser.GetType(), Find.ByUrl(url => url.IndexOf("頁面名稱") > -1), context.TestConfig.Timeout);//查找新窗口的頁面名稱
??context.Browser.Refresh();//進行刷新
??///
??///對窗口中的內容進行操作
??///
??context.Browser.Close();
}
catch { }
?context.Browser = orginBrowser;
轉載于:https://www.cnblogs.com/stonespawn/archive/2011/04/08/2009341.html
總結
以上是生活随笔為你收集整理的WatiN——Web自动化测试(三)【弹出窗口处理】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Firefox XUL
- 下一篇: win32中GBK、UTF8的互转