测试javascript点击功能capybara附件文件图片上传

时间:2015-12-10 20:01:21

标签: javascript ruby-on-rails-4 tdd cucumber capybara

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

range_error

的Javascript

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

Steps.rb文件

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

HTML(使用inspect元素)

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

瘦身格式的Ruby代码

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

2 个答案:

答案 0 :(得分:2)

attach_file(:upload, fixture_path)

错了。正如我在您的HTML中看到的,您将不得不使用:

attach_file('photo_upload_entry[upload]', fixture_path)

,因为attach_file适用于输入字段名称

答案 1 :(得分:1)

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

page.attach_file('photo_upload_entry_upload', path_of_file)