0

我使用量角器进行 E2E 测试。在自动化过程中,我需要将文件下载到系统中的 C:\Automation 文件夹中。但是下面的代码不起作用。

注意:在自动化执行期间,“另存为”弹出窗口打开(但我以后必须禁用它),然后我手动单击“保存”选项。它保存在默认位置,即下载文件夹。我如何让它保存在我给定的路径中。

let profile = require('firefox-profile');        
let firefoxProfile = new profile();

//_browser = 'chrome';
_browser = 'firefox';
// _browser = 'internet explorer';

firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference('browser.download.dir', "C:\\Automation");

exports.config = {
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
    'browserName': _browser,
    'shardTestFiles': false,
    'maxInstances': 1,
    'acceptInsecureCerts': true,
    'moz:firefoxOptions': {
    'profile': firefoxProfile
    }},
beforeLaunch: function () {...}
}
4

2 回答 2

0

看起来您可能只是缺少一些与 Firefox 一起使用的偏好。尝试添加这些,看看是否有帮助。

profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "browser.helperApps.neverAsk.saveToDisk", 
  /* A comma-separated list of MIME types to save to disk without asking goes here */ );
于 2017-12-15T21:39:36.737 回答
0

this will save to downloads folder inside your project. You can try to tweak it to save to desired folder. You have to specify which types of files are suppose to be downloaded without prompt. JSON and csv are already there.

var q = require('q');
var path = require('path');
var sh = require("shelljs");
var cwd = sh.pwd().toString();

var FirefoxProfile = require('selenium-webdriver/firefox').Profile;

var makeFirefoxProfile = function(preferenceMap) {
    var profile = new FirefoxProfile();
    for (var key in preferenceMap) {
        profile.setPreference(key, preferenceMap[key]);
    }
    return q.resolve({
        browserName: 'firefox',
        marionette: true,
        firefox_profile: profile
    });
};

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    framework: 'jasmine2',
    getMultiCapabilities: function() {
        return q.all([
            makeFirefoxProfile(
                {
                    'browser.download.folderList': 2,
                    'browser.download.dir': (path.join(cwd, 'downloads')).toString(),
                    'browser.download.manager.showWhenStarting': false,
                    'browser.helperApps.alwaysAsk.force': false,
                    'browser.download.manager.useWindow': false,
                    'browser.helperApps.neverAsk.saveToDisk': 'application/octet-stream, application/json, text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext, text/plaintext'
                }
            )
        ]);
    },
    allScriptsTimeout: 1000000,
    specs: ['./tmp/**/*.spec.js'],

    jasmineNodeOpts: {
        defaultTimeoutInterval: 1000000,
        showColors: true
    },
    onPrepare: function() {
        browser.driver.getCapabilities().then(function(caps) {
            browser.browserName = caps.get('browserName');
        });

        setTimeout(function() {
            browser.driver.executeScript(function() {
                return {
                    width: window.screen.availWidth,
                    height: window.screen.availHeight
                };
            }).then(function(result) {
                browser.driver.manage().window().setPosition(0,0);
                browser.driver.manage().window().setSize(result.width, result.height);
            });
        });
    }
};
于 2017-12-15T21:44:03.320 回答