测试javascript点击功能水豚附件图片上传

Testing javascript click function capybara attach file image upload

我正在尝试使用 Capybara 测试照片上传曲别针。但是,当 运行 黄瓜测试时,我收到有关文件字段的错误。

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

Javascript

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

Steps.rb 文件

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(使用检查元素)

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

Ruby slim 格式的表单代码

.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

你的

attach_file(:upload, fixture_path)

错了。正如我在您的 HTML 中看到的,您必须使用:

attach_file('photo_upload_entry[upload]', fixture_path)

因为 attach_file 使用输入字段名称

尝试使用 input 元素的 id 属性,我认为它可以。

page.attach_file('photo_upload_entry_upload', path_of_file)