5

I need to download a zip file on Firefox with protractor. On clicking on download link, Windows dialog asking to Open/Save the file pops up. So How can I handle that. What args do I need to pass to driver? With chrome I can do that with download: { 'prompt_for_download': false },

but what should i do with firefox.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Prashant Kajale
  • 473
  • 4
  • 8

1 Answers1

2

The problem is - you cannot manipulate that "Save As..." dialog via protractor/selenium. You should avoid it being opened in the first place and let firefox automatically download the files of a specified mime-type(s) - in your case application/zip.

In other words, you need to fire up Firefox with a custom Firefox Profile setting the appropriate preferences:

var q = require("q");
var FirefoxProfile = require("firefox-profile");

var makeFirefoxProfile = function(preferenceMap, specs) {
    var deferred = q.defer();
    var firefoxProfile = new FirefoxProfile();

    for (var key in preferenceMap) {
        firefoxProfile.setPreference(key, preferenceMap[key]);
    }

    firefoxProfile.encoded(function (encodedProfile) {
        var capabilities = {
            browserName: "firefox",
            firefox_profile: encodedProfile,
            specs: specs
        };

        deferred.resolve(capabilities);
    });
    return deferred.promise;
};

exports.config = {
    getMultiCapabilities: function() {
        return q.all([
            makeFirefoxProfile(
                {
                    "browser.download.folderList": 2,
                    "browser.download.dir": "/path/to/save/downloads",
                    "browser.helperApps.neverAsk.saveToDisk": "application/zip"
                },
                ["specs/*.spec.js"]
            )
        ]);
    },

    // ...
}

Here we are basically saying: Firefox, please download zip files automatically, without asking into the /path/to/save/downloads directory.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • @alecxe how could we verify weather the file is downloaded? – Nick Jan 06 '17 at 21:26
  • 1
    @Nick sure, wait for it to be downloaded, here is the working sample: http://stackoverflow.com/questions/41082777/protractor-test-download-file-without-knowing-filename. – alecxe Jan 06 '17 at 21:28
  • @alecxe Files are by default downloaded to 'downloads' i configured to point to relative path but still it downloads to default location, help would be greatly appreciated... makeFirefoxProfile ( { "browser.download.folderList": 2, "browser.download.dir": "../e2eConf/conf_Files/", "browser.helperApps.neverAsk.saveToDisk": "application/x-binary" }) – Nick Jan 06 '17 at 23:42
  • @alecxe, is it not possible to use something like an executeScript and an window.accept() to interact with the "Save as:" – Jeremy Kahan Mar 05 '17 at 15:02
  • and how can one tell Internet Explorer not to ask "Do you want to save..." – Jeremy Kahan Mar 05 '17 at 19:55