class Capybara::RackTest::Node

Constants

BLOCK_ELEMENTS
DISABLED_BY_FIELDSET_XPATH
OPTION_OWNER_XPATH

Public Instance Methods

[](name) click to toggle source
# File lib/capybara/rack_test/node.rb, line 25
def [](name)
  string_node[name]
end
all_text() click to toggle source
# File lib/capybara/rack_test/node.rb, line 8
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
checked?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 93
def checked?
  string_node.checked?
end
click(keys = [], **options) click to toggle source
# File lib/capybara/rack_test/node.rb, line 67
def click(keys = [], **options)
  options.delete(:offset)
  raise ArgumentError, 'The RackTest driver does not support click options' unless keys.empty? && options.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
  elsif (details = native.xpath('.//ancestor-or-self::details').last)
    toggle_details(details)
  end
end
disabled?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 101
def disabled?
  return true if string_node.disabled?

  if %w[option optgroup].include? tag_name
    find_xpath(OPTION_OWNER_XPATH)[0].disabled?
  else
    !find_xpath(DISABLED_BY_FIELDSET_XPATH).empty?
  end
end
find_css(locator, **_hints) click to toggle source
# File lib/capybara/rack_test/node.rb, line 126
def find_css(locator, **_hints)
  native.css(locator, Capybara::RackTest::CSSHandlers.new).map { |el| self.class.new(driver, el) }
end
find_xpath(locator, **_hints) click to toggle source
# File lib/capybara/rack_test/node.rb, line 122
def find_xpath(locator, **_hints)
  native.xpath(locator).map { |el| self.class.new(driver, el) }
end
path() click to toggle source
# File lib/capybara/rack_test/node.rb, line 118
def path
  native.path
end
readonly?() click to toggle source
Calls superclass method Capybara::Driver::Node#readonly?
# File lib/capybara/rack_test/node.rb, line 111
def readonly?
  # readonly attribute not valid on these input types
  return false if input_field? && %w[hidden range color checkbox radio file submit image reset button].include?(type)

  super
end
select_option() click to toggle source
# File lib/capybara/rack_test/node.rb, line 54
def select_option
  return if disabled?

  deselect_options unless select_node.multiple?
  native['selected'] = 'selected'
end
selected?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 97
def selected?
  string_node.selected?
end
set(value, **options) click to toggle source
# File lib/capybara/rack_test/node.rb, line 37
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 range? then set_range(value)
  elsif input_field? then set_input(value)
  elsif textarea? then native['_capybara_raw_value'] = value.to_s
  end
end
style(_styles) click to toggle source
# File lib/capybara/rack_test/node.rb, line 29
def style(_styles)
  raise NotImplementedError, 'The rack_test driver does not process CSS'
end
tag_name() click to toggle source
# File lib/capybara/rack_test/node.rb, line 85
def tag_name
  native.node_name
end
unselect_option() click to toggle source
# File lib/capybara/rack_test/node.rb, line 61
def unselect_option
  raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple?

  native.remove_attribute('selected')
end
value() click to toggle source
# File lib/capybara/rack_test/node.rb, line 33
def value
  string_node.value
end
visible?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 89
def visible?
  string_node.visible?
end
visible_text() click to toggle source
# File lib/capybara/rack_test/node.rb, line 17
def visible_text
  displayed_text.squeeze(' ')
                .gsub(/[\ \n]*\n[\ \n]*/, "\n")
                .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
                .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
                .tr("\u00a0", ' ')
end

Protected Instance Methods

checkbox?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 291
def checkbox?
  input_field? && type == 'checkbox'
end
checkbox_or_radio?(field = self) click to toggle source
# File lib/capybara/rack_test/node.rb, line 287
def checkbox_or_radio?(field = self)
  field&.checkbox? || field&.radio?
end
displayed_text(check_ancestor: true) click to toggle source

@api private

# File lib/capybara/rack_test/node.rb, line 152
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 # rubocop:disable Lint/DuplicateBranch
    ''
  end
end
input_field?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 303
def input_field?
  tag_name == 'input'
end
radio?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 295
def radio?
  input_field? && type == 'radio'
end
range?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 311
def range?
  input_field? && type == 'range'
end
text_or_password?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 299
def text_or_password?
  input_field? && (type == 'text' || type == 'password')
end
textarea?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 307
def textarea?
  tag_name == 'textarea'
end

Private Instance Methods

attribute_is_not_blank?(attribute) click to toggle source
# File lib/capybara/rack_test/node.rb, line 242
def attribute_is_not_blank?(attribute)
  self[attribute] && !self[attribute].empty?
end
checkable?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 281
def checkable?
  tag_name == 'input' && %w[checkbox radio].include?(type)
end
click_label() click to toggle source
# File lib/capybara/rack_test/node.rb, line 252
def click_label
  labelled_control = if native[:for]
    find_xpath("//input[@id='#{native[:for]}']")
  else
    find_xpath('.//input')
  end.first

  labelled_control.set(!labelled_control.checked?) if checkbox_or_radio?(labelled_control)
end
deselect_options() click to toggle source
# File lib/capybara/rack_test/node.rb, line 176
def deselect_options
  select_node.find_xpath('.//option[@selected]').each { |node| node.native.remove_attribute('selected') }
end
form() click to toggle source
# File lib/capybara/rack_test/node.rb, line 193
def form
  if native[:form]
    native.xpath("//form[@id='#{native[:form]}']")
  else
    native.ancestors('form')
  end.first
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 185
def select_node
  find_xpath('./ancestor::select[1]').first
end
set_checkbox(value) click to toggle source
# File lib/capybara/rack_test/node.rb, line 207
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
set_input(value) click to toggle source
# File lib/capybara/rack_test/node.rb, line 223
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 |val|
      new_native = native.clone
      new_native.remove_attribute('value')
      native.add_next_sibling(new_native)
      new_native['value'] = val.to_s
    end
    native.remove
  else
    native['value'] = value.to_s
  end
end
set_radio(_value) click to toggle source
# File lib/capybara/rack_test/node.rb, line 201
def set_radio(_value) # rubocop:disable Naming/AccessorMethodName
  other_radios_xpath = XPath.generate { |xp| xp.anywhere(:input)[xp.attr(:name) == self[:name]] }.to_s
  driver.dom.xpath(other_radios_xpath).each { |node| node.remove_attribute('checked') }
  native['checked'] = 'checked'
end
set_range(value) click to toggle source
# File lib/capybara/rack_test/node.rb, line 215
def set_range(value) # rubocop:disable Naming/AccessorMethodName
  min, max, step = (native['min'] || 0).to_f, (native['max'] || 100).to_f, (native['step'] || 1).to_f
  value = value.to_f
  value = value.clamp(min, max)
  value = (((value - min) / step).round * step) + min
  native['value'] = value.clamp(min, max)
end
stale_check() click to toggle source
# File lib/capybara/rack_test/node.rb, line 172
def stale_check
  raise Capybara::RackTest::Errors::StaleElementReferenceError unless native.document == driver.dom
end
string_node() click to toggle source
# File lib/capybara/rack_test/node.rb, line 180
def string_node
  @string_node ||= Capybara::Node::Simple.new(native)
end
submits?() click to toggle source
# File lib/capybara/rack_test/node.rb, line 277
def submits?
  (tag_name == 'input' && %w[submit image].include?(type)) || (tag_name == 'button' && [nil, 'submit'].include?(type))
end
toggle_details(details = nil) click to toggle source
# File lib/capybara/rack_test/node.rb, line 262
def toggle_details(details = nil)
  details ||= native.xpath('.//ancestor-or-self::details').last
  return unless details

  if details.has_attribute?('open')
    details.remove_attribute('open')
  else
    details.set_attribute('open', 'open')
  end
end
type() click to toggle source
# File lib/capybara/rack_test/node.rb, line 189
def type
  native[:type]
end