Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cucumber test file download

Tags:

cucumber

Anybody have idea how to test file download using cucumber?

like image 765
dimas.priyanto Avatar asked Mar 10 '11 03:03

dimas.priyanto


People also ask

What is a .feature file?

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

How do I test file uploads with Cucumber?

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.

What are the files in Cucumber?

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


3 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
like image 196
ToreyHeinz Avatar answered Oct 25 '22 04:10

ToreyHeinz


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

like image 39
krichard Avatar answered Oct 25 '22 05:10

krichard


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.

like image 1
Tom Avatar answered Oct 25 '22 04:10

Tom