14

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?

4
  • That sounds wrong. Why not testing whether the controller sends you the correnct file (MIME type, response code) and whether the model generates the correct data (content)? In an integration spec I'd only test, whether the button exists and that a click won't blow up your application.
    – DMKE
    Oct 3, 2013 at 16:53
  • Sorry about not being clear. All I care about it getting the correct file and verify data. I just don't know how to get the file from the site using Capybara. Oct 3, 2013 at 17:05
  • 1
    Exactly my point. You can simply get a file with a controller spec; you don't need an integration/feature test to do so: in a controller spec, after a get :show, id: 42, format: :csv, the response.body will contain the CSV content.
    – DMKE
    Oct 3, 2013 at 22:06
  • 3
    This is a perfectly, legitimate question. Yes, alternatively, you can write a controller test, but it's also reasonable to want to test that the user sees a "download dialog".
    – Andrew
    Feb 4, 2014 at 21:00

3 Answers 3

15
+50

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

1
  • You might also want to assert against page.body rather than page. The latter rendering of the CSV (via page) will cause brower-y things to happen to its contents, e.g. truncation of whitespace, potentially leading to false negatives / positives. If you do go with page.body you're back to plain ol' string comparison assertions so it would make sense to use Rspec's matchers rather than Capybara's.
    – biril
    Jul 29, 2021 at 7:47
6

Assuming you have a controller action that looks something like this:

require 'csv' # assuming you have this line somewhere

  def download
    @records = MyModel.all
    csv_data = CSV.generate do |data|
      data << ["Field 1", "Field 2"]
      @records.each do |record|
        data << [record.field1, record.field2]
      end
    end
    send_data csv_data, type: "text/csv", filename: "my_file.csv"
  end

I was able to use Capybara to verify that clicking my button would have caused the browser to trigger a download prompt:

click_on 'Download as CSV'
header = page.response_headers['Content-Disposition']
header.should match /^attachment/
header.should match /filename="my_file.csv"$/

Note that page.response_headers is driver specific. I'm using capybara-webkit. A Content-Disposition header should contain attachment rather than inline for it to cause a download prompt. Capybara also displayed the content of the file as the response body (although this may also be driver specific), so it was easy to verify the output:

MyModel.all.each do |record|
  page.should have_content record.field1
  page.should have_content record.field2
end
1
  • That is indeed driver specific. PhantomJS doesn't do that. Sep 27, 2016 at 9:00
2

If you are "verify(ing) the information given", then you use DMKE's 2nd answer. You may need to post to a login form to get a session cookie first, and use post rather than get for the form that will result in the file being downloaded.

If the result uses javascript and an iframe to download the file, then parse the returned html, extract the actual url to download the information and download the data from that.

My personal preference is to test my code against a copy of the file checked into source code control, so tests of my own code is not dependent on the foreign site being up and available. I code up a rake task / helper class to grab a fresh copy of the data as appropriate, and have some simple tests to validate I can decode the data file into something that makes sense.

If you tests are validating exactly how the data is decoded then you will want a fixed input file. If you are just validating that it "look reasonable" then you can

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.