2016-10-25 133 views
0

为什么不是水豚(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 )输入,但我真的不希望有添加标识到我所有的输入只是为了使这个测试工作。有什么方法可以将现有的类与Capybara一起使用?

回答

0

很可能实际的文件输入元素在页面上隐藏。这通常是为了允许在多个平台上对文件输入进行相同的样式设置。使用#execute_script删除输入上的任何CSS使其在页面上不可见,然后在其上调用#attach_file

更新:我没有注意到你正在尝试使用类来定位字段。由于documented#attach_file需要元素名称,id或关联标签的文本来查找元素,而不是css选择器。如果你真的需要通过类查找,并有停留在版本豚的你,你可以做

find('.story-item-image-url-file-input-large').set(test_file) 

,或者如果您更新到水豚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)

+0

谢谢您的回答,但是这不是案件。 – pdoherty926

+0

@ pdoherty926现在更新了答案,我注意到您正在尝试使用类名称 –

+0

Aha,我没有意识到2.10中引入了'class:'选项。我尝试使用它,并遇到一个无效的键错误 - 现在有道理。 – pdoherty926

相关问题