我正在使用 WatiN 测试一个网站。 在其中一个页面上,我在 Iframe 中获得了一个“报告”,在这个 I 框架中有一个下载和保存报告的链接。但是由于获得链接的唯一方法是使用 frame.Link(...) 弹出窗口在打开后立即关闭;下面的代码片段
//点击创建图表按钮
ie.Button(Find.ById("ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_btnCreateGraph")).Click();
//Lets export the data
ie.Div(Find.ById("colorbox"));
ie.Div(Find.ById("cboxContent"));
ie.Div(Find.ById("cboxLoadedContent"));
Thread.Sleep(1000);//Used to cover performance issues
Frame frame = ie.Frame(Find.ByName(frameNameRegex));
for (int Count = 0; Count < 10000000; Count++) {double nothing = (Count/12); }//Do nothing I just need a short pause
//SelectList waits for a postback which does not occur.
try
{
frame.SelectList(Find.ById("rvReport_ctl01_ctl05_ctl00")).SelectByValue("Excel");
}
catch (Exception)
{
//Do nothing
}
//Now click export
frame.Link(Find.ById("rvReport_ctl01_ctl05_ctl01")).ClickNoWait();
IE ieNewBrowserWindow = IE.AttachTo<IE>(Find.ByUrl(urlRegex));
fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(150);
fileDownloadHandler.WaitUntilDownloadCompleted(200);
我曾尝试使用 ie 而不是 frame,这就是为什么所有这些 ie.Div 都存在的原因。 如果我使用框架,弹出窗口会立即打开和关闭。 如果我使用 ie,我会收到一个链接未找到的错误。 如果我手动单击链接,而测试正在“尝试查找链接”,文件将正确下载。 我已更改代码以使用没有框架的不同页面,但我仍然遇到相同的问题,即下载弹出窗口立即关闭。
[STA线程]
public void TestForMeterDataExport()
{
// Open a new Internet Explorer window and
// goto the website.
IE ie = new IE("https://<URL>", true);
FileDownloadHandler fileDownloadHandler = new FileDownloadHandler("C:\\Documents and Settings\\karnold\\Desktop\\MeterUsageReport_Large.xls");
Regex urlRegex = new Regex("<URL>\\?Mode=true&ReportID=[a-z A-Z 0-9]{30,33}&ControlID=[a-z A-Z 0-9]{30,33}&Culture=1033&UICulture=1033&ReportStack=1&OpType=Export&FileName=BuildingMeterDataReport&ContentDisposition=OnlyHtmlInline&Format=Excel");
//Find the Username text field and input the user ID
ie.TextField(Find.ByName("ctl00$ContentPlaceHolder1$txtUsername")).TypeText("<Name>");
//Find the Password text field and input the password
ie.TextField(Find.ByName("ctl00$ContentPlaceHolder1$txtPassword")).TypeText("PASS");
//Go ahead and login
ie.Button(Find.ByName("ctl00$ContentPlaceHolder1$butLogin")).Click();
//Let's use the Reports Tab
ie.Link(Find.ByUrl("https://<URL>")).Click();
// Let's get the meter data
ie.Link(Find.ByUrl("https://<URL>")).Click();
//Let's choose University of
ie.SelectList(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_ucFacility_ddlFacility")).SelectByValue("5041");
//Set the date range for which we want to get data
ie.TextField(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_DateRangePicker1_dpBeginDate_TextBox")).TypeText("12/09/10");
ie.TextField(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_DateRangePicker1_dpEndDate_TextBox")).TypeText("12/10/10");
//Click the create report button
ie.Button(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_btnSubmit")).ClickNoWait();
//Lets export the data
Thread.Sleep(2000);
//SelectList waits for a postback which does not occur.
try
{
ie.SelectList(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_ContentAreaContentPlaceHolder_ContentAreaContentPlaceHolder_rvMain_ctl01_ctl05_ctl00")).SelectByValue("Excel");
}
catch (Exception)
{
ie.SelectList(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_ContentAreaContentPlaceHolder_ContentAreaContentPlaceHolder_rvMain_ctl01_ctl05_ctl00")).FireEventNoWait("onchange");
//fire the postback event
}
//Now click export
ie.Link(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_ContentAreaContentPlaceHolder_ContentAreaContentPlaceHolder_rvMain_ctl01_ctl05_ctl01")).ClickNoWait();
IE ieNewBrowserWindow = IE.AttachTo<IE>(Find.ByUrl(urlRegex));
fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(10);
fileDownloadHandler.WaitUntilDownloadCompleted(20);
}// close TestForMeterDataExport()
希望有人能告诉我我做错了什么。谢谢
这是我在程序找不到句柄时遇到的错误,也许它会有所帮助 测试用例“M:WebTest.CommandLine.WatiNConsoleWebAndDB.TestForMeterDataExport” 失败:调用 COM 组件返回错误 HRESULT E_FAIL。 System.Runtime.InteropServices.COMException (0x80004005):调用 COM 组件返回错误 HRESULT E_FAIL。 在 SHDocVw.IWebBrowser2.get_Document() 在 WatiN.Core.Native.InternetExplorer.IEBrowser.get_NativeDocument() 在 WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForCompleteOrTimeout() 在 WatiN.Core.WaitForCompleteBase.DoWait() 在 WatiN.Core.DomContainer.WaitForComplete(IWait waitForComplete) 在 WatiN.Core.Native.InternetExplorer.AttachToIeHelper.FinishInitializationAndWaitForComplete(即 SimpleTimer 计时器, bool waitForComplete) 在 WatiN.Core.Native.InternetExplorer.AttachToIeHelper.Find(约束 findBy,Int32 超时, bool waitForComplete) 在 WatiN.Core.Browser.AttachTo(类型 browserType,Constraint 约束,Int32 超时) 在 WatiN.Core.Browser.AttachTo(类型 browserType,Constraint 约束) 在 WatiN.Core.Browser.AttachTo[T](Constraint 约束) Web+DB_test_app.cs(139,0): 在WebTest.CommandLine.WatiNConsoleWebAndDB.TestForMeterDataExport()
最佳答案
感谢 Baptiste 的指点。
//设置句柄和文件另存为名称
FileDownloadHandler handler = new FileDownloadHandler("MeterUsageReport_Large_Iframe.xls");
// add a watcher to look for the save file local
ie.AddDialogHandler(handler);
//Do not close dialog boxes immediately
ie.DialogWatcher.CloseUnhandledDialogs = false;
//create a single use instance that will be easily cleaned up and avoid having windows open after we are done with them
using (new UseDialogOnce(ie.DialogWatcher, handler))
{
//Now click export
frame.Link(Find.ById("rvReport_ctl01_ctl05_ctl01")).ClickNoWait();
//Grab hold of the poup dialog and download the file
handler.WaitUntilFileDownloadDialogIsHandled(30);
handler.WaitUntilDownloadCompleted(35);
}
现在为了好玩,这只有在所有工作都在本地主机上完成时才有效。 如果你需要访问一个非本地的服务器并且你想很好地使用 IE 那么 2) 您需要编辑“受信任站点”的安全性以允许编写下载和 Iframe 脚本。 * a) 打开IE * b) 工具 -> Internet 选项 * c) 安全选项卡。 * d) 确保突出显示“受信任的站点” * e) 单击自定义级别,确保所有 .Net 和 .Net 依赖组件都设置为启用。 * f) 启用或提示所有 activeX 组件 * g) 启用所有下载 * h) 启用 .Net 框架设置 * i) 启用 Web 浏览器控件的脚本 * j) 启用允许网站打开没有地址栏或状态栏的窗口。 * k) 启用从 Iframe 启动程序或文件。 * l) 脚本下的所有内容都设置为启用或提示。 * 如您所知,localhost 被视为“低”安全区域,因此在 localhost 上运行的测试不需要这些更改。 * 引用http://support.microsoft.com/kb/174360
关于testing - 如何捕获从框架打开的弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4683501/