Anybody have idea how to test file download using cucumber?
A feature file is usually a common file which stores feature, scenarios, and feature description to be tested. The feature file is an entry point, to write the cucumber tests and used as a live document at the time of testing. The extension of the feature file is ". feature".
I put them inside features/upload-files, you can place them wherever you like. The attach_file method received a symbol with the name of the field used for the upload and the path for a file in your disc. After associating the field with the path for the file, all we'll have to do is “to click” at the submit button.
The file, in which Cucumber tests are written, is known as feature files. It is advisable that there should be a separate feature file, for each feature under test. The extension of the feature file needs to be “. feature”.
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With