我一直在尝试更新conf.js文件以下载文件,它应该适用于Firefox和chrome(彼此独立)。
我一直在网上遵循指南和答案,(包括但不限于stackoverflow),但对我来说并不完全有效。 Chrome没问题,Firefox是我的麻烦制造者。
在Firefox上,它根本不下载文件,与chrome下载文件不同。
从我的utils方法setBrowserProfile:
const FirefoxProfile = require('firefox-profile');
const path = require('path');
const Deferred = require('../utils/deferred');
const downloadsPath = path.join(__dirname, '../');
exports.setBrowserProfile = () => {
const defer = new Deferred();
const multiCapabilities = [
{
browserName: 'chrome',
chromeOptions: {
args: ['--no-sandbox'],
prefs: {
download: {
prompt_for_download: false,
directory_upgrade: true,
default_directory: downloadsPath,
},
},
},
},
];
const firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference('javascript.enabled', false);
firefoxProfile.setPreference('browser.download.dir', downloadsPath);
firefoxProfile.setPreference('browser.download.folderList', 2);
firefoxProfile.setPreference('browser.download.useDownloadDir', true);
firefoxProfile.setPreference('browser.download.manager.useWindow', false);
firefoxProfile.setPreference('browser.helperApps.neverAsk.openFile', false);
firefoxProfile.setPreference('browser.download.manager.alertOnEXEOpen', false);
firefoxProfile.setPreference('browser.download.manager.showWhenStarting', false);
firefoxProfile.setPreference('browser.download.manager.focusWhenStarting', false);
firefoxProfile.setPreference('browser.download.manager.showAlertOnComplete', false);
firefoxProfile.setPreference('browser.helperApps.neverAsk.saveToDisk', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
firefoxProfile.encoded((encodedProfile) => {
const capabilities = {
browserName: 'firefox',
firefox_profile: encodedProfile,
};
multiCapabilities.push(capabilities);
defer.resolve(multiCapabilities);
});
return defer.promise;
};
以及conf.js中的相关信息
exports.config = {
[...]
getMultiCapabilities: profile.setBrowserProfile,
[...]
};
从我的deffered.js方法(转换为ES2015):
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = undefined;
function _instanceof(left, right) {
if (right != null && typeof Symbol !== 'undefined' && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
function _classCallCheck(instance, Constructor) {
if (!_instanceof(instance, Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
const Deferred = function Deferred() {
const _this = this;
_classCallCheck(this, Deferred);
this.promise = new Promise((resolve, reject) => {
_this.resolve = resolve;
_this.reject = reject;
});
};
module.exports = Deferred;
Chrome尝试这样:
const multiCapabilities = [
{
browserName: 'chrome',
'goog:chromeOptions': {
w3c: false, // due problems with latest chrome driver
// browser.actions is causing issues: https://github.com/jan-molak/serenity-js/issues/329
prefs: {
download: {
'prompt_for_download': false,
'directory_upgrade': true,
'default_directory': downloadsPath
}
}
}
}
];