Permalink
Browse files

Add failing unit test for selected attribute bug

capybara-webkit's driver implements `(un)select_option` by modifying
the target node's `selected` attribute directly. The [HTML5 spec][]
states that this attribute represents the _default selectedness_ of an
`<option>` element, so modifying it will necessarily break the behavior
of reset buttons in forms.

In addition, the existing code leaves this attribute set on existing
`<option>` elements, which can put the page into an invalid state.

[HTML5 spec]: http://dev.w3.org/html5/spec/Overview.html#the-option-element
  • Loading branch information...
1 parent 73a3082 commit acd6e4705c0d32a88e3f3b7b1bbd0e530dbd84b7 @jasonmp85 jasonmp85 committed Feb 12, 2012
Showing with 36 additions and 8 deletions.
  1. +36 −8 spec/driver_spec.rb
View
@@ -380,6 +380,7 @@
</select>
<textarea id="only-textarea">what a wonderful area for text</textarea>
<input type="radio" id="only-radio" value="1"/>
+ <button type="reset">Reset Form</button>
</form>
</body></html>
HTML
@@ -450,18 +451,45 @@
let(:banana_option) { subject.find("//option[@id='topping-banana']").first }
let(:cherry_option) { subject.find("//option[@id='topping-cherry']").first }
let(:toppings_select) { subject.find("//select[@name='toppings']").first }
+ let(:reset_button) { subject.find("//button[@type='reset']").first }
- it "selects an option" do
- animal_select.value.should == "Capybara"
- monkey_option.select_option
- animal_select.value.should == "Monkey"
+ context "a select element's selection has been changed" do
+ before do
+ animal_select.value.should == "Capybara"
+ monkey_option.select_option
+ end
+
+ it "returns the new selection" do
+ animal_select.value.should == "Monkey"
+ end
+
+ it "does not modify the selected attribute of a new selection" do
+ monkey_option['selected'].should be_empty
+ end
+
+ it "returns the old value when a reset button is clicked" do
+ reset_button.click
+
+ animal_select.value.should == "Capybara"
+ end
end
- it "unselects an option in a multi-select" do
- toppings_select.value.should include("Apple", "Banana", "Cherry")
+ context "a multi-select element's option has been unselected" do
+ before do
+ toppings_select.value.should include("Apple", "Banana", "Cherry")
- apple_option.unselect_option
- toppings_select.value.should_not include("Apple")
+ apple_option.unselect_option
+ end
+
+ it "does not return the deselected option" do
+ toppings_select.value.should_not include("Apple")
+ end
+
+ it "returns the deselected option when a reset button is clicked" do
+ reset_button.click
+
+ toppings_select.value.should include("Apple", "Banana", "Cherry")
+ end
end
it "reselects an option in a multi-select" do

0 comments on commit acd6e47

Please sign in to comment.