Skip to content

Commit

Permalink
Add option option when selecting radio buttons or checkboxes by the…
Browse files Browse the repository at this point in the history
…ir name, closes #1040
  • Loading branch information
jnicklas committed Oct 20, 2013
1 parent 3f5180f commit 14e7ae5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/capybara/selector.rb
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions lib/capybara/spec/session/check_spec.rb
Expand Up @@ -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
14 changes: 14 additions & 0 deletions lib/capybara/spec/session/choose_spec.rb
Expand Up @@ -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.