class Capybara::RackTest::Node
def ==(other)
def ==(other) native == other.native end
def [](name)
def [](name) string_node[name] end
def all_text
def all_text native.text .gsub(/[\u200b\u200e\u200f]/, '') .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ') .gsub(/\A[[:space:]&&[^\u00a0]]+/, "") .gsub(/[[:space:]&&[^\u00a0]]+\z/, "") .tr("\u00a0", ' ') end
def attribute_is_not_blank?(attribute)
def attribute_is_not_blank?(attribute) self[attribute] && !self[attribute].empty? end
def checkable?
def checkable? tag_name == 'input' and %w[checkbox radio].include?(type) end
def checkbox?
def checkbox? input_field? && type == 'checkbox' end
def checked?
def checked? string_node.checked? end
def click(keys = [], offset = {})
def click(keys = [], offset = {}) raise ArgumentError, "The RackTest driver does not support click options" unless keys.empty? && offset.empty? if link? follow_link elsif submits? associated_form = form Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_form elsif checkable? set(!checked?) elsif tag_name == 'label' click_label end end
def click_label
def click_label labelled_control = if native[:for] find_xpath("//input[@id='#{native[:for]}']") else find_xpath(".//input") end.first if labelled_control && (labelled_control.checkbox? || labelled_control.radio?) labelled_control.set(!labelled_control.checked?) end end
def deselect_options
def deselect_options select_node.find_xpath(".//option[@selected]").each { |node| node.native.remove_attribute("selected") } end
def disabled?
def disabled? return true if string_node.disabled? if %w[option optgroup].include? tag_name find_xpath("parent::*[self::optgroup or self::select]")[0].disabled? else !find_xpath("parent::fieldset[@disabled] | ancestor::*[not(self::legend) or preceding-sibling::legend][parent::fieldset[@disabled]]").empty? end end
def displayed_text(check_ancestor: true)
- Api: - private
def displayed_text(check_ancestor: true) if !string_node.visible?(check_ancestor) '' elsif native.text? native.text .gsub(/[\u200b\u200e\u200f]/, '') .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ') elsif native.element? text = native.children.map do |child| Capybara::RackTest::Node.new(driver, child).displayed_text(check_ancestor: false) end.join || '' text = "\n#{text}\n" if BLOCK_ELEMENTS.include?(tag_name) text else '' end end
def find_css(locator)
def find_css(locator) native.css(locator, Capybara::RackTest::CSSHandlers.new).map { |n| self.class.new(driver, n) } end
def find_xpath(locator)
def find_xpath(locator) native.xpath(locator).map { |n| self.class.new(driver, n) } end
def follow_link
def follow_link method = self["data-method"] if driver.options[:respect_data_method] method ||= :get driver.follow(method, self[:href].to_s) end
def form
def form if native[:form] native.xpath("//form[@id='#{native[:form]}']") else native.ancestors('form') end.first end
def input_field?
def input_field? tag_name == 'input' end
def link?
def link? tag_name == 'a' && !self[:href].nil? end
def path
def path native.path end
def radio?
def radio? input_field? && type == 'radio' end
def select_node
def select_node find_xpath('./ancestor::select[1]').first end
def select_option
def select_option return if disabled? deselect_options unless select_node.multiple? native["selected"] = 'selected' end
def selected?
def selected? string_node.selected? end
def set(value, **options)
def set(value, **options) return if disabled? || readonly? warn "Options passed to Node#set but the RackTest driver doesn't support any - ignoring" unless options.empty? if value.is_a?(Array) && !multiple? raise TypeError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}" end if radio? then set_radio(value) elsif checkbox? then set_checkbox(value) elsif input_field? then set_input(value) elsif textarea? then native['_capybara_raw_value'] = value.to_s end end
def set_checkbox(value) # rubocop:disable Naming/AccessorMethodName
def set_checkbox(value) # rubocop:disable Naming/AccessorMethodName if value && !native['checked'] native['checked'] = 'checked' elsif !value && native['checked'] native.remove_attribute('checked') end end
def set_input(value) # rubocop:disable Naming/AccessorMethodName
def set_input(value) # rubocop:disable Naming/AccessorMethodName 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 value.is_a?(Array) # 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 set_radio(_value) # rubocop:disable Naming/AccessorMethodName
def set_radio(_value) # rubocop:disable Naming/AccessorMethodName other_radios_xpath = XPath.generate { |x| x.anywhere(:input)[x.attr(:name) == self[:name]] }.to_s driver.dom.xpath(other_radios_xpath).each { |node| node.remove_attribute("checked") } native['checked'] = 'checked' end
def string_node
def string_node @string_node ||= Capybara::Node::Simple.new(native) end
def submits?
def submits? (tag_name == 'input' and %w[submit image].include?(type)) || (tag_name == 'button' and [nil, "submit"].include?(type)) end
def tag_name
def tag_name native.node_name end
def text_or_password?
def text_or_password? input_field? && (type == 'text' || type == 'password') end
def textarea?
def textarea? tag_name == "textarea" end
def type
def type native[:type] end
def unselect_option
def unselect_option raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box." unless select_node.multiple? native.remove_attribute('selected') end
def value
def value string_node.value end
def visible?
def visible? string_node.visible? end
def visible_text
def visible_text displayed_text.gsub(/\ +/, ' ') .gsub(/[\ \n]*\n[\ \n]*/, "\n") .gsub(/\A[[:space:]&&[^\u00a0]]+/, "") .gsub(/[[:space:]&&[^\u00a0]]+\z/, "") .tr("\u00a0", ' ') end