vsathyak
vsathyak

Reputation: 73

Protractor e2e test case for download file work fine on chrome but not with firefox and vice versa?

I have a scenario where I need to click on a link which will trigger .CSV file download to a default location(/tmp)and it is working fine on both chrome and firefox browsers but based on multiCapabilities configuration in conf.js, at times it work only on single browser(means one set of configuration helps in chrome working fine but not the firefox and the other set results in firefox to work but not the chrome).And I used the following stackoverflow post as the reference : Protractor e2e test case for downloading pdf file. And my attempt worked fine someway but then script hits only on chrome or firefox based on multiCapabilities configuration I used.

Please note that chrome will work with following config and in this I haven't added the firefox profile setup . So file download part in firefox wont work with the following configuration.

multiCapabilities: [
    {
        'browserName': 'chrome',
        'platform': 'ANY',
        'chromeOptions': {
            args: ['--no-sandbox', '--test-type=browser'],
            prefs: {
                download: {
                    'prompt_for_download': false,
                    'directory_upgrade': true,
                    'default_directory': '/tmp'
                }
            }
        }
    },
    {
        'browserName': 'firefox',
    }
],

Based on the above mentioned url(Protractor e2e test case for downloading pdf file) I added the function getFirefoxProfile() in my util file : common.js

var getFirefoxProfile = function() {
    var deferred = q.defer();
    var firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("browser.download.folderList", 2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
    firefoxProfile.setPreference("browser.download.dir", '/tmp');
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext");
    firefoxProfile.encoded(function(encodedProfile) {
        var multiCapabilities = [{
            browserName: 'firefox',
            firefox_profile : encodedProfile
        }];
        deferred.resolve(multiCapabilities);
    });
    return deferred.promise;
}
exports.getFirefoxProfile = getFirefoxProfile;

And then I updated the conf.js as follows:

getMultiCapabilities: com.getFirefoxProfile,
multiCapabilities: [
    {
        'browserName': 'chrome',
        'platform': 'ANY',
        'chromeOptions': {
            args: ['--no-sandbox', '--test-type=browser'],
            prefs: {
                download: {
                    'prompt_for_download': false,
                    'directory_upgrade': true,
                    'default_directory': '/tmp'
                }
            }
        }
    },
    {
        'browserName': 'firefox',
    }
],

And getMultiCapabilities: com.getFirefoxProfile when used in conf.js will override both capabilities and multiCapabilities mentioned in the conf.js and when I run my script, it executes the script only on firefox and not in chrome. Any idea on how to fix this? And my requirement is to login to chrome, perform the csv download, logout from chrome, then login to firefox and do the same.

Any help would be greatly appreciated..

Upvotes: 2

Views: 1456

Answers (1)

vsathyak
vsathyak

Reputation: 73

For adding capabilities to multiple browser(chrome and firefox), we need to use multiCapabilities, and add capabilities of each browser(firefox and chrome) as follows.

Note : Here I configured multi capabilities with promises.

var q = require("q");
var FirefoxProfile = require("firefox-profile");

exports.config = {
    directConnect: true,

    onPrepare: function () {
        browser.driver.getCapabilities().then(function(caps){
            browser.browserName = caps.get('browserName');
    });
},

maxSessions: 1,

getPageTimeout: 150000,

allScriptsTimeout: 150000,

params: require('../testdata/data.json'),

framework: 'jasmine2',

specs: ['../unit_test/*_spec.js'],

jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 150000
},

//seleniumAddress: "http://127.0.0.1:4444/wd/hub",

getMultiCapabilities: function() {
    var deferred = q.defer();

    var multiCapabilities = [
        {
            'browserName': 'chrome',
            'platform': 'ANY',
            'chromeOptions': {
                args: ['--no-sandbox', '--test-type=browser'],
                prefs: {
                    download: {
                        'prompt_for_download': false,
                        'directory_upgrade': true,
                        'default_directory': '/tmp'
                    }
                }
            }
        },
    ];

    // Wait for a server to be ready or get capabilities asynchronously.
    setTimeout(function() {
        var firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("javascript.enabled", false);
        firefoxProfile.setPreference("browser.download.folderList", 2);
        firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
        firefoxProfile.setPreference("browser.download.dir", '/tmp');
        firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext");
        firefoxProfile.encoded(function (encodedProfile) {
            var capabilities = {
                "browserName": "firefox",
                "firefox_profile": encodedProfile
            };
            multiCapabilities.push(capabilities);
            deferred.resolve(multiCapabilities);
        });
    }, 1000);
    return deferred.promise;
    }
};

The default download location is set as /tmp for both browsers and for firefox to set capabilities, we need create a firefox profile and set preference.

Note : "browser.download.folderList", 2 ==> set the download location as user defined. passing value 0 set download to desktop and 1 will download to default download location.

Also "browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext" ==> mime type used is csv mime type.

If your download file is pdf or some other, replace the csv mime type with your files mime type.

Upvotes: 1

Related Questions