Tom
Tom

Reputation: 1105

Change Firefox default download settings within Selenium

I'm in the deep end in which I'm learning to use Cucumber with Selenium Webdriver in Ruby on the job. Totally new to it.

I have a test in which I need to test a CSV file download is working. At the moment in Firefox, a pop up appears and Selenium has trouble focusing on that and the test fails. I'd like to change the default settings of Firefox within Selenium so that the download will automatically go to the downloads folder and skip the pop up box stage completely.

I have seen a few answers:

profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.folderList"] = 1 # use the custom folder defined in "browser.download.dir" below
profile["browser.download.dir"] = 'C:\Users\OSAT TESTING\Downloads'
profile["browser.helperApps.neverAsk.saveToDisk"] = 'application/csv'

Yet there's no explanation as to where to place this code. Is it within the code to test the step, or the env.rb file etc?

Help, please this is giving me such a headache!!

Thanks

Upvotes: 1

Views: 3333

Answers (2)

user2108865
user2108865

Reputation:

I was struggling with similar issue only in the context of Capybara + Selenium + Firefox + PDF download. Found the solution that worked for me here: http://yizeng.me/2014/05/23/download-pdf-files-automatically-in-firefox-using-selenium-webdriver/

Hope this saves some hair-pulling for some of you.

Capybara.register_driver :selenium_autodownload do |app|
  Selenium::WebDriver::Firefox::Binary.path = ENV['CUSTOM_FF_PATH'] if ENV['CUSTOM_FF_PATH'].present?
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile['browser.download.folderList'] = 2
  profile['browser.download.saveLinkAsFilenameTimeout'] = 1
  profile['browser.download.manager.showWhenStarting'] = false
  profile['browser.download.dir'] = "#{Rails.root}/spec/downloads/"
  profile['browser.download.downloadDir'] = "#{Rails.root}/spec/downloads/"
  profile['browser.download.defaultFolder'] = "#{Rails.root}/spec/downloads/"
  profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf,application/x-pdf,application/octet-stream"
  profile["pdfjs.disabled"] = true
  profile["plugin.scan.plid.all"] = false
  profile["plugin.scan.Acrobat"] = "99.0"
  Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end

[EDIT] keep in mind that if this driver is used as the default and you are testing pdf preview in your suite - them tests will fail since the preview is disabled in this driver instance.

If this is the case, register another driver and use it as the default

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :firefox)
end
Capybara.default_driver = :selenium

and use the other driver for the specific test

it 'downloads the pdf file', :driver => :selenium_autodownload

Upvotes: 0

user1896766
user1896766

Reputation: 304

  Capybara.register_driver :selenium do |app|
    Capybara.app_host = "http://google.com"
    Selenium::WebDriver::Firefox::Binary.path = ENV['CUSTOM_FF_PATH'] if ENV['CUSTOM_FF_PATH'].present?
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile.assume_untrusted_certificate_issuer = ENV['SKIP_CERT_ISSUER'].present?
    profile['browser.download.dir'] = "#{Rails.root}/tmp/webdriver-downloads"
    profile['browser.download.folderList'] = 2 # implies custom location
    profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf,application/doc,application/docx,image/jpeg"
    profile.native_events = true
    Capybara::Selenium::Driver.new(app, :browser => :firefox ,:profile => profile)
       #Capybara::Selenium::Driver.new app, :profile => profile
  end

Try with this configuration

Upvotes: 1

Related Questions