diff --git a/lib/capybara/rack_test/node.rb b/lib/capybara/rack_test/node.rb index 40c027cd..e4f992a2 100644 --- a/lib/capybara/rack_test/node.rb +++ b/lib/capybara/rack_test/node.rb @@ -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 diff --git a/lib/capybara/spec/session/node_spec.rb b/lib/capybara/spec/session/node_spec.rb index 434c8702..f5110780 100644 --- a/lib/capybara/spec/session/node_spec.rb +++ b/lib/capybara/spec/session/node_spec.rb @@ -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