downloading a file with Watin
How can i download a file with watin? I searched a lot and tryied but i cant get it. I only want to click on a link that have a download and save it. I used examples that i found but without success. The problem is that i use "WaitUntilFileDownloadDialogIsHandled(15)" but the 15 seconds pass and throw and exception: WatiN.Core.Exceptions.WatiNException : Has not shown dialog after 15 seconds.
This is the code:
FileDownloadHandler download = new FileDownloadHandler("C:/Development/Test/Downloads/" + "excel" + ".xls");
using (new UseDialogOnce(browser.DialogWatcher, downloa开发者_高级运维d))
{
browser.Button(Find.ById("id_of_the_button")).ClickNoWait();
download.WaitUntilFileDownloadDialogIsHandled(15);
download.WaitUntilDownloadCompleted(150);
browser.RemoveDialogHandler(download);
}
Please, help!
I did discover one issue that plagued me
FileDownloadHandler handler = new FileDownloadHandler(@"c:\temp\file.csv");
browser.DialogWatcher.CloseUnhandledDialogs = false;
using (new UseDialogOnce(browser.DialogWatcher, handler))
{
browser.Link(Find.ByText("July2011")).Click();
handler.WaitUntilFileDownloadDialogIsHandled(15);
handler.WaitUntilDownloadCompleted(240);
}
Note the inclusion of the "CloseUnhandledDialogs = false" line. My save dialog popup was disappearing immediately and it took me forever to realize what was going on.
I had used the following code snippet that worked absolutely fine
FileDownloadHandler download = new FileDownloadHandler("C:\\Development\\Test\\Downloads\\" + "excel" + ".xls");
browser.AddDialogHandler(download);
browser.Button(Find.ById("id_of_the_button")).ClickNoWait();
download.WaitUntilFileDownloadDialogIsHandled(15);
download.WaitUntilDownloadCompleted(150);
browser.RemoveDialogHandler(download);
The only difference in the code snippet that I used was that I used a backslash with the escape character and that I added and removed the DialogHandler without an using block.
~Ashish Narmen
To Download Documents using WATIN
public FileDownloadHandler fileDownloadHandler;
/*CLICK ON THE FILE LINK TO DOWNLOAD..IT WILL PROMPT FOR FILE DOWNLOAD DIALOG..TO HANDLE THAT DIALOG USE THE BELOW CODE*/
fileDownloadHandler = new FileDownloadHandler(//THE PATH IN WHICH YOU DOWNLOAD DOCUMENTS//);
try
{
using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler))
{
download();
}
}
catch (WatiN.Core.Exceptions.WatiNException ex)
{
download();
}
public void download()
{
try
{
fileDownloadHandler.WaitUntilDownloadCompleted(8);
}
catch (WatiN.Core.Exceptions.WatiNException ex)
{
download();
}
}
精彩评论