3

I want to launch the Firefox browser through selenium-webdriver 3.6.0 with some of the default settings of the browser changed. Specifically, I want Firefox to download files, during automated testing, without prompting whether to save or not and to download to a predefined directory other than the default, which is the downloads folder.

The way to do it on google chrome is this:

if (this.bName === 'chrome') {  
   var cap = Capabilities.chrome();  
   var options = {  
      'prefs': {  
          profile: {  
            default_content_settings: {  
                  popups: 0,  
            },  
          },  
          download: {  
            default_directory: path.join(__dirname,  
 '/../Downloads For Testing'),  
          }  
      }  
 };  
 var cap = cap.set('chromeOptions', options);  
 this.browser = new Builder().withCapabilities(cap).build();  
}

A relevant try on Firefox, by setting the preferences after creating a new profile, didn't work.

I include the Profile from Firefox folder

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

and I build with new capabilities

else if (this.bName === 'firefox') {
            var cap = Capabilities.firefox();
            var profile = new firefoxProfile;
            profile.setPreference("browser.download.folderList", 2);
            profile.setPreference("browser.download.manager.showWhenStarting", false);
            profile.setPreference("browser.download.dir", path.join(__dirname, '/../Downloads For Testing'));
            profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/html");
            cap.set('firefox_profile', profile);
            console.log(profile);
            this.browser = new Builder().withCapabilities(cap).build();
        }

This is the printout of the new profile object:

Profile {
  preferences_:
   { 'browser.download.folderList': 2,
     'browser.download.manager.showWhenStarting': false,
     'browser.download.dir': 'C:\\path\\Downloads For Testing',
     'browser.helperApps.neverAsk.saveToDisk': 'text/html' },
  template_: undefined,
  extensions_: []
}

The browser is initiated with no errors and all promises are returned correctly by mocha, the test framework, until pressing the button to download a file and the normal dialog shows up, so no success.

1
  • Please check the paragraph "Notes for Selenium-webdriver package users" on npmjs.com/package/firefox-profile. Links don't work.. It doesn't say which v of Selenium. Have tried the package without "addExtension" wraparound, just "myProfile.updatePreferences();" before calling encode. Doesn't call anything inside encode func. No errors
    – seek88
    Commented Nov 6, 2018 at 12:31

1 Answer 1

1

The index.js file in selenium-webdriver\firefox states clearly how to create a new profile dynamically and set the preferences.

* The {@linkplain Profile} class may be used to configure the browser profile
* used with WebDriver, with functions to install additional
* {@linkplain Profile#addExtension extensions}, configure browser
* {@linkplain Profile#setPreference preferences}, and more. For example, you
* may wish to include Firebug:
*
*     const {Builder} = require('selenium-webdriver');
*     const firefox = require('selenium-webdriver/firefox');
*
*     let profile = new firefox.Profile();
*     profile.addExtension('/path/to/firebug.xpi');
*     profile.setPreference('extensions.firebug.showChromeErrors', true);
*
*     let options = new firefox.Options().setProfile(profile); 
*     let driver = new Builder()
*         .forBrowser('firefox')
*         .setFirefoxOptions(options)
*         .build();

It tried it and it didn't work. Obviously I didn't try to add the extension but only to set the 4 preferences written in my question.

What did the trick for me, as a workaround though, was to create a new profile by executing firefox.exe -p on the Windows' Run dialog. (Press the Windows icon key and R on keyboard to get the run dialog)
After that, I used this new "testing" profile as a template to create a new temporary one dynamically through Selenium.

var profile = new firefox.Profile('/pathTo/firefox profile for testing/');

And here is the trick. It appears to be a thing with Firefox and MIME types. If the server, sending the file to download, names the file's content-type differently as the Firefox would, the auto save won't happen and the 'save or open file' dialog will appear.
Probably for security reasons.
The content type can be found here

In my case it's about a CSV file and setting profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv") while the server sends a content-type of text/html and firefox identifies it as TXT file didn't work.
So I edited the handlers.json file inside /pathTo/firefox profile for testing/ by setting the attribute 'ask' of 'text/plain' inside 'mimeTypes' from true to false.

And of course I set as an only preference the path for the file to get downloaded to. BTW this will also create the path, at least the last folder. So my code is:

    else if (this.bName === 'firefox') {
        var cap = Capabilities.firefox();
        var profile = new firefox.Profile('/pathTo/firefox profile for testing/');
        profile.setPreference("browser.download.dir", '/pathTo/Downloads For Testing');
        let options = new firefox.Options().setProfile(profile);
        this.browser = new Builder().forBrowser(this.bName).setFirefoxOptions(options).build();
    }

You can also try this and not mess with the handler.json (end of page) but didn't work for me.

Or just go brute force, this didn't either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.