使用量角器在 Firefox 上下载文件

我需要使用量角器在 Firefox 上下载一个 zip 文件.单击下载链接时,会弹出要求打开/保存文件的 Windows 对话框.那么我该如何处理.我需要将哪些参数传递给驱动程序?使用 chrome 我可以做到这一点下载: {'prompt_for_download':假},

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 },

但是我应该用 Firefox 做什么.

but what should i do with firefox.

推荐答案

问题是 - 您无法通过量角器/硒操作另存为..."对话框.您应该首先避免打开它,让 firefox 自动下载指定 mime 类型的文件 - 在您的情况下为 application/zip.

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.

换句话说,您需要使用自定义 Firefox 来启动 Firefox配置文件设置适当的偏好:

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"]
            )
        ]);
    },

    // ...
}

这里我们基本上是在说:Firefox,请自动下载 zip 文件,而不是询问 /path/to/save/downloads 目录.

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

相关文章