0

I would like to know if there is any way for the automated tests to download the file (excel and pdf in my case) and save in a desired location using selenium web driver. I tried using Firefox profile, but that didn't work. When the test is running, there is window pop-up with asking whether to open or save the file. So, when we click a button, I do not want the windows pop-up to display, instead automatically allow it to download in a desired location( both locally and on Selenium Server) We are using C# to write the tests. Attached is the windows pop-up. Could someone please help with this?

enter image description here

public static IWebDriver Build(SeleniumInstanceContext context)
    {
        IWebDriver instance;            
        var capabilities = new DesiredCapabilities();
        var profile = CreateFirefoxProfile();

        //Pass the Firefox profile to be used by RemoteWebDriver
        capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());

        switch (context.TestingBrowser.ToUpperInvariant())
        {
            case "CHROME":
                instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
                    ? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), DesiredCapabilities.Chrome())
                    : new ChromeDriver();
                break;
            case "IE":
                instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
                    ? new RemoteWebDriver(new Uri(context.SeleniumServerUrl),
                        DesiredCapabilities.InternetExplorer())
                    : new InternetExplorerDriver();
                break;
            default:
                instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
                    ? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), capabilities,
                        TimeSpan.FromMinutes(5))
                    : new FirefoxDriver(profile);
                break;
        }

        return instance;
    }

[![enter image description here][1]][1]public static FirefoxProfile CreateFirefoxProfile()
    {
        //Create FireFox Profile object
        var profile = new FirefoxProfile();

        //Set Location to store files after downloading.
        const string path = "C:\\Users\\abc.xyz\\Downloads";
        profile.SetPreference("browser.download.dir", path);
        profile.SetPreference("browser.download.folderList", 2);

        //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
        profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/csv");
        //profile.SetPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");

        //profile.SetPreference("browser.download.manager.showWhenStarting", false);
        profile.SetPreference("pdfjs.disabled", true);

        profile.SetPreference("browser.download.alertOnEXEOpen", false);
        profile.SetPreference("browser.download.manager.focusWhenStarting", false);
        profile.SetPreference("browser.helperApps.alwaysAsk.force", true);
        profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.SetPreference("browser.download.manager.closeWhenDone", false);
        profile.SetPreference("browser.download.manager.showAlertOnComplete", false);
        profile.SetPreference("browser.download.manager.useWindow", false);

        return profile;
    }
ranp
  • 57
  • 2
  • 12
  • Have you tried checking "Do this automatically..." and saving it as a Firefox profile? Also make sure you do not modify temporary profile created during Selenium test session! – Kirhgoph Jul 11 '17 at 16:00
  • How do I handle "Do this automatically.." since it's an automated test and it's a windows pop-up not the browser? – ranp Jul 11 '17 at 16:46

2 Answers2

0
  1. Load the Firefox profile you use for testing, try to download any .xls file - you'll see this pop-up.
  2. Tick the checkbox and download file like you want it to be downloaded in your autotests.

Next time you open Firefox with this profile in autotests these xls files will be downloaded without any pop-up window

Kirhgoph
  • 405
  • 5
  • 10
0

It appears that you've forgotten to include some MIME types in the browser.helperApps.neverAsk.saveToDisk preference.

Here are the MIME types for .xls, .xlsx, and .pdf file extensions:

  • .xls - application/excel
  • .xls - application/vnd.ms-excel
  • .xls - application/x-excel
  • .xls - application/x-msexcel
  • .xlsx - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • .pdf - application/pdf

References:


To hide this pop up for .xls, .xlsx, and .pdf file extensions, add the MIME types, separated by a comma:

string[] mimeTypes = new string[]
{
    // .xls
    "application/excel",
    "application/vnd.ms-excel",
    "application/x-excel",
    "application/x-msexcel",

    // .xlsx
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",

    // .pdf
    "application/pdf"
};

FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
    string.Join(",", mimeTypes));
budi
  • 6,351
  • 10
  • 55
  • 80
  • Thank you. Adding this mime type worked in my local. However, if I run the same test in remote web driver using the selnium hub-node, it is not downloading the file. I have created the folder structure in my remote machine in order to download the file. So, any idea to make it work on remote ? – ranp Jul 21 '17 at 17:21
  • Hmm... unfortunately I'm not sure. I would imagine that it would work the same, did you confirm the folder structure exactly matches your local machine? – budi Jul 21 '17 at 17:34
  • yes I have verified it in the console output which returns the same folder structure. – ranp Jul 21 '17 at 18:14
  • Not sure then, I haven't worked much with remote web driver, sorry about that. – budi Jul 21 '17 at 18:24