module Capybara

def HTML(html) # rubocop:disable Naming/MethodName

Returns:
  • (Nokogiri::HTML::Document) - HTML document

Parameters:
  • html (String) -- The raw html
def HTML(html) # rubocop:disable Naming/MethodName
  # Nokogiri >= 1.12.0 or Nokogumbo installed and allowed for use
  html_parser, using_html5 = if defined?(Nokogiri::HTML5) && Capybara.use_html5_parsing
    [Nokogiri::HTML5, true]
  else
    [defined?(Nokogiri::HTML4) ? Nokogiri::HTML4 : Nokogiri::HTML, false]
  end
  html_parser.parse(html).tap do |document|
    document.xpath('//template').each do |template|
      # template elements content is not part of the document
      template.inner_html = ''
    end
    document.xpath('//textarea').each do |textarea|
      # The Nokogiri HTML5 parser already returns spec compliant contents
      textarea['_capybara_raw_value'] = using_html5 ? textarea.content : textarea.content.delete_prefix("\n")
    end
  end
end

def add_selector(name, **options, &block)

Other tags:
    Yield: - A block executed in the context of the new {Capybara::Selector}

Parameters:
  • name (Symbol) -- The name of the selector to add
def add_selector(name, **options, &block)
  Capybara::Selector.add(name, **options, &block)
end

def config

def config
  @config ||= Capybara::Config.new
end

def configure


- **javascript_driver** (Symbol = `:selenium`) - The name of a driver to use for JavaScript enabled tests.
- **default_driver** (Symbol = `:rack_test`) - The name of the driver to use by default.

When using `capybara/dsl`, the following options are also available:

#### DSL Options

- **w3c_click_offset** (Boolean = 'true') - Whether click offsets should be from element center (true) or top left (false)
- **threadsafe** (Boolean = `false`) - Whether sessions can be configured individually.
- **test_id** (Symbol, String, `nil` = `nil`) - Optional attribute to match locator against with built-in selectors along with id.
and {configure raise_server_errors} is `true`.
- **server_errors** (Array\ = `[Exception]`) - Error classes that should be raised in the tests if they are raised in the server
- **server_host** (String = "127.0.0.1") - The IP address Capybara will bind the application server to. If the test application is to be accessed from an external host, you will want to change this to "0.0.0.0" or to a more specific IP address that your test client can reach.
- **server_port** (Integer) - The port Capybara will run the application server on, if not specified a random port will be used.
- **server** (Symbol = `:default` (which uses puma)) - The name of the registered server to use when running the app under test.
{Capybara::Session#save_and_open_page save_and_open_page}, or {Capybara::Session#save_and_open_screenshot save_and_open_screenshot}.
- **save_path** (String = `Dir.pwd`) - Where to put pages saved through {Capybara::Session#save_page save_page}, {Capybara::Session#save_screenshot save_screenshot},
- **run_server** (Boolean = `true`) - Whether to start a Rack server for the given Rack app.
- **reuse_server** (Boolean = `true`) - Whether to reuse the server thread between multiple sessions using the same app object.
- **raise_server_errors** (Boolean = `true`) - Should errors raised in the server be raised in the tests?
- **predicates_wait** (Boolean = `true`) - Whether Capybara's predicate matchers use waiting behavior by default.
- **match** (`:one`, `:first`, `:prefer_exact`, `:smart` = `:smart`) - The matching strategy to find nodes.
- **ignore_hidden_elements** (Boolean = `true`) - Whether to ignore hidden elements on the page.
- **exact_text** (Boolean = `false`) - Whether the text matchers and `:text` filter match exactly or on substrings.
written using the `XPath#is` method.
- **exact** (Boolean = `false`) - Whether locators are matched exactly or with substrings. Only affects selector conditions
- **enable_aria_role** (Boolean = `false`) - Selectors will check for relevant aria role (currently only `button`).
- **enable_aria_label** (Boolean = `false`) - Whether fields, links, and buttons will match against `aria-label` attribute.
- **default_set_options** (Hash = `{}`) - The default options passed to {Capybara::Node::Element#set Element#set}.
- **default_selector** (`:css`, `:xpath` = `:css`) - Methods which take a selector use the given type by default. See also {Capybara::Selector}.
- **default_retry_interval** (Numeric = `0.01`) - The number of seconds to delay the next check in asynchronous processes.
- **default_normalize_ws** (Boolean = `false`) - Whether text predicates and matchers use normalize whitespace behavior.
- **default_max_wait_time** (Numeric = `2`) - The maximum number of seconds to wait for asynchronous processes to finish.
- **automatic_reload** (Boolean = `true`) - Whether to automatically reload elements as Capybara is waiting.
{Capybara::Node::Element#uncheck Element#uncheck} will attempt to click the associated `
def configure
  yield config
end

def current_driver

Returns:
  • (Symbol) - The name of the driver currently in use
def current_driver
  if threadsafe
    Thread.current.thread_variable_get :capybara_current_driver
  else
    @current_driver
  end || default_driver
end

def current_driver=(name)

def current_driver=(name)
  if threadsafe
    Thread.current.thread_variable_set :capybara_current_driver, name
  else
    @current_driver = name
  end
end

def current_session

Returns:
  • (Capybara::Session) - The currently used session
def current_session
  specified_session || session_pool["#{current_driver}:#{session_name}:#{app.object_id}"]
end

def drivers

def drivers
  @drivers ||= RegistrationContainer.new
end

def modify_selector(name, &block)

Other tags:
    Yield: - A block executed in the context of the existing {Capybara::Selector}

Parameters:
  • name (Symbol) -- The name of the selector to modify
def modify_selector(name, &block)
  Capybara::Selector.update(name, &block)
end

def register_driver(name, &block)

Other tags:
    Yieldreturn: - A Capybara driver instance

Other tags:
    Yieldparam: app - The rack application that this driver runs against. May be nil.

Other tags:
    Yield: - This block takes a rack app and returns a Capybara driver

Parameters:
  • name (Symbol) -- The name of the new driver
def register_driver(name, &block)
  drivers.send(:register, name, block)
end

def register_server(name, &block)

Other tags:
    Yieldparam: host - The host/ip to bind to
    Yieldparam: port - The port number the server should listen on
    Yieldparam: app - The rack application that this server will contain.

Other tags:
    Yield: - This block takes a rack app and a port and returns a rack server listening on that port

Parameters:
  • name (Symbol) -- The name of the new driver
def register_server(name, &block)
  servers.send(:register, name.to_sym, block)
end

def reset_sessions!


as cookies.
Reset sessions, cleaning out the pool of sessions. This will remove any session information such

#
def reset_sessions!
  # reset in reverse so sessions that started servers are reset last
  session_pool.reverse_each { |_mode, session| session.reset! }
end

def run_default_server(app, port)

Parameters:
  • port (Integer) -- The port to run the application on
  • app (Rack Application) -- The rack application to run
def run_default_server(app, port)
  servers[:puma].call(app, port, server_host)
end

def servers

def servers
  @servers ||= RegistrationContainer.new
end

def session_name

Returns:
  • (Symbol) - The name of the currently used session.
def session_name
  if threadsafe
    Thread.current.thread_variable_get(:capybara_session_name) ||
      Thread.current.thread_variable_set(:capybara_session_name, :default)
  else
    @session_name ||= :default
  end
end

def session_name=(name)

def session_name=(name)
  if threadsafe
    Thread.current.thread_variable_set :capybara_session_name, name
  else
    @session_name = name
  end
end

def session_options

def session_options
  config.session_options
end

def session_pool

def session_pool
  @session_pool ||= Hash.new do |hash, name|
    hash[name] = Capybara::Session.new(current_driver, app)
  end
end

def specified_session

def specified_session
  if threadsafe
    Thread.current.thread_variable_get :capybara_specified_session
  else
    @specified_session ||= nil
  end
end

def specified_session=(session)

def specified_session=(session)
  if threadsafe
    Thread.current.thread_variable_set :capybara_specified_session, session
  else
    @specified_session = session
  end
end

def string(html)

Returns:
  • (Capybara::Node::Simple) - A node which has Capybara's finders and matchers

Parameters:
  • html (String) -- An html fragment or document

Other tags:
    Example: Multiple elements -
    Example: A single element -
def string(html)
  Capybara::Node::Simple.new(html)
end

def use_default_driver


Use the default driver as the current driver

#
def use_default_driver
  self.current_driver = nil
end

def using_driver(driver)


Yield a block using a specific driver

#
def using_driver(driver)
  previous_driver = Capybara.current_driver
  Capybara.current_driver = driver
  yield
ensure
  self.current_driver = previous_driver
end

def using_session(name_or_session, &block)


Yield a block using a specific session name or {Capybara::Session} instance.

#
def using_session(name_or_session, &block)
  previous_session = current_session
  previous_session_info = {
    specified_session: specified_session,
    session_name: session_name,
    current_driver: current_driver,
    app: app
  }
  self.specified_session = self.session_name = nil
  if name_or_session.is_a? Capybara::Session
    self.specified_session = name_or_session
  else
    self.session_name = name_or_session
  end
  if block.arity.zero?
    yield
  else
    yield current_session, previous_session
  end
ensure
  self.session_name, self.specified_session = previous_session_info.values_at(:session_name, :specified_session)
  self.current_driver, self.app = previous_session_info.values_at(:current_driver, :app) if threadsafe
end

def using_wait_time(seconds)


Yield a block using a specific wait time

#
def using_wait_time(seconds)
  previous_wait_time = Capybara.default_max_wait_time
  Capybara.default_max_wait_time = seconds
  yield
ensure
  Capybara.default_max_wait_time = previous_wait_time
end