It’s not so obvious how to test a scenario, where user clicks an export link and gets a CSV file download. Especially if the user is able to filter the generated CSV content before downloading.
Capybara’s Poltergeist engine is a wrapper for headless browser called PhantomJS. A basic Capybara spec to test the downloading might look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
We select a filter from a select box, apply the filter and download the CSV.
The problem here is that Poltergeist starts a download when clicking the export
link and the page.text contains the previous page, not the CSV one might
expect. There seems to be no clean solution for this.
An easy way out is to register a different mime type for CSV files for the tests, so we can trick Poltergeist to render the CSV instead of downloading it:
1 2 3 4 5 6 7 8 9 | |
And now our page.text contains the data we want.