Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a disabled field with Capybara

Tags:

I have a select box, with a label:

<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>

As you can see the select box is disabled. When I try to find it with field_labeled("Slide orientation"), it returns an error:

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!'

When the select box is not disabled, field_labeled("Slide orientation") returns the select element just fine.

Is this expected behavior? And if so, how would I go about finding a disabled element? In my case I need it to test if it's disabled or not.

like image 947
Pascal Lindelauf Avatar asked Mar 13 '13 21:03

Pascal Lindelauf


2 Answers

Capybara 2.1.0 supports disabled filter. You can easily find disabled fields with it.

field_labeled("Slide orientation", disabled: true)

You need to specify it explicitly because the disabled filter is off by default.

like image 78
Shuhei Kagawa Avatar answered Sep 28 '22 03:09

Shuhei Kagawa


This one passes if it has the disabled attribute.

Run with js: true and page.evaluate_script.

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

Update

or you can use have_css

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

(stolen form this excellent answer )

like image 22
Andreas Lyngstad Avatar answered Sep 28 '22 03:09

Andreas Lyngstad