toy
toy

Reputation: 12151

Cucumber + Capybara file upload not working for me

I'm trying to test a simple file upload with Capybara. Here is my Gemfile.lock

capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)

and my selenium-webdriver version is 2.18. Here is my web_steps file (it's generated):

When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"(?: within "([^\"]*)")?$/ do |path, field, selector|
  with_scope(selector) do
    attach_file(field, path)
  end
end

This is my feature on the line to upload file:

Then I attach the file "features/resources/empty.file" to "file" within "#uploadForm"

Actually it ran fine and green on the line, but the input didn't pick up any file so the test failed after that because there was no file selected.

Here is my form:

%form#uploadForm{:action => "/upload", :method => "POST", :enctype => "multipart/form-data"}
  %input{:type => "file", :name => "file", :id => "file"}

It's pretty basic, but I'm not sure why it's not working.

Upvotes: 3

Views: 8009

Answers (2)

alexey_the_cat
alexey_the_cat

Reputation: 1872

Toy, I suggest to:

attach_file(field, File.expand_path(path))

Instead of using absolute path, especially if you collaborating.

edit: File.expand_path not File.extend_path

Upvotes: 12

toy
toy

Reputation: 12151

I just wanted to answer my question, the problem is I didn't use the full path. Somehow capybara doesn't recognise relative path.

Upvotes: 5

Related Questions