2013-06-23 25 views
2

如何測試一個元素不存在? 我試過以下,但得到錯誤textarea#Question1Text does not exist,而不是通過測試。如何測試一個元素不存在與RSpec?

it "should not have question 1" do 
    find('textarea#Question1Text').should_not be 
end 

it { should_not find('textarea#Question1Text') } 

回答

3

你可以使用水豚匹配器(具體have_css)中的一個,以測試的元件存在與否。

it 'should not have question1' do 
    page.should_not have_css('textarea#Question1Text') 
end 
1

易peasy

expect{page.find_by_id("update-#{term1.id}").should}.not_to raise_error 
expect{page.find_by_id("update-#{term4.id}")}.to raise_error 

,如果你不具有唯一的ID,然後將CSS作爲spullen建議將工作

2

與方式的問題,建議要做到這一點,他們將有比賽例如,如果在點擊一個按鈕後隱藏了一個字段,就會出現這種情況。在這種情況下,它會在按鈕被點擊後但在JavaScript有機會找到它之前抓取頁面,因此您的測試可能會失敗。它往往是間歇性的,主要發生在像phantomjs這樣的快速無頭瀏覽器上。

相反,我建議你使用類似:

expect(page).to have_selector("#id_of_element") 

這將等到條件爲真,但如果失敗,還是會迅速超時。