Selenium : How to handle file download in Firefox.

By | April 3, 2016

If you use Selenium to test your applications with Firefox, You already have been into the case where a test trigger a file download (pdf, excel, png). This don’t block your test but windows can quickly accumulate if your application uses lots of download. Here is the solution.

To resolve this problem you need to edit the default firefox profile and change some properties.

FirefoxProfile profile = new FirefoxProfile()

profile.setPreference("", 2);

profile.setPreference("", DOWNLOAD_DIR);

profile.setPreference("browser.helpersApps.neverAsk.openFile", "application/octet-stream;application/csv;text/csv;application.pdf;image/png:image/jpeg;text/plain"

profile.setPreference("browser.helpersApps.neverAsk.saveToDisk", "application/octet-stream;application/csv;text/csv;application.pdf;image/png:image/jpeg;text/plain"

profile.setPreference("browser.helpersApps.alwaysAsk.force", false);

new FirefoxDriver(profile);

The property specifies the directory where files will be downloaded.

  • 0 : The user desktop
  • 1 : The user download directory
  • 2 : A custom directory

Here we choose the custom directory option and we specify a directory with the property.

browser.helpersApps.neverAsk.openFile and browser.helpersApps.neverAsk.saveToDisk properties specifies which extensions handled by the browser.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.