Capybara无法找到字段“搜索”

时间:2017-01-17 02:25:47

标签: ruby-on-rails capybara

我尝试在表单的各个部分设置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"

1 个答案:

答案 0 :(得分:2)

您正在使用的命令fill_post_form应该有效,假设您在页面上呈现显示的部分,并且该部分的输出实际上在页面上可见(不通过CSS隐藏)。您的方案没有显示访问实际页面,并且您没有显示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 正在执行的操作,因此很难确切地知道出现了什么问题。第一步是做一些像

这样的事情
x