gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:09:22 25 4
gpt4 key购买 nike

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

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">

Slim 格式的 Ruby 代码

.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 使用输入字段名称

关于javascript - 测试javascript点击功能capybara附加文件图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com