This is a migrated thread and some comments may be shown as answers.

Does the HTMLAnchor.Download() method work for Safari and Chrome

4 Answers 53 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daniel
Top achievements
Rank 1
Daniel asked on 06 Jan 2012, 10:00 PM
I am using the following code to download a file from a link.  it works fine in IE 8 and Firefox but I am not able to finish setting the paths/download process in Safari and Chrome.  Should this be supported for all 4 browsers or is there some other method I need to use for Safari and Chrome

if (allChildlinks != null)
{
    HtmlAnchor anchor = allChildlinks[0];
    combinedFileName = downloadPath + anchor.InnerText;
    anchor.Download(false, DownloadOption.Save, combinedFileName, 200000);
}

4 Answers, 1 is accepted

Sort by
0
Anthony
Telerik team
answered on 09 Jan 2012, 10:57 PM
Hello Daniel,

Please try using our DownloadsDialogHandler as shown at the bottom of that page instead. It handles the download dialogs across all browsers.

Regards,
Anthony
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Daniel
Top achievements
Rank 1
answered on 10 Jan 2012, 08:00 PM
I have tried the suggested approach.  I am still having a problem with chrome.  In chrome we get to the point where the Save As dialogue is opened, but the path does not get set.  The chrome window is then closed leaving the dialogue opened.  
Note I am not using the default file download path.  To get this to work in safari I had to set the download path in the browser settings to the directory that I wanted to use.

This is the full code of the step:
[CodedStep(@"Find and Download File")]
        public void DownloadDoc_CodedStep()
        {
            string combinedFileName = "";
            string downloadPath = @"C:\temp\SmokeTestDownloads\";
            HtmlDiv sectionDiv = Pages.Vault.Find.ById<HtmlDiv>("section");
            sectionDiv.Refresh();
 
            HtmlDiv contentDiv = sectionDiv.Find.ByAttributes<HtmlDiv>("class=content");
            HtmlTable records = contentDiv.Find.ByAttributes<HtmlTable>("class=records");
 
            while (records == null || records.Rows.Count == 0)
            {
                contentDiv.Refresh();
                records = sectionDiv.Find.ByAttributes<HtmlTable>("class=records");
            }
            records.Refresh();
 
            System.Collections.ObjectModel.ReadOnlyCollection<HtmlAnchor> allChildlinks = records.Find.AllByAttributes<HtmlAnchor>("class=filename");
             
 
            if (allChildlinks != null)
            {
                Desktop desktop = new Desktop();
                HtmlAnchor anchor = allChildlinks[0];
                combinedFileName = downloadPath + anchor.InnerText;
                DownloadDialogsHandler handler = new DownloadDialogsHandler(ActiveBrowser, DialogButton.SAVE, combinedFileName, desktop);
 
                //anchor.Download(false, DownloadOption.Save, combinedFileName, 200000);
 
                anchor.Click();
                handler.WaitUntilHandled(200000);
            }
 
            SetExtractedValue("combinedfileName", combinedFileName);
 
        }





Also on the document page it would be good to note that there needs to be a reference to the ArtOfTest.WebAii.Win32.Dialogs namespace in the test.  Also the desktop object needs to be declared.
0
Accepted
Anthony
Telerik team
answered on 11 Jan 2012, 09:33 PM
Hello Daniel,

You are correct about Safari; when clicking a download link it will automatically save the file to the default path you specify in its settings. So, the code will work in Safari, just not the custom path portion. The file will save to the default location.

The only way to specify a custom save location in Safari is to right click the download link and choose "Download Linked File As...". If you want to specify a custom download path at run-time in Safari, see below for a work-around using a right mouse click and key presses to make that selection and then type in the custom path.

I saw the same behavior you described with Chrome, and I'm currently researching the extent of our support for download dialogs in code for that browser. In the meantime you can employ the same work-around as with Safari:

Manager.Settings.UnexpectedDialogAction = UnexpectedDialogAction.DoNotHandle;
Manager.LaunchNewBrowser(BrowserType.Chrome);
 
HtmlAnchor dlLink = Find.ByContent<HtmlAnchor>("Sample Address File");
dlLink.MouseClick(MouseClickType.RightClick);
 
System.Threading.Thread.Sleep(1000);
 
Manager.Desktop.KeyBoard.KeyPress(Keys.Down);
Manager.Desktop.KeyBoard.KeyPress(Keys.Down);
Manager.Desktop.KeyBoard.KeyPress(Keys.Down);
Manager.Desktop.KeyBoard.KeyPress(Keys.Down);
Manager.Desktop.KeyBoard.KeyPress(Keys.Enter);
 
System.Threading.Thread.Sleep(3000);
Manager.Desktop.KeyBoard.TypeText("C:\\address.xls", 10);
Manager.Desktop.KeyBoard.KeyPress(Keys.Enter);


Greetings,

Anthony
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Daniel
Top achievements
Rank 1
answered on 07 Feb 2012, 01:24 AM
That work around seemed to be ok for Chrome.  One note is that you will need to as a reference to System.Windows.Form for the Keys commands to work correctly.
Tags
General Discussions
Asked by
Daniel
Top achievements
Rank 1
Answers by
Anthony
Telerik team
Daniel
Top achievements
Rank 1
Share this question
or