Allow Node#click to set checkboxes and radio buttons in the rack_test driver
This commit is contained in:
parent
ea68ce79a2
commit
b2af3d45c4
|
@ -17,6 +17,12 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(value)
|
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?
|
if (Array === value) && !multiple?
|
||||||
raise TypeError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
|
raise TypeError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
|
||||||
end
|
end
|
||||||
|
@ -28,11 +34,7 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
|
||||||
elsif input_field?
|
elsif input_field?
|
||||||
set_input(value)
|
set_input(value)
|
||||||
elsif textarea?
|
elsif textarea?
|
||||||
if self[:readonly]
|
native['_capybara_raw_value'] = value.to_s
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -60,6 +62,8 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
|
||||||
((tag_name == 'button') and type.nil? or type == "submit")
|
((tag_name == 'button') and type.nil? or type == "submit")
|
||||||
associated_form = form
|
associated_form = form
|
||||||
Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_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')
|
elsif (tag_name == 'label')
|
||||||
labelled_control = if native[:for]
|
labelled_control = if native[:for]
|
||||||
find_xpath("//input[@id='#{native[:for]}']").first
|
find_xpath("//input[@id='#{native[:for]}']").first
|
||||||
|
@ -181,7 +185,7 @@ private
|
||||||
end
|
end
|
||||||
native.remove
|
native.remove
|
||||||
else
|
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"
|
warn "Attempt to set readonly element with value: #{value} \n *This will raise an exception in a future version of Capybara"
|
||||||
else
|
else
|
||||||
native['value'] = value.to_s
|
native['value'] = value.to_s
|
||||||
|
|
|
@ -313,6 +313,30 @@ Capybara::SpecHelper.spec "node" do
|
||||||
@session.find(:css, '#link_placeholder').click
|
@session.find(:css, '#link_placeholder').click
|
||||||
expect(@session.current_url).to match(%r{/with_html$})
|
expect(@session.current_url).to match(%r{/with_html$})
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe '#double_click', requires: [:js] do
|
describe '#double_click', requires: [:js] do
|
||||||
|
|
Loading…
Reference in New Issue