更改Selenium中的Firefox默认下载设置

时间:2014-08-11 09:52:46

标签: ruby-on-rails ruby firefox selenium

我正处于深入的领域,我正在学习如何在Ruby中使用Cucumber和Selenium Webdriver。完全是新的。

我有一个测试,我需要测试CSV文件下载是否有效。目前在Firefox中,出现了一个弹出窗口,Selenium很难专注于此,测试失败。我想在Selenium中更改Firefox的默认设置,以便下载将自动转到下载文件夹并完全跳过弹出框。

我看到了几个答案:

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'

然而,没有解释在何处放置此代码。是否在代码中测试步骤,或env.rb文件等?

求助,请给我这么头疼!!

由于

2 个答案:

答案 0 :(得分:1)

  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

尝试使用此配置

答案 1 :(得分:0)

我只是在Capybara + Selenium + Firefox + PDF下载环境中遇到类似问题。找到了适用于我的解决方案:http://yizeng.me/2014/05/23/download-pdf-files-automatically-in-firefox-using-selenium-webdriver/

希望这能为你们中的一些人节省一些毛发。

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

[编辑]请记住,如果将此驱动程序用作默认设置并且您正在套件中测试pdf预览 - 由于在此驱动程序实例中禁用了预览,因此测试将失败。

如果是这种情况,请注册另一个驱动程序并将其用作默认

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

并使用其他驱动程序进行特定测试

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