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

5 posts, 1 answers
  1. Daniel
    Daniel avatar
    1 posts
    Member since:
    Jul 2011

    Posted 06 Jan 2012 Link to this post

    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);
    }
  2. Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 09 Jan 2012 Link to this post

    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.
  3. Daniel
    Daniel avatar
    1 posts
    Member since:
    Jul 2011

    Posted 10 Jan 2012 Link to this post

    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.
  4. Answer
    Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 11 Jan 2012 Link to this post

    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.
  5. Daniel
    Daniel avatar
    1 posts
    Member since:
    Jul 2011

    Posted 06 Feb 2012 Link to this post

    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.
Back to Top
  Cast Your Vote at DevPro Connections! Cast Your Vote at  Win IT Pro Connections!