Search code examples
rubyselenium-webdrivercucumberselenium-chromedriver

Download location Selenium-webdriver Cucumber Chrome


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?


Solution

  • 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)