khoamle
khoamle

Reputation: 726

Testing javascript click function capybara attach file image upload

I'm trying to test a photo upload paperclip using Capybara. However I'm getting an error about file field when running cucumber test.

Unable to find file field :upload (Capybara::ElementNotFound)

Javascript

$("#uploadhere").click(function() {
    $("#photo_upload_entry_upload").click();
  });

Steps.rb file

Then(/^I should see photo when I upload and submit entry$/) do
  script = "$('form.new_photo_upload_entry').css('i.fa.fa-file-image-o');"
  page.execute_script(script)

  fixture_path = Rails.root.join('spec', 'support', 'fixtures', 'test.jpg')

  within('form.new_photo_upload_entry') do 
    attach_file(:upload, fixture_path)
  end
end

HTML (using inspect element)

<input type="file" name="photo_upload_entry[upload]" id="photo_upload_entry_upload">

Ruby code of form in slim format

.entry-label STEP 1: UPLOAD YOUR IMAGE
    .entry-upload#uploadhere
      .upload-here
        i.fa.fa-file-image-o
        br
        = "UPLOAD YOUR IMAGE HERE"
      img
    =f.file_field :upload

Upvotes: 1

Views: 697

Answers (2)

Sosth
Sosth

Reputation: 33

try to use id attribute of an input element, I think it'll be ok.

page.attach_file('photo_upload_entry_upload', path_of_file)

Upvotes: 1

fabdurso
fabdurso

Reputation: 2444

Your

attach_file(:upload, fixture_path)

is wrong. As i can see in your HTML, you'll have to use:

attach_file('photo_upload_entry[upload]', fixture_path)

as the attach_file works with the input field name

Upvotes: 2

Related Questions