class Capybara::Node::Simple

A {Capybara::Node::Simple} is a simpler version of {Capybara::Node::Base} which includes only {Capybara::Node::Finders} and {Capybara::Node::Matchers} and does not include {Capybara::Node::Actions}. This type of node is returned when using {Capybara.string}.

It is useful in that it does not require a session, an application or a driver, but can still use Capybara’s finders and matchers on any string that contains HTML.

Constants

VISIBILITY_XPATH

Attributes

native[R]

Public Class Methods

new(native) click to toggle source
# File lib/capybara/node/simple.rb, line 22
def initialize(native)
  native = Capybara::HTML(native) if native.is_a?(String)
  @native = native
end

Public Instance Methods

==(other) click to toggle source
# File lib/capybara/node/simple.rb, line 194
def ==(other)
  eql?(other) || (other.respond_to?(:native) && native == other.native)
end
[](name) click to toggle source

Retrieve the given attribute

element[:title] # => HTML title attribute

@param [Symbol] name The attribute name to retrieve @return [String] The value of the attribute

# File lib/capybara/node/simple.rb, line 45
def [](name)
  attr_name = name.to_s
  if attr_name == 'value'
    value
  elsif (tag_name == 'input') && (native[:type] == 'checkbox') && (attr_name == 'checked')
    native['checked'] == 'checked'
  else
    native[attr_name]
  end
end
allow_reload!(*) click to toggle source
# File lib/capybara/node/simple.rb, line 159
def allow_reload!(*)
  # no op
end
checked?() click to toggle source

Whether or not the element is checked.

@return [Boolean] Whether the element is checked

# File lib/capybara/node/simple.rb, line 123
def checked?
  native.has_attribute?('checked')
end
disabled?() click to toggle source

Whether or not the element is disabled.

@return [Boolean] Whether the element is disabled

# File lib/capybara/node/simple.rb, line 132
def disabled?
  native.has_attribute?('disabled') &&
    %w[button input select textarea optgroup option menuitem fieldset].include?(tag_name)
end
find_css(css, **_options) click to toggle source

@api private

# File lib/capybara/node/simple.rb, line 175
def find_css(css, **_options)
  native.css(css)
end
find_xpath(xpath, **_options) click to toggle source

@api private

# File lib/capybara/node/simple.rb, line 180
def find_xpath(xpath, **_options)
  native.xpath(xpath)
end
initial_cache() click to toggle source

@api private

# File lib/capybara/node/simple.rb, line 190
def initial_cache
  {}
end
inspect() click to toggle source
# File lib/capybara/node/simple.rb, line 170
def inspect
  %(#<Capybara::Node::Simple tag="#{tag_name}" path="#{path}">)
end
multiple?() click to toggle source
# File lib/capybara/node/simple.rb, line 147
def multiple?
  native.has_attribute?('multiple')
end
path() click to toggle source

An XPath expression describing where on the page the element can be found

@return [String] An XPath expression

# File lib/capybara/node/simple.rb, line 70
def path
  native.path
end
readonly?() click to toggle source
# File lib/capybara/node/simple.rb, line 151
def readonly?
  native.has_attribute?('readonly')
end
selected?() click to toggle source

Whether or not the element is selected.

@return [Boolean] Whether the element is selected

# File lib/capybara/node/simple.rb, line 143
def selected?
  native.has_attribute?('selected')
end
session_options() click to toggle source

@api private

# File lib/capybara/node/simple.rb, line 185
def session_options
  Capybara.session_options
end
synchronize(_seconds = nil) { || ... } click to toggle source
# File lib/capybara/node/simple.rb, line 155
def synchronize(_seconds = nil)
  yield # simple nodes don't need to wait
end
tag_name() click to toggle source

@return [String] The tag name of the element

# File lib/capybara/node/simple.rb, line 60
def tag_name
  native.node_name
end
text(_type = nil, normalize_ws: false) click to toggle source

@return [String] The text of the element

# File lib/capybara/node/simple.rb, line 31
def text(_type = nil, normalize_ws: false)
  txt = native.text
  normalize_ws ? txt.gsub(/[[:space:]]+/, ' ').strip : txt
end
title() click to toggle source

@return [String] The title of the document

# File lib/capybara/node/simple.rb, line 166
def title
  native.title
end
value() click to toggle source

@return [String] The value of the form element

# File lib/capybara/node/simple.rb, line 78
def value
  if tag_name == 'textarea'
    native['_capybara_raw_value']
  elsif tag_name == 'select'
    selected_options = find_xpath('.//option[@selected]')
    if multiple?
      selected_options.map(&method(:option_value))
    else
      option_value(selected_options.first || find_xpath('.//option').first)
    end
  elsif tag_name == 'input' && %w[radio checkbox].include?(native[:type])
    native[:value] || 'on'
  else
    native[:value]
  end
end
visible?(check_ancestors = true) click to toggle source

Whether or not the element is visible. Does not support CSS, so the result may be inaccurate.

@param [Boolean] check_ancestors Whether to inherit visibility from ancestors @return [Boolean] Whether the element is visible

# File lib/capybara/node/simple.rb, line 103
def visible?(check_ancestors = true) # rubocop:disable Style/OptionalBooleanParameter
  return false if (tag_name == 'input') && (native[:type] == 'hidden')
  return false if tag_name == 'template'

  if check_ancestors
    !find_xpath(VISIBILITY_XPATH)
  else
    # No need for an xpath if only checking the current element
    !(native.key?('hidden') ||
      /display:\s?none/.match?(native[:style] || '') ||
      %w[script head style].include?(tag_name))
  end
end

Private Instance Methods

option_value(option) click to toggle source
# File lib/capybara/node/simple.rb, line 200
def option_value(option)
  return nil if option.nil?

  option[:value] || option.content
end