Download file on Firefox with protractor

前端 未结 1 735
时光取名叫无心
时光取名叫无心 2021-01-13 09:12

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

相关标签:
1条回答
  • 2021-01-13 09:41

    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.

    0 讨论(0)
提交回复
热议问题