Permalink
Browse files

Add `option` option when selecting radio buttons or checkboxes by the…

…ir name, closes #1040
  • Loading branch information...
1 parent 3f5180f commit 14e7ae57db41baa1d3dcccdc48b97fa0d79e8c0f @jnicklas jnicklas committed Oct 20, 2013
Showing with 30 additions and 0 deletions.
  1. +2 −0 lib/capybara/selector.rb
  2. +14 −0 lib/capybara/spec/session/check_spec.rb
  3. +14 −0 lib/capybara/spec/session/choose_spec.rb
View
@@ -146,13 +146,15 @@ def filter(name, options={}, &block)
xpath { |locator| XPath::HTML.radio_button(locator) }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
+ filter(:option) { |node, value| node.value == value.to_s }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
end
Capybara.add_selector(:checkbox) do
xpath { |locator| XPath::HTML.checkbox(locator) }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
+ filter(:option) { |node, value| node.value == value.to_s }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
end
@@ -96,4 +96,18 @@
end.to raise_error(Capybara::ElementNotFound)
end
end
+
+ context "with `option` option" do
+ it "can check boxes by their value" do
+ @session.check('form[pets][]', :option => "cat")
+ @session.click_button('awesome')
+ extract_results(@session)['pets'].should include('cat')
+ end
+
+ it "should raise an error if option not found" do
+ expect do
+ @session.check('form[pets][]', :option => "elephant")
+ end.to raise_error(Capybara::ElementNotFound)
+ end
+ end
end
@@ -51,4 +51,18 @@
end.to raise_error(Capybara::ElementNotFound)
end
end
+
+ context "with `option` option" do
+ it "can check radio buttons by their value" do
+ @session.choose('form[gender]', :option => "male")
+ @session.click_button('awesome')
+ extract_results(@session)['gender'].should == "male"
+ end
+
+ it "should raise an error if option not found" do
+ expect do
+ @session.choose('form[gender]', :option => "hermaphrodite")
+ end.to raise_error(Capybara::ElementNotFound)
+ end
+ end
end

0 comments on commit 14e7ae5

Please sign in to comment.