123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Node::Simple

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/capybara/node/simple.rb,
lib/capybara/minitest/spec.rb

Overview

A Simple is a simpler version of Base which includes only Finders and Matchers and does not include 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 Capybara.HTML.

Constant Summary

  • VISIBILITY_XPATH =
    # File 'lib/capybara/node/simple.rb', line 206
    XPath.generate do |x|
      x.ancestor_or_self[
        x.attr(:style)[x.contains('display:none') | x.contains('display: none')] |
        x.attr(:hidden) |
        x.qname.one_of('script', 'head') |
        (~x.self(:summary) & XPath.parent(:details)[!XPath.attr(:open)])
      ].boolean
    end.to_s.freeze

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Minitest::Expectations - Included

#must_have_all_of_selectors
#must_have_ancestor
#must_have_any_of_selectors
#must_have_button
#must_have_checked_field
#must_have_content
#must_have_css
#must_have_current_path
#must_have_element
#must_have_field
#must_have_link
#must_have_none_of_selectors
#must_have_select
#must_have_selector
#must_have_sibling
#must_have_style,
#must_have_table
#must_have_text
#must_have_title
#must_have_unchecked_field
#must_have_xpath
#must_match_style
#wont_have_button
#wont_have_checked_field
#wont_have_content
#wont_have_css
#wont_have_current_path
#wont_have_element
#wont_have_field
#wont_have_link
#wont_have_select
#wont_have_selector
#wont_have_table
#wont_have_text
#wont_have_title
#wont_have_unchecked_field
#wont_have_xpath

DocumentMatchers - Included

#assert_no_title

Asserts that the page doesn’t have the given title.

#assert_title

Asserts that the page has the given title.

#has_no_title?

Checks if the page doesn’t have the given title.

#has_title?

Checks if the page has the given title.

#_verify_title

Matchers - Included

#==,
#assert_all_of_selectors

Asserts that all of the provided selectors are present on the given page or descendants of the current node.

#assert_ancestor

Asserts that a given selector matches an ancestor of the current node.

#assert_any_of_selectors

Asserts that any of the provided selectors are present on the given page or descendants of the current node.

#assert_matches_selector

Asserts that the current node matches a given selector.

#assert_matches_style

Asserts that an element has the specified CSS styles.

#assert_no_ancestor,
#assert_no_selector

Asserts that a given selector is not on the page or a descendant of the current node.

#assert_no_sibling,
#assert_no_text

Asserts that the page or current node doesn’t have the given text content, ignoring any Capybara.HTML tags.

#assert_none_of_selectors

Asserts that none of the provided selectors are present on the given page or descendants of the current node.

#assert_not_matches_selector

Asserts that the current node does not match a given selector.

#assert_selector

Asserts that a given selector is on the page or a descendant of the current node.

#assert_sibling

Asserts that a given selector matches a sibling of the current node.

#assert_style,
#assert_text

Asserts that the page or current node has the given text content, ignoring any Capybara.HTML tags.

#has_ancestor?

Predicate version of #assert_ancestor

#has_button?

Checks if the page or current node has a button with the given text, value or id.

#has_checked_field?

Checks if the page or current node has a radio button or checkbox with the given label, value, id, or test_id attribute that is currently checked.

#has_content?
#has_css?

Checks if a given CSS selector is on the page or a descendant of the current node.

#has_element?

Checks if the page or current node has a element with the given local name.

#has_field?

Checks if the page or current node has a form field with the given label, name or id.

#has_link?

Checks if the page or current node has a link with the given text or id.

#has_no_ancestor?

Predicate version of #assert_no_ancestor

#has_no_button?

Checks if the page or current node has no button with the given text, value or id.

#has_no_checked_field?

Checks if the page or current node has no radio button or checkbox with the given label, value or id, or test_id attribute that is currently checked.

#has_no_content?
#has_no_css?

Checks if a given CSS selector is not on the page or a descendant of the current node.

#has_no_element?

Checks if the page or current node has no element with the given local name.

#has_no_field?

Checks if the page or current node has no form field with the given label, name or id.

#has_no_link?

Checks if the page or current node has no link with the given text or id.

#has_no_select?

Checks if the page or current node has no select field with the given label, name or id.

#has_no_selector?

Checks if a given selector is not on the page or a descendant of the current node.

#has_no_sibling?

Predicate version of #assert_no_sibling

#has_no_table?

Checks if the page or current node has no table with the given id or caption.

#has_no_text?

Checks if the page or current node does not have the given text content, ignoring any Capybara.HTML tags and normalizing whitespace.

#has_no_unchecked_field?

Checks if the page or current node has no radio button or checkbox with the given label, value or id, or test_id attribute that is currently unchecked.

#has_no_xpath?

Checks if a given ::XPath expression is not on the page or a descendant of the current node.

#has_select?

Checks if the page or current node has a select field with the given label, name or id.

#has_selector?

Checks if a given selector is on the page or a descendant of the current node.

#has_sibling?

Predicate version of #assert_sibling

#has_style?,
#has_table?

Checks if the page or current node has a table with the given id or caption:

#has_text?

Checks if the page or current node has the given text content, ignoring any Capybara.HTML tags.

#has_unchecked_field?

Checks if the page or current node has a radio button or checkbox with the given label, value or id, or test_id attribute that is currently unchecked.

#has_xpath?

Checks if a given ::XPath expression is on the page or a descendant of the current node.

#matches_css?

Checks if the current node matches given CSS selector.

#matches_selector?

Checks if the current node matches given selector.

#matches_style?

Checks if a an element has the specified CSS styles.

#matches_xpath?

Checks if the current node matches given ::XPath expression.

#not_matches_css?

Checks if the current node does not match given CSS selector.

#not_matches_selector?

Checks if the current node does not match given selector.

#not_matches_xpath?

Checks if the current node does not match given ::XPath expression.

#_set_query_session_options, #_verify_match_result, #_verify_multiple, #_verify_selector_result, #_verify_text, #extract_selector, #make_predicate

Finders - Included

#all

Find all elements on the page matching the given selector and options.

#ancestor

Find an Element based on the given arguments that is also an ancestor of the element called on.

#find

Find an Element based on the given arguments.

#find_all

Alias for Finders#all.

#find_button

Find a button on the page.

#find_by_id

Find a element on the page, given its id.

#find_field

Find a form field on the page.

#find_link

Find a link on the page.

#first

Find the first element on the page matching the given selector and options.

#sibling

Find an Element based on the given arguments that is also a sibling of the element called on.

#ambiguous?, #options_include_minimum?, #parent, #prefer_exact?, #synced_resolve

Constructor Details

.new(native) ⇒ Simple

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 22

def initialize(native)
  native = Capybara::HTML(native) if native.is_a?(String)
  @native = native
end

Instance Attribute Details

#checked?Boolean (readonly)

Whether or not the element is checked.

Returns:

  • (Boolean)

    Whether the element is checked

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 123

def checked?
  native.has_attribute?('checked')
end

#disabled?Boolean (readonly)

Whether or not the element is disabled.

Returns:

  • (Boolean)

    Whether the element is disabled

[ GitHub ]

  
# 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

#multiple?Boolean (readonly)

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 147

def multiple?
  native.has_attribute?('multiple')
end

#native (readonly)

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 20

attr_reader :native

#readonly?Boolean (readonly)

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 151

def readonly?
  native.has_attribute?('readonly')
end

#selected?Boolean (readonly)

Whether or not the element is selected.

Returns:

  • (Boolean)

    Whether the element is selected

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 143

def selected?
  native.has_attribute?('selected')
end

Instance Method Details

#==(other)

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 194

def ==(other)
  eql?(other) || (other.respond_to?(:native) && native == other.native)
end

#[](name) ⇒ String

Retrieve the given attribute

element[:title] # => HTML title attribute

Parameters:

  • name (Symbol)

    The attribute name to retrieve

Returns:

  • (String)

    The value of the attribute

[ GitHub ]

  
# 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!

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 159

def allow_reload!(*)
  # no op
end

#find_css(css, **_options)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 175

def find_css(css, **_options)
  native.css(css)
end

#find_xpath(xpath, **_options)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 180

def find_xpath(xpath, **_options)
  native.xpath(xpath)
end

#initial_cache

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 190

def initial_cache
  {}
end

#inspect

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 170

def inspect
  %(#<Capybara::Node::Simple tag="#{tag_name}" path="#{path}">)
end

#option_value(option) (private)

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 200

def option_value(option)
  return nil if option.nil?

  option[:value] || option.content
end

#pathString

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

Returns:

  • (String)

    An XPath expression

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 70

def path
  native.path
end

#session_options

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 185

def session_options
  Capybara.session_options
end

#synchronize(_seconds = nil)

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 155

def synchronize(_seconds = nil)
  yield # simple nodes don't need to wait
end

#tag_nameString

Returns:

  • (String)

    The tag name of the element

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 60

def tag_name
  native.node_name
end

#text(_type = nil, normalize_ws: false) ⇒ String

Returns:

  • (String)

    The text of the element

[ GitHub ]

  
# 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

#titleString

Returns:

  • (String)

    The title of the document

[ GitHub ]

  
# File 'lib/capybara/node/simple.rb', line 166

def title
  native.title
end

#valueString

Returns:

  • (String)

    The value of the form element

[ GitHub ]

  
# 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) ⇒ Boolean

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

Parameters:

  • check_ancestors (Boolean) (defaults to: true)

    Whether to inherit visibility from ancestors

Returns:

  • (Boolean)

    Whether the element is visible

[ GitHub ]

  
# 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