2017-01-17 60 views
1

我尝试在窗体的各个部分设置id,以及将id封装到ID为search的div中。结果总是一样的。水豚找不到字段“搜索”

规格/功能/ 02_post_spec.rb

scenario 'search for post title' do 
    fill_post_form(post3) 
    fill_in "search", with: post3.title 
    click_button("Search") 

    expect(page).to_not have_content(post1.title) 
    expect(page).to_not have_content(post2.title) 
    expect(page).to have_content(post3.title) 
    end 

规格/ spec_helper.rb

def fill_post_form(post) 
    visit new_post_path 
    fill_in 'Title', with: post.title 
    fill_in 'Body', with: post.body 
    click_button 'Create Post' 
end 

这将redirect_to post_path(post)

的意见/职位/ index.html.erb

<%= form_tag(posts_path, method: :get, class: "block") do %> 
    <div class="form-group"> 
    <div class="input-group"> 
     <%= text_field_tag :search, params[:search], class: "form-control", id: "search" %> 
     <span class="input-group-btn"> 
     <%= submit_tag "Search", name: nil, class: "btn btn-default" %> 
     </span> 
    </div> 
    </div> 
<% end %> 

水豚输出

Failure/Error: fill_in "search", with: post3.title 

Capybara::ElementNotFound: 
    Unable to find field "search" 
+0

查看页面上表单的原始html ...很可能他的字段被称为有点不同的东西。如果您尝试基于ID,那么您可能还需要'fill_in'#search''或类似的东西。 –

+0

@TarynEast'fill_in'按输入元素的名称,id或相关标签文本进行搜索 - 它不接受CSS选择器,因此它将是'fill_in'search',...'不是#search –

回答

2

你使用应该工作的命令fill_in "search", with: post3.title,假设你是呈现出局部的页面上,这部分的输出是页面(通过CSS不隐藏)在实际可见。您的方案不显示访问实际页面,并且您不显示fill_post_form正在做什么,因此很难确切知道发生了什么问题。第一步是做类似

fill_post_form(post3) # already in your tests 
sleep 2 # wait a few seconds to make sure above method has completed whatever actions it does 
puts page.html # output the current page and take a look to see if your 'search' element is actually on the page 
+0

您是对的。这就像在错误的页面上一样简单。我一直盯着屏幕太久。谢谢。 –

+0

@ n.milsht欢迎您,不要忘记接受答案,如果它对您有帮助,那么问题会被标记为已回答 –