2017-01-17 1 views
1

id를으로 설정하여 div의 양식을 줄뿐만 아니라 양식의 여러 부분에서 id를 설정해 보았습니다. 결과는 항상 동일합니다.Capybara 필드를 찾을 수 없습니다. "search"

사양/기능/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', ...'not #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

You 맞았 어. 그것은 잘못된 페이지에있는 것처럼 간단했습니다. 나는 너무 오랫동안 화면을 쳐다 보았다. 고맙습니다. –

+0

@ n.milsht 환영합니다. 도움이된다면 답을 수락하는 것을 잊지 마십시오. 그러면 질문에 답변이 표시됩니다. –