Anybody have idea how to test file download using cucumber?
-
2Need a bit more information. Is this a batch app, a website, are you using Watir or similar API? What exactly relating to file download are you trying to test? – Mike Cornell Mar 11 '11 at 15:47
5 Answers
This worked for me based when using send_data like so send_data(data, :filename => "inventory_#{Date.today.to_s}.csv", :disposition => 'attachment')
Probably not best way to write the step, but it worked!
Then /^I should receive a file(?: "([^"]*)")?/ do |file|
result = page.response_headers['Content-Type'].should == "application/octet-stream"
if result
result = page.response_headers['Content-Disposition'].should =~ /#{file}/
end
result
end

- 585
- 1
- 4
- 16
I found this to be a convenient way of testing for downloads, its a naiv way just testing for the headers put for most of the time its reasonable.
If you are using capbybara then put the following inside your step_definitions.rb
Then /^I should get a download with the filename "([^\"]*)"$/ do |filename|
page.response_headers['Content-Disposition'].should include("filename=\"#{filename}\"")
end
Inside your feature you can now do:
When I follow "Export as ZIP"
Then I should get a download with the filename "contacts_20110203.zip"
Cheers
-
1Tried but fired this exception `Capybara::NotSupportedByDriverError Exception: Capybara::NotSupportedByDriverError`. Any suggestion? – kxhitiz Jul 29 '11 at 09:08
-
I only tested it with rack-test as driver. What are you using, Selenium ? – krichard Jul 29 '11 at 09:32
-
-
-
Hi folks recently I have faced same issue. I have solved the problem by the help of these comments.Thank you very much :) – Soundar Rathinasamy Mar 13 '12 at 09:40
-
Would be nice to test with with selenium, anyone know why it doesn't work with `js: true`? – Ian Vaughan Jan 10 '15 at 23:42
I run selenium through chrome, when I'm testing that the csv has downloaded, I use the following in Ruby:
Then /^I should get a downloaded file for fleet: "(.*?)"$/ do |export|
puts Dir["C:/Users/**/Downloads/fleet_#{export}_export_all_*.csv"].last
end
It simply looks in the default download directory and confirms that the file is there and outputs the filename within cmd.

- 1,055
- 1
- 14
- 29
On chrome I do it by opening the page: chrome://downloads
and then use the shadow dom to retrieve the downloaded files:
document
.querySelector('downloads-manager')
.shadowRoot.querySelector('#downloads-list')
.getElementsByTagName('downloads-item');
The downloaded files list also contain information such as file path and download date.

- 2,782
- 22
- 16
For Chrome you can set the capability providing chrome options. Refer below code
String downloadFilepath = "path to specify download location e.g. C:\\Downloads";
Map<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
then you can use exists() method of java.io.File to make sure if file exists.
File file = new file(downloadFilepath+filename);
assert file.exists() : "File not downloaded";

- 41
- 5