2

I am using Selenium Webdriver with C# for creating Automation test cases and have encountered an issue i.e. not able to automate the file download windows pop-up. On doing search over web, I found that this is a normal issue and there is a way to handle this with Selenium (not only 1 way but couple of ways), I have tried the following solutions but nothing is working.

  1. Setting preferences of Firefox to automatically save files. I tried this but the Firefox instance which is launched by me (manually) is different from the instance launched by Selenium, so any setting done by me is not getting reflected to the Automated script. In addition to it, I don't want to go with this solution (although I tried).

  2. Using solution combined from this link and SO Question1, SO Question2, in addition to it I explored from questions over SO and SQA related to this and; I have implemented something like this

    FirefoxProfile prof = new FirefoxProfile();    
    FirefoxDriver driver = new FirefoxDriver(prof);    
    driver.Navigate().GoToUrl("http://test.com/");    
    driver.Manage().Window.Maximize();    
    String title = driver.Title;
    driver.FindElement(By.Id("name-1")).Click();    
    driver.FindElement(By.Id("address-1")).Click();    
    prof.SetPreference("browser.download.dir", "C:\\Dhiman\\");
    prof.SetPreference("browser.download.folderList", 2);    
    //prof.SetPreference("browser.download.manager.showWhenStarting", false);    
    prof.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip"); 
    driver.FindElement(By.XPath("html/body/div[1]/div[3]/div[2]/div/div/form/fieldset/div[8]/a")).Click();
    

But this too is not working, as using this code "A new instance of Firefox is launched every time it is executed and that instance doesn't contain any download preference for 'Compressed Zipped Folder', even if I save it in the current instance, it is not shown when this script is executed again."

This solution I think can work for me, but here the Firefox is causing issues.

  1. Lastly, I found that I can use AutoIT for this, but that is a separate tool and I will need an approval from the Client and their IT department for downloading, installing and running that tool (a long long process with lots of approvals) as I am working on client provided machine.

How this issue of downloading files can be resolved without using AutoIT? IS there a way to resolve this Firefox issue permanently?

If any more information is required, just out the same query/info. needed in comments section of this question and will add that information too.

Update:-

Please read the full question, as the problem is not only related to downloading, and I have already gone through SO links, some of them are referenced in questions itself.

2
  • Possible duplicate of How to download a file using Selenium's WebDriver?
    – kirbycope
    Commented Oct 23, 2015 at 20:04
  • I have already mentioned that I have gone through many related links (3 of them are mentioned in my question itself). But these didn't solve my issue. Moreover, the solution mentioned in provided link has also been used, but still not working.
    – Dhiman
    Commented Oct 23, 2015 at 20:30

2 Answers 2

1

If you take a step back and think Why you are performing this test, then it is likley to prove the following;

  1. The URL is to the correct file
  2. A file is present at that location

If the file download locations are static, maybe it is enough to have an assert that checks the URL endpoint is what is expected.

If you want some additional peace of mind the file hasn't moved, do an AssertFalse on a 404 being returned if you do click the link.

Again, this avoids you having to handle the download itself but answers the questions you were asking in the test.

I tend to avoid actually downloading files using automated suites as it will inevitably lead to deleting the files later to avoid the HDD filing up.

1
  • Agreed. Once you download the file it becomes a whole other ballgame to do verifications on it. "Selenium automates browsers. That's it!" - SeleniumHQ
    – kirbycope
    Commented Jan 18, 2017 at 18:16
-1

Try the below code to download a file using Selenium C# with autoit

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenQA.Selenium.Firefox; using OpenQA.Selenium; using OpenQA.Selenium.Support; using OpenQA.Selenium.Interactions; using NUnit.Framework; using OpenQA.Selenium.IE; using System.Threading; using OpenQA.Selenium.Support.UI; using AutoItX3Lib;

namespace windowControlHandler {

class ExportFeasibility
{   
    public static String downloadPath = "C:\\SeleniumDownloads";
    static void Main(string[] args)
    {
        //-----------------------------------------------------------------------------------------------------------------------
        IWebDriver driver = new FirefoxDriver(new FirefoxBinary("C:\\Program Files (x86)\\Mozilla Firefox ESR v38.7.0\\firefox.exe"), new FirefoxProfile(), TimeSpan.FromMinutes(10));
        //-----------------------------------------------------------------------------------------------------------------------
        driver.Navigate().GoToUrl("URL");
        driver.Manage().Window.Maximize();
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30000));
        Thread.Sleep(3000);
        IWebElement export = driver.FindElement(By.XPath("html/body/ui-view[2]/qa-planning-multi-grid/div[2]/div[2]/button[1]"));
        Actions saction = new Actions(driver);
        saction.Click(export).Perform();
        AutoItX3 autoit = new AutoItX3();
        autoit.WinActivate("Opening GPO Plan.xlsx");
        Thread.Sleep(3000);
        autoit.Send("{ALTDOWN}s{ALTUP}");    
        autoit.Send("{Enter}");
        Thread.Sleep(3000);
        autoit.WinActivate("Enter name of file to save to..");
        autoit.Send("{Enter}");

    }

}

}

1
  • 1
    The OP is asking for a solution that does NOT use AutoIT
    – Kate Paulk
    Commented Jun 22, 2016 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.