2017-01-17 106 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歡迎您,不要忘記接受答案,如果它對您有幫助,那麼問題會被標記爲已回答 –