3
votes

I'm using Cucumber/Capybara to test a web application. I'm pretty much a complete beginner in Ruby and its a real testimony to the developers of Cucumber/Capybara just how far I have been able to test my application with only the miniscule amount of Ruby knowledge that I have.

However, as you've probably guessed, I've reach the point were I need some expert help. I need to test a multipart file upload. The problem is that the web application that I'm testing has a URL command interface, but no associated pages. So I can't just load the page, fill in a parameter and push a button. I have to format the POST command programatically.

Up until now, I have been interacting this the application exclusively using 'visit'. i.e. i have steps definitions such as:

Given /^I delete an alert with alertID "([^"]*)" from the site$/ do |alertID|
visit WEB_SITE_ROOT + "/RemoteService?command=deleteAlert&siteName=#{$Site}&alertID=#{alertID}" 
end

But now I need to do some posts. I found some code that seems to do what I need:

Given /^I upload the "([^"]*)" file "([^"]*)" for the alert$/ do |fileType, fileName|
file = File.new(fileName, "rb")
reply = RestClient.post(
  "#{WEB_SITE_ROOT}" + "/FileUploader?command=upload&siteName=#{$Site}&alertID=#{$OriginalAlertID}",
  :pict       =>  file,
  :function   =>  "#{fileType}",
  :content_type => 'multipart/jpg',
  )
end

But this is not running in the same cucumber/capybara session, and so is not authorised (one of the previous steps was a login). Also, the reply from the web application is not picked up by cucumber/capybara and so my test for success/failure do not work.

Can someone please point me in the right direction?

1

1 Answers

0
votes

By default capybara uses the Rack::Test adapter which will bypass the HTTP server and interact with your Rack/your app directly. The POST request you're doing in your step won't go through capybara, hence why it's failing.

To upload files when using Rack::Test you'll need to use the Rails #fixture_file_upload method, which by default should be available in your cucumber steps.