我嘗試在窗體的各個部分設置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"
查看頁面上表單的原始html ...很可能他的字段被稱爲有點不同的東西。如果您嘗試基於ID,那麼您可能還需要'fill_in'#search''或類似的東西。 –
@TarynEast'fill_in'按輸入元素的名稱,id或相關標籤文本進行搜索 - 它不接受CSS選擇器,因此它將是'fill_in'search',...'不是#search –