1

I am using cucumber to test my application(which is in Rails). Now, I am having problem to test downloading file. I am using backbone so I am putting @javascript tag at every feature. I have gone through the solutions which is given in stackoverflow like : cucumber test file download but they are in rack-test. I want to know that how to click on file download box when we have @javascript in cucumber? I was trying to verify the response headers like : page.response_headers['Content-Type'].should == "text/csv" but it shows that its not supported in capybara.

Any suggestion would be appreciated.

Community
  • 1
  • 1
Anand Soni
  • 5,070
  • 11
  • 50
  • 101

1 Answers1

3

I assume that you use Selenium as a driver which is default driver in case of @javascript.

Selenium doesn't provide cross-browser solution for this. You want to test your application, not browser's native download windows so you should tell browser to autosave downloaded files.

If you use Firefox:

Capybara.register_driver :selenium do |app|
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile['browser.download.dir'] = "/path-to-folder/webdriver-downloads"
  profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf" # content-type of file that will be downloaded
  Capybara::Selenium::Driver.new(app, :browser => :firefox, profile: profile)
end

If you use Google Chrome:

Capybara.register_driver :selenium do |app|
  profile = Selenium::WebDriver::Chrome::Profile.new
  profile['download.prompt_for_download'] = false
  profile['download.default_directory'] = "/path/to/dir"
  Capybara::Selenium::Driver.new(app, :browser => :chrome, profile: profile)
end

Then you click link (which invokes file download) and file is saved to specified directory.

Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123