如何强制Capybara下载到我指定的目录而不是Firefox中的默认下载目录?

时间:2018-04-05 04:40:16

标签: ruby-on-rails windows selenium capybara geckodriver

当我运行测试时,Firefox会将文件下载到默认下载目录USER\Downloads。但是,我告诉它要下载到PROJECT\tmp目录。我怎么让它听我的?

spec/config/capybara.rb
DOWNLOAD_DIRECTORY = Rails.root.join('tmp', 'capybara', 'downloads').to_s

Capybara.register_driver :firefox do |app|
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile['devtools.selfxss.count'] = 9999
  profile['browser.download.dir'] = DOWNLOAD_DIRECTORY
  profile['browser.download.folderList'] = 2 # implies custom location
  profile['browser.helperApps.neverAsk.saveToDisk'] =
    'text/csv,text/tsv,text/xml,text/plain,application/pdf,application/doc,application/docx,image/jpeg,application/gzip,application/x-gzip'
  profile.native_events = true
  options = Selenium::WebDriver::Firefox::Options.new
  options.profile = profile
  Capybara::Selenium::Driver.new(
    app,
    browser: :firefox,
    options: options,
    desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox(
      marionette: true
    )
  )
end
part of the feature test
click_on 'CTD_chem_gene_ixn_types.csv'
# Only run for download capable drivers
download_capable do
  wait_until(10) {
    download_present?('CTD_chem_gene_ixn_types.csv')
  }
end

错误消息

 Failure/Error: File.exist?(Rails.root.join('tmp', 'capybara', 'downloads', file))

 Timeout::Error:
   execution expired
 # ./spec/support/feature_support.rb:139:in `download_present?'

Capybara 2.16.1,selenium-webdriver 3.4.4,Firefox 59.0.2

1 个答案:

答案 0 :(得分:0)

我必须做以下

  profile['browser.download.dir'] = DOWNLOAD_DIRECTORY
  profile['browser.download.dir'] = DOWNLOAD_DIRECTORY.gsub('/', '\\') if Gem.win_platform?