为什么Capybara(2.8.0)能够找到我的文件输入? (如果我从调试器检查page.body
,则输入存在。)
And(/^if I add a large image$/) do
within all('.story-title-wrapper').first do
attach_file('.story-item-image-url-file-input-large', test_file)
end
end
# Cucumber error message
And if I add a large image # features/step_definitions/story_steps.rb:466
Unable to find file field ".story-item-image-url-file-input-large" (Capybara::ElementNotFound)
./features/step_definitions/story_steps.rb:468:in `block (2 levels) in <top (required)>'
./features/step_definitions/story_steps.rb:467:in `/^if I add a large image$/'
features/long_form_features.feature:450:in `And if I add a large image'
编辑:我能够使用ID(attach_file 'story_title_item_image_url_file_input_large', test_file
)找到输入,但我真的不想为了使这个测试工作而不必为我的所有输入添加ID。有没有办法将现有的课程与Capybara一起使用?
答案 0 :(得分:0)
很可能实际的文件输入元素在页面上的视图中是隐藏的。通常这样做是为了允许在多个平台上相同地设置文件输入的样式。使用#execute_script
删除输入中的任何CSS,使其在页面上不可见,然后在其上调用#attach_file
。
更新:我没有注意到你正在尝试使用课程来定位该字段。由于documented #attach_file
采用元素名称,id或相关标签的文本来查找元素,而不是css选择器。如果你真的需要在课堂上找到,并且必须留在你所使用的Capybara版本,你可以做到
find('.story-item-image-url-file-input-large').set(test_file)
或者如果您更新到Capybara 2.10+,您应该可以
attach_file(test_file, class: 'story-item-image-url-file-input-large')
另外within all('.story-title-wrapper').first
可以只是within first('.story-title-wrapper')
,但是如果有必要的话,这两者都不会等待元素出现在页面上,并且最好不要将其写为within first('.story-title-wrapper', minimum: 1)
}