9

I'm using Cucumber with Ruby. When running tests in Chrome via Selenium-Webdriver, I'd like to alter the download location to the test folder instead of the users download folder.

My current chrome driver is set up like so:

Capybara.default_driver = :selenium
 Capybara.register_driver :selenium do |app|
    Capybara::Selenium::Driver.new(app, :browser => :chrome,
      desired_capabilities: {
      'chromeOptions' => {
      'args' => %w{ window-size=1920,1080 }

     }
    }
   )
  end

What would I need to add there to change the download location please?

3 Answers 3

16

The download directory can be set with the download.default_directory preference:

require 'capybara'
require 'selenium-webdriver'

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app,
    :browser => :chrome,
    :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
      'chromeOptions' => {
        'args' => [ "--window-size=1920,1080" ],
        'prefs' => {
          'download.default_directory' => File.expand_path("C:\\Download"),
          'download.prompt_for_download' => false,
          'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
        }
      }
    )
  )
end

session = Capybara::Session.new(:chrome)
1
  • You sir, are a gentleman. Thank you. I've been trying many combinations of this. I was missing 'File.expand_path'
    – Tom
    May 24, 2016 at 7:00
0

I've run into this problem recently and was unable to get the previous answer to work due to my setup being different. I have the following setup:

test_helper.rb

ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/reporters'
MiniTest::Reporters.use!

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

application_system_test_case.rb

require 'test_helper'
require 'capybara/rails'
require 'capybara/poltergeist'
require 'fileutils'
require 'selenium-webdriver'

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  #driven_by :poltergeist, options: { js_errors: false } #uncomment if you want to run headless
  self.use_instantiated_fixtures = true
  @downloads = File.expand_path(Rails.root + 'tmp/downloads')
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: {prefs:{
      'download.default_directory' => @downloads,
      'download.prompt_for_download' => false,
      'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
  } }
end

A typical test: admin_page_test.rb

require 'application_system_test_case'
include ApplicationHelper

class AdminPageTest < ApplicationSystemTestCase

  setup do
    visit new_user_session_url
    fill_in 'Email', with: '[email protected]'
    fill_in 'Password', with: 'password'
    click_on 'commit'
    assert_selector 'h1', text: 'Admin Status Board'
  end

I looked everywhere for the correct way to pass the options in the class and finally stumbled on it through reading through the Capybara modules, and trial and error. I think I read almost a hundred posts in various places and none of them worked. Hopefully this helps someone who comes across it.

5
  • This indeed works but it throws a deprecation warning with Rails 6.1 (:prefs is deprecated. Use Selenium::WebDriver::Chrome::Options#add_preference instead). Unfortunately I could not figure out how to use Selenium::WebDriver::Chrome::Options#add_preference properly yet. Feb 9, 2021 at 9:42
  • For me also. And I have tried countless ways to get the '#add_preference' to work but nothing works for me. I really wish if they say something like that they would give an example or a link to where the examples would be. It's driving me crazy to the point that I'm just having to ignore it until it finally breaks and then I'm forced to fix it.
    – Beartech
    Feb 9, 2021 at 14:52
  • It seems yielding to a block works: driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400] do |driver_options| driver_options.add_preference(:download, prompt_for_download: false) driver_options.add_preference(:download, default_directory: File.join(Rails.root, "tmp", "downloads")) end Feb 9, 2021 at 15:47
  • So I'm not getting errors with your code and it seems to work, but it is not setting the download directory. Files are downloading to my Downloads directory in my profile.
    – Beartech
    Feb 9, 2021 at 20:25
  • That's interesting. For me it works as expected (or as they say: "works on my machine", sorry I can't be of more help). Feb 10, 2021 at 8:09
0

Since my Chrome driver is set up with options rather than capabilities, I ended up going this route:

Capybara.register_driver :selenium_chrome_headless do |app|
  Capybara::Selenium::Driver.load_selenium
  browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
    opts.args << "--headless"
  end

  browser_options.add_preference(:download, { prompt_for_download: false, default_directory: DOWNLOAD_PATH })
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end

As inspired by this gist

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.