2016-07-26 9 views
1

我有以下規格:RSpec的/水豚:奇怪的行爲沒有找到<INPUT TYPE = 「隱藏」>,但它肯定是有

within dom_id_selector(@boilerplate_copy) do 
    within '.copied_attributes_differing_from_original' do 
    within 'form.title[action="http://www.textdiff.com/"]' do 
     expect(page).to have_css 'button', text: 'Title' 
     expect(page).to have_css 'input[name="string1"]' 
     expect(page).to have_css 'input[name="string2"]' 
    end 
    end 
end 

這裏是有問題的HTML:

<div class="copied_attributes_differing_from_original"> 
    <form action="http://www.textdiff.com/" class="title" method="post" target="_blank"> 
    <input name="string1" type="hidden" value="Boilerplate Original test title"> 
    <input name="string2" type="hidden" value="Boilerplate Copy test title"> 
    <button type="submit">Title</button> 
    </form> 

    <form action="http://www.textdiff.com/" class="success_criterion_id" method="post" target="_blank"> 
    <input name="string1" type="hidden"> 
    <input name="string2" type="hidden" value="1"> 
    <button type="submit">Success criterion</button> 
    </form> 

    // More of them... 
    <form action="http://www.textdiff.com/"... 
</div> 

雖然第一have_css通行證(該按鈕被找到),第二have_css失敗:

Failure/Error: expect(page).to have_css 'input[name="string1"]' 
    expected to find css "input[name=\"string1\"]" but there were no matches. Also found "", which matched the selector but not all filters. 

但在我看來,這個元素絕對是在那裏!我也不明白輸出Also found "", which matched the selector but not all filters.,這是什麼意思?

謝謝。

回答

3

默認情況下,水豚不會找到不可見的元素(隱藏的輸入是),因爲用戶無法看到/與他們交互。如果你真的需要檢查不可見元素的存在,你可以做

expect(page).to have_css('input[name="string1"]', visible: false) 

話雖這麼說,因爲功能測試應該測試該頁面功能的工作原理,而不是確切的細節它是如何運作的,你可不想檢查隱藏輸入的存在,而只是確保它們實現的功能正常工作。

+0

啊,謝謝!你是對的,功能測試應該是黑匣子,但是我測試了一個存在的表單將數據發送到外部資源(這超出了我的測試範圍),所以我唯一能做的就是確保數據可按預期獲得。 –

+0

你有一個想法什麼'也找到了「,」匹配的選擇器,但不是所有的過濾器。「應該是什麼意思? –

+0

@JoshuaMuheim唯一的發現可見行爲被實現爲水豚結果的過濾器,所以它告訴你它找到了一個匹配CSS選擇器但與可見性過濾器不匹配的元素 –

相關問題