Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a CSV using Capybara

For a rspec test I need to download a report in a CSV file format and verify the information given.

The report is generated from a web page when a button is clicked. The browsers save dialog box is open giving the options to open or save.

How can I get the the file to be saved to the computer using rspec and Capybara?

like image 376
user1496754 Avatar asked Oct 03 '13 16:10

user1496754


1 Answers

I have been using MiniTest::Spec for this and did it with the webkit-driver, but it should translate to RSpec without a problem as it is basically just Capybara functionality:

scenario "download overview" do
  within "aside#actions" do
    click_link "Download overview"
  end
  page.status_code.must_equal 200
  page.response_headers['Content-Type'].must_equal "text/csv"
  page.response_headers['Content-Disposition'].must_match /attachment; filename="Übersicht.*\.csv/
  page.must_have_content "Schubidu"
  page.must_have_content "U13"
  page.wont_have_content "5000"
end

I did not go into details about the format of the data, but the records that should have been there were there and the ones that should not have been left out (of course this was just a small dataset to keep the tests swift).

like image 109
Patru Avatar answered Sep 21 '22 17:09

Patru