2013-03-13 96 views
15

殘疾場我有一個選擇框,一個標籤:查找與水豚

<label for="slide_orientation">Slide orientation</label> 
<select disabled="" class="property" id="slide_orientation" name="slide_orientation"> 
    <option value="horizontal">Horizontal</option> 
    <option value="vertical" selected="selected">Vertical</option> 
</select> 

正如你可以看到選擇框被禁用。當我試着使用field_labeled("Slide orientation")找到它,它會返回一個錯誤:

Capybara::ElementNotFound: Unable to find field "Slide orientation" 
from /Users/pascal/.rvm/gems/ruby-1.9.3-p392/gems/capybara-2.0.2/lib/capybara/result.rb:22:in `find!' 

當選擇框未被禁用,field_labeled("Slide orientation")返回選擇元素就好了。

這是預期的行爲?如果是這樣,我將如何去尋找一個禁用的元素?在我的情況下,我需要它來測試它是否被禁用。

+1

按鈕的情況是一樣的:http://stackoverflow.com/questions/12917227/how-do-i-write-a-capybara-assertion-that-c​​hecks-for-the-presence-of-a -button-和 – 2014-10-15 15:22:52

回答

2

安德烈亞斯和this answer讓我在賽道上的最終解決方案。查找同一個標籤下(而不是HTML ID),一個無效字段可以實現這種方式:

label_field = all("label").detect { |l| (l.text =~ /#{label}/i).present? } 
raise Exception.new("Couldn't find field '#{label}'") if label_field.nil? 
the_actual_field = first("##{label_field[:for]}") 

如果該字段被禁用,可以用一個語句來完成檢查:

page.should have_css("##{label_field[:for]}[disabled]") 

它仍然感覺像一個解決方法,而不是最好的水豚類解決方案,但它的工作原理!

+0

ouch,不幸的是只適用於

9

如果它具有禁用屬性,則通過該通道。使用js: truepage.evaluate_script運行。

it "check slider orientation", js: true do 
    disabled = page.evaluate_script("$('#slide_orientation').attr('disabled');") 
    disabled.should == 'disabled' 
end 

更新

,或者您可以使用have_css

page.should have_css("#slide_orientation[disabled]") 

(被盜形式this excellent answer

30

Capybara 2.1.0 supports disabled filter。你可以很容易地找到它的殘疾領域。

field_labeled("Slide orientation", disabled: true) 

您需要明確指定它,因爲默認情況下disabled過濾器處於關閉狀態。