Download file to given absolute path in Firefox using Protractor

1.1k Views Asked by At

Im using Protractor for E2E testing. During automation, I need to download files to C:\Automation folder in my system. But below code is not working.

Note:During automation execution,The Save as popup opens(but i have to disable that in future) and I manually click "Save" option. It saves in default location ie Downloads folder.How do I make it save in my given path.

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 () {...}
}
2

There are 2 best solutions below

0
On

It looks like you may just be missing a couple of preferences for it to work with firefox. Try adding these and see if that helps.

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 */ );
0
On

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);
            });
        });
    }
};