Allow Node#click to set checkboxes and radio buttons in the rack_test driver

This commit is contained in:
Thomas Walpole 2017-01-26 13:53:03 -08:00
parent ea68ce79a2
commit b2af3d45c4
2 changed files with 34 additions and 6 deletions

View File

@ -17,6 +17,12 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
end
def set(value)
return if disabled?
if readonly?
warn "Attempt to set readonly element with value: #{value} \n * This will raise an exception in a future version of Capybara"
return
end
if (Array === value) && !multiple?
raise TypeError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
end
@ -28,11 +34,7 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
elsif input_field?
set_input(value)
elsif textarea?
if self[:readonly]
warn "Attempt to set readonly element with value: #{value} \n * This will raise an exception in a future version of Capybara"
else
native['_capybara_raw_value'] = value.to_s
end
native['_capybara_raw_value'] = value.to_s
end
end
@ -60,6 +62,8 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
((tag_name == 'button') and type.nil? or type == "submit")
associated_form = form
Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_form
elsif (tag_name == 'input' and %w(checkbox radio).include?(type))
set(!checked?)
elsif (tag_name == 'label')
labelled_control = if native[:for]
find_xpath("//input[@id='#{native[:for]}']").first
@ -181,7 +185,7 @@ private
end
native.remove
else
if self[:readonly]
if readonly?
warn "Attempt to set readonly element with value: #{value} \n *This will raise an exception in a future version of Capybara"
else
native['value'] = value.to_s

View File

@ -313,6 +313,30 @@ Capybara::SpecHelper.spec "node" do
@session.find(:css, '#link_placeholder').click
expect(@session.current_url).to match(%r{/with_html$})
end
it "should be able to check a checkbox" do
@session.visit('form')
cbox = @session.find(:checkbox, 'form_terms_of_use')
expect(cbox).not_to be_checked
cbox.click
expect(cbox).to be_checked
end
it "should be able to uncheck a checkbox" do
@session.visit('/form')
cbox = @session.find(:checkbox, 'form_pets_dog')
expect(cbox).to be_checked
cbox.click
expect(cbox).not_to be_checked
end
it "should be able to select a radio button" do
@session.visit('/form')
radio = @session.find(:radio_button, 'gender_male')
expect(radio).not_to be_checked
radio.click
expect(radio).to be_checked
end
end
describe '#double_click', requires: [:js] do