简体   繁体   中英

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

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

尝试使用输入元素的id属性,我认为它会没问题。

page.attach_file('photo_upload_entry_upload', path_of_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM