mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Merge pull request #933 from jamescook/cleanup_set_method
Clean up Capybara::RackTest::Node#set
This commit is contained in:
commit
6ee2454240
1 changed files with 65 additions and 29 deletions
|
@ -15,35 +15,14 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
|
||||||
if (Array === value) && !self[:multiple]
|
if (Array === value) && !self[:multiple]
|
||||||
raise ArgumentError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
|
raise ArgumentError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
|
||||||
end
|
end
|
||||||
if tag_name == 'input' and type == 'radio'
|
|
||||||
other_radios_xpath = XPath.generate { |x| x.anywhere(:input)[x.attr(:name).equals(self[:name])] }.to_s
|
if radio?
|
||||||
driver.dom.xpath(other_radios_xpath).each { |node| node.remove_attribute("checked") }
|
set_radio(value)
|
||||||
native['checked'] = 'checked'
|
elsif checkbox?
|
||||||
elsif tag_name == 'input' and type == 'checkbox'
|
set_checkbox(value)
|
||||||
if value && !native['checked']
|
elsif input_field?
|
||||||
native['checked'] = 'checked'
|
set_input(value)
|
||||||
elsif !value && native['checked']
|
elsif textarea?
|
||||||
native.remove_attribute('checked')
|
|
||||||
end
|
|
||||||
elsif tag_name == 'input'
|
|
||||||
if (type == 'text' || type == 'password') && self[:maxlength] &&
|
|
||||||
!self[:maxlength].empty?
|
|
||||||
# Browser behavior for maxlength="0" is inconsistent, so we stick with
|
|
||||||
# Firefox, allowing no input
|
|
||||||
value = value[0...self[:maxlength].to_i]
|
|
||||||
end
|
|
||||||
if Array === value #Assert multiple attribute is present
|
|
||||||
value.each do |v|
|
|
||||||
new_native = native.clone
|
|
||||||
new_native.remove_attribute('value')
|
|
||||||
native.add_next_sibling(new_native)
|
|
||||||
new_native['value'] = v.to_s
|
|
||||||
end
|
|
||||||
native.remove
|
|
||||||
else
|
|
||||||
native['value'] = value.to_s
|
|
||||||
end
|
|
||||||
elsif tag_name == "textarea"
|
|
||||||
native.content = value.to_s
|
native.content = value.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -135,4 +114,61 @@ private
|
||||||
def form
|
def form
|
||||||
native.ancestors('form').first
|
native.ancestors('form').first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_radio(value)
|
||||||
|
other_radios_xpath = XPath.generate { |x| x.anywhere(:input)[x.attr(:name).equals(self[:name])] }.to_s
|
||||||
|
driver.dom.xpath(other_radios_xpath).each { |node| node.remove_attribute("checked") }
|
||||||
|
native['checked'] = 'checked'
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_checkbox(value)
|
||||||
|
if value && !native['checked']
|
||||||
|
native['checked'] = 'checked'
|
||||||
|
elsif !value && native['checked']
|
||||||
|
native.remove_attribute('checked')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_input(value)
|
||||||
|
if text_or_password? && attribute_is_not_blank?(:maxlength)
|
||||||
|
# Browser behavior for maxlength="0" is inconsistent, so we stick with
|
||||||
|
# Firefox, allowing no input
|
||||||
|
value = value[0...self[:maxlength].to_i]
|
||||||
|
end
|
||||||
|
if Array === value #Assert multiple attribute is present
|
||||||
|
value.each do |v|
|
||||||
|
new_native = native.clone
|
||||||
|
new_native.remove_attribute('value')
|
||||||
|
native.add_next_sibling(new_native)
|
||||||
|
new_native['value'] = v.to_s
|
||||||
|
end
|
||||||
|
native.remove
|
||||||
|
else
|
||||||
|
native['value'] = value.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute_is_not_blank?(attribute)
|
||||||
|
self[attribute] && !self[attribute].empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def checkbox?
|
||||||
|
input_field? && type == 'checkbox'
|
||||||
|
end
|
||||||
|
|
||||||
|
def input_field?
|
||||||
|
tag_name == 'input'
|
||||||
|
end
|
||||||
|
|
||||||
|
def radio?
|
||||||
|
input_field? && type == 'radio'
|
||||||
|
end
|
||||||
|
|
||||||
|
def textarea?
|
||||||
|
tag_name == "textarea"
|
||||||
|
end
|
||||||
|
|
||||||
|
def text_or_password?
|
||||||
|
input_field? && (type == 'text' || type == 'password')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue