123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Node::Base

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

Overview

A Base represents either an element on a page through the subclass Element or a document through Document.

Both types of Node share the same methods, used for interacting with the elements on the page. These methods are divided into three categories, finders, actions and matchers. These are found in the modules Finders, Actions and Matchers respectively.

A Session exposes all methods from Document directly:

session = Capybara::Session.new(:rack_test, my_app)
session.visit('/')
session.fill_in('Foo', with: 'Bar')    # from Capybara::Node::Actions
bar = session.find('#bar')                # from Capybara::Node::Finders
bar.select('Baz', from: 'Quox')        # from Capybara::Node::Actions
session.has_css?('#foobar')               # from Capybara::Node::Matchers

Constant Summary

Actions - Included

CAPTURE_FILE_ELEMENT_SCRIPT, DATALIST_OPTIONS_SCRIPT, RESET_STYLE_SCRIPT, UPDATE_STYLE_SCRIPT

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

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

Actions - Included

#attach_file

Find a descendant file field on the page and attach a file given its path.

#check

Find a descendant check box and mark it as checked.

#choose

Find a descendant radio button and mark it as checked.

#click_button

Finds a button on the page and clicks it.

#click_link

Finds a link by id, test_id attribute, text or title and clicks it.

#click_link_or_button

Finds a button or link and clicks it.

#click_on
#fill_in

Locate a text field or text area and fill it in with the given text.

#select

If from option is present, #select finds a select box, or text input with associated datalist, on the page and selects a particular option from it.

#uncheck

Find a descendant check box and uncheck it.

#unselect

Find a select box on the page and unselect a particular option from it.

#_check_with_label, #_reset_style, #_update_style, #find_select_or_datalist_input, #select_datalist_option, #while_visible

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(session, base) ⇒ Base

[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 32

def initialize(session, base)
  @session = session
  @base = base
end

Instance Attribute Details

#base (readonly)

[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 26

attr_reader :session, :base, :query_scope

#query_scope (readonly)

[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 26

attr_reader :session, :base, :query_scope

#session (readonly)

[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 26

attr_reader :session, :base, :query_scope

Instance Method Details

#find_css(css, **options)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 106

def find_css(css, **options)
  if base.method(:find_css).arity == 1
    base.find_css(css)
  else
    base.find_css(css, **options)
  end
end

#find_xpath(xpath, **options)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 115

def find_xpath(xpath, **options)
  if base.method(:find_xpath).arity == 1
    base.find_xpath(xpath)
  else
    base.find_xpath(xpath, **options)
  end
end

#reload

overridden in subclasses, e.g. Element

[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 38

def reload
  self
end

#session_options

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 124

def session_options
  session.config
end

#synchronize(seconds = nil, errors: nil) ⇒ Object

This method is Capybara’s primary defence against asynchronicity problems. It works by attempting to run a given block of code until it succeeds. The exact behaviour of this method depends on a number of factors. Basically there are certain exceptions which, when raised from the block, instead of bubbling up, are caught, and the block is re-run.

Certain drivers, such as RackTest, have no support for asynchronous processes, these drivers run the block, and any error raised bubbles up immediately. This allows faster turn around in the case where an expectation fails.

Only exceptions that are ElementNotFound or any subclass thereof cause the block to be rerun. Drivers may specify additional exceptions which also cause reruns. This usually occurs when a node is manipulated which no longer exists on the page. For example, the Selenium driver specifies Selenium::WebDriver::Error::ObsoleteElementError.

As long as any of these exceptions are thrown, the block is re-run, until a certain amount of time passes. The amount of time defaults to Capybara.default_max_wait_time and can be overridden through the seconds argument. This time is compared with the system time to see how much time has passed. On rubies/platforms which don’t support access to a monotonic process clock if the return value of Time.now is stubbed out, ::Capybara will raise FrozenInTime.

Parameters:

  • seconds (Integer) (defaults to: nil)

    (current sessions default_max_wait_time) Maximum number of seconds to retry this block

  • errors (Array<Exception>)

    (driver.invalid_element_errors + [Capybara::ElementNotFound]) exception types that cause the block to be rerun

Returns:

  • (Object)

    The result of the given block

Raises:

[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 76

def synchronize(seconds = nil, errors: nil)
  return yield if session.synchronized

  seconds = session_options.default_max_wait_time if [nil, true].include? seconds
  interval = session_options.default_retry_interval
  session.synchronized = true
  timer = Capybara::Helpers.timer(expire_in: seconds)
  begin
    yield
  rescue StandardError => e
    session.raise_server_error!
    raise e unless catch_error?(e, errors)

    if driver.wait?
      raise e if timer.expired?

      sleep interval
      reload if session_options.automatic_reload
    else
      old_base = @base
      reload if session_options.automatic_reload
      raise e if old_base == @base
    end
    retry
  ensure
    session.synchronized = false
  end
end

#to_capybara_node

[ GitHub ]

  
# File 'lib/capybara/node/base.rb', line 128

def to_capybara_node
  self
end