我正试图使用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">
答案 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)