barbasa
barbasa

Reputation: 675

Capybara headless download

I'm quite new to Capybara, so I might have misunderstood something. What I'm trying to do is download a file with Capybara and the Headless gem by clicking on a link. In that case I'm using a Vagrant page to test it.

I'm using the following code to try to achieve it:

require 'selenium-webdriver'
require 'capybara'
require 'headless'

class MyDownloader
  include Capybara::DSL

  def initialize

    headless = Headless.new(:destroy_at_exit => true)
    headless.start

    Capybara.configure do |config|
      config.default_driver = :selenium
    end

    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['browser.download.dir'] = '/tmp'
    profile['browser.helperApps.neverAsk.saveToDisk'] = 'application/octet-stream'
    profile['browser.helperApps.alwaysAsk.force'] = false
    profile['browser.download.manager.showWhenStarting'] = false

    Capybara.register_driver :firefox do |app|
        Capybara::Selenium::Driver.new(app,
            browser: :firefox,
            profile: profile,
        )
    end

  end

  def download_stuff
    visit 'https://www.vagrantup.com/download-archive/v1.7.1.html'
    click_link('here')
  end

end

MyDownloader.new.download_stuff

sleep 10

I can see that the download has been started (there is a .part file in the /tmp directory), but even if I wait for long time (see the 10 seconds sleep for a really small file) it doesn't complete.

What am I doing wrong ?

Upvotes: 0

Views: 930

Answers (1)

barbasa
barbasa

Reputation: 675

I found out the reason why it wasn't working.

There were 2 issues in my code:

  1. Wrong driver:

    Capybara.register_driver :selenium do |app|
    

    Has to be selected instead of

    Capybara.register_driver :firefox do |app|
    
  2. folderList setting missing:

    Apparently this setting is needed as well:

    profile['browser.download.folderList'] = 2 # 2 - save to user defined location
    

Upvotes: 1

Related Questions