水豚无法找到字段 "search"
Capybara unable to find field "search"
我尝试在表单的各个部分设置 id,并将表单包装在 div 中并将 id 设置为 search
。结果总是一样的。
spec/features/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/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)
views/posts/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"
您正在使用的命令 fill_in "search", with: post3.title
应该可以工作,假设您在页面上呈现显示的部分,并且该部分的输出实际上在页面上可见(未通过 [=15= 隐藏) ]).您的场景没有显示访问实际页面,也没有显示 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
我尝试在表单的各个部分设置 id,并将表单包装在 div 中并将 id 设置为 search
。结果总是一样的。
spec/features/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/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)
views/posts/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"
您正在使用的命令 fill_in "search", with: post3.title
应该可以工作,假设您在页面上呈现显示的部分,并且该部分的输出实际上在页面上可见(未通过 [=15= 隐藏) ]).您的场景没有显示访问实际页面,也没有显示 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