【问题标题】:Download file on Firefox with protractor使用量角器在 Firefox 上下载文件
【发布时间】:2015-07-20 20:50:53
【问题描述】:

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

但是我应该用 Firefox 做什么。

【问题讨论】:

    标签: javascript firefox selenium-webdriver jasmine protractor


    【解决方案1】:

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

    换句话说,您需要使用自定义Firefox Profile 设置appropriate preferences 来启动Firefox:

    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 目录。

    【讨论】:

    • @alecxe 我们如何验证文件是否已下载?
    • @Nick 当然,等待下载,这是工作示例:stackoverflow.com/questions/41082777/…
    • @alecxe 文件默认下载到我配置为指向相对路径的“下载”,但仍然下载到默认位置,非常感谢您的帮助... makeFirefoxProfile ( { "browser.download. folderList": 2, "browser.download.dir": "../e2eConf/conf_Files/", "browser.helperApps.neverAsk.saveToDisk": "application/x-binary" })
    • @alecxe,难道不能使用像 executeScript 和 window.accept() 这样的东西来与“另存为:”交互吗
    • 如何告诉 Internet Explorer 不要问“你想保存...”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 2018-03-07
    相关资源
    最近更新 更多