I am writing my acceptance tests using Capybara and Poltergeist driver.I need to validate the content of the CSV file downloaded.
- I tried various ways of rendering the content on the page itself instead of downloading it.
- Also tried changing the mime types, but it is not working.
Finally I want to settle down with the option of downloading the file in a specific folder and then read the CSV file using core ruby libraries.
In order to achieve this,when poltergeist driver clicks on download link then I want it to handle the pop-up and download the file directly in the given folder.
In Selenium's chrome and firefox drivers, I have option of configuring profiles to handle pop ups and configure download directory.
Is there any such option using poltergeist? Any information will be helpful.
I've had to do similar things in my rails app. My solution is using Javascript to make a XMLHttpRequest to the URL, downloading the file, returning the contents of the file back to Capybara, and using ruby to save the file somewhere on disk. Then in another step, I check the contents to the downloaded CSV file.
Here's the step definition for downloading the file:
Change the URL in the Javascript code to your CSV's location.
And finally, here's my step definition for validating the CSV file's contents:
Good luck. Hope this helps.
It is not possible with Poltergeist, you can just check the headers.
But is is possible with Chrome driver and also with recent versions of Firefox and Selenium Webdriver. Unfortunately it runs via Selenium - i.e. not headless... See this article: http://collectiveidea.com/blog/archives/2012/01/27/testing-file-downloads-with-capybara-and-chromedriver/
My approach - slightly different as I'm working with Spinach and Rubyzip:
Add the following to your Gemfile
features/support/capybara.rb - I'm using Poltergeist for scenarios with
@javascript
tag and Chrome for scenarios with@download
tag.features/support/downloads.rb
features/file_download.feature
features/steps/file_download.rb - Note that you can't use
page.response_headers
as it is not supported by the Selenium/ChromeDriver. But you can check the filename of the downloaded file using theFile.basename()
.This is not currently possible with Poltergeist.
I think you'd be better off writing a test for this CSV which doesn't use Capybara. (E.g. by using the built-in Rails integration testing stuff and parsing the response as a CSV.)
There is an ticket to support downloading files in PhantomJS/Poltergeist and there are one or two forks which claims that they made it to work somehow. See https://github.com/ariya/phantomjs/issues/10052