class Capybara::RackTest::Node
Public Instance Methods
==(other)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 115 def ==(other) native == other.native end
[](name)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 10 def [](name) string_node[name] end
all_text()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 2 def all_text Capybara::Helpers.normalize_whitespace(native.text) end
checked?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 87 def checked? string_node.checked? end
click()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 55 def click if tag_name == 'a' && !self[:href].nil? method = self["data-method"] if driver.options[:respect_data_method] method ||= :get driver.follow(method, self[:href].to_s) elsif (tag_name == 'input' and %w(submit image).include?(type)) or ((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 else find_xpath(".//input").first end if labelled_control && (labelled_control.checkbox? || labelled_control.radio?) labelled_control.set(!labelled_control.checked?) end end end
disabled?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 95 def disabled? if %w(option optgroup).include? tag_name string_node.disabled? || find_xpath("parent::*[self::optgroup or self::select]")[0].disabled? else string_node.disabled? || !find_xpath("parent::fieldset[@disabled] | ancestor::*[not(self::legend) or preceding-sibling::legend][parent::fieldset[@disabled]]").empty? end end
find_css(locator)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 111 def find_css(locator) native.css(locator, Capybara::RackTest::CSSHandlers.new).map { |n| self.class.new(driver, n) } end
find_xpath(locator)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 107 def find_xpath(locator) native.xpath(locator).map { |n| self.class.new(driver, n) } end
path()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 103 def path native.path end
select_option()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 40 def select_option return if disabled? if select_node['multiple'] != 'multiple' select_node.find_xpath(".//option[@selected]").each { |node| node.native.remove_attribute("selected") } end native["selected"] = 'selected' end
selected?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 91 def selected? string_node.selected? end
set(value)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 18 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 if radio? set_radio(value) elsif checkbox? set_checkbox(value) elsif input_field? set_input(value) elsif textarea? native['_capybara_raw_value'] = value.to_s end end
tag_name()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 79 def tag_name native.node_name end
unselect_option()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 48 def unselect_option if select_node['multiple'] != 'multiple' raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box." end native.remove_attribute('selected') end
value()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 14 def value string_node.value end
visible?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 83 def visible? string_node.visible? end
visible_text()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 6 def visible_text Capybara::Helpers.normalize_whitespace(unnormalized_text) end
Protected Instance Methods
checkbox?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 201 def checkbox? input_field? && type == 'checkbox' end
input_field?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 205 def input_field? tag_name == 'input' end
radio?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 209 def radio? input_field? && type == 'radio' end
text_or_password?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 217 def text_or_password? input_field? && (type == 'text' || type == 'password') end
textarea?()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 213 def textarea? tag_name == "textarea" end
unnormalized_text(check_ancestor_visibility = true)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 121 def unnormalized_text(check_ancestor_visibility = true) if !string_node.visible?(check_ancestor_visibility) '' elsif native.text? native.text elsif native.element? native.children.map do |child| Capybara::RackTest::Node.new(driver, child).unnormalized_text(false) end.join else '' end end
Private Instance Methods
attribute_is_not_blank?(attribute)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 195 def attribute_is_not_blank?(attribute) self[attribute] && !self[attribute].empty? end
form()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 150 def form if native[:form] native.xpath("//form[@id='#{native[:form]}']").first else native.ancestors('form').first end end
select_node()
click to toggle source
a reference to the select node if this is an option node
# File lib/capybara/rack_test/node.rb, line 142 def select_node find_xpath('./ancestor::select').first end
set_checkbox(value)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 164 def set_checkbox(value) if value && !native['checked'] native['checked'] = 'checked' elsif !value && native['checked'] native.remove_attribute('checked') end end
set_input(value)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 172 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.to_s[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 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 end end end
set_radio(_value)
click to toggle source
# File lib/capybara/rack_test/node.rb, line 158 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
string_node()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 137 def string_node @string_node ||= Capybara::Node::Simple.new(native) end
type()
click to toggle source
# File lib/capybara/rack_test/node.rb, line 146 def type native[:type] end