Skip to content

Commit

Permalink
Add failing unit test for selected attribute bug
Browse files Browse the repository at this point in the history
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
jasonmp85 committed Feb 12, 2012
1 parent 73a3082 commit acd6e47
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions spec/driver_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit acd6e47

Please sign in to comment.