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::HTML(html).tap do |document|
    document.xpath('//textarea').each do |textarea|
      textarea['_capybara_raw_value'] = textarea.content.sub(/\A\n/, '')
    end
  end
end

def add_selector(name, &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, &block)
  Capybara::Selector.add(name, &block)
end

def config

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

def configure


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

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

=== DSL Options

[default_normalize_ws = Boolean] Whether text predicates and matchers use normalize whitespace behaviour (Default: false)
[predicates_wait = Boolean] Whether Capybaras predicate matchers use waiting behavior by default (Default: true)
[test_id = Symbol/String/nil] Optional attribute to match locator aginst with builtin selectors along with id (Default: nil)
[default_set_options = Hash] The default options passed to Node::set (Default: {})
[server = Symbol] The name of the registered server to use when running the app under test (Default: :webrick)
[threadsafe = Boolean] Whether sessions can be configured individually (Default: false)
[reuse_server = Boolean] Reuse the server thread between multiple sessions using the same app object (Default: true)
[enable_aria_label = Boolean] Whether fields, links, and buttons will match against aria-label attribute (Default: false)
[automatic_label_click = Boolean] Whether Node#choose, Node#check, Node#uncheck will attempt to click the associated label element if the checkbox/radio button are non-visible (Default: false)
[save_path = String] Where to put pages saved through save_(page|screenshot), save_and_open_(page|screenshot) (Default: Dir.pwd)
[automatic_reload = Boolean] Whether to automatically reload elements as Capybara is waiting (Default: true)
[ignore_hidden_elements = Boolean] Whether to ignore hidden elements on the page (Default: true)
[default_max_wait_time = Numeric] The maximum number of seconds to wait for asynchronous processes to finish (Default: 2)
[default_selector = :css/:xpath] Methods which take a selector use the given type by default (Default: :css)
[server_errors = Array\] Error classes that should be raised in the tests if they are raised in the server and Capybara.raise_server_errors is true (Default: [StandardError])
[raise_server_errors = Boolean] Should errors raised in the server be raised in the tests? (Default: true)
[run_server = Boolean] Whether to start a Rack server for the given Rack app (Default: true)
[asset_host = String] Where dynamic assets are hosted - will be prepended to relative asset locations if present (Default: nil)
[always_include_port = Boolean] Whether the Rack server's port should automatically be inserted into every visited URL unless another port is explicitly specified (Default: false)
[app_host = String/nil] The default host to use when giving a relative URL to visit, must be a valid URL e.g. http://www.example.com

=== Configurable options

end
config.app_host = 'http://www.google.com'
config.run_server = false
Capybara.configure do |config|

Configure Capybara to suit your needs.

#
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['capybara_current_driver']
  else
    @current_driver
  end || default_driver
end

def current_driver=(name)

def current_driver=(name)
  if threadsafe
    Thread.current['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 ||= {}
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[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[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 ||= {}
end

def session_name

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

def session_name=(name)

def session_name=(name)
  if threadsafe
    Thread.current['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['capybara_specified_session']
  else
    @specified_session
  end
end

def specified_session=(session)

def specified_session=(session)
  if threadsafe
    Thread.current['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
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)


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

#
def using_session(name_or_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
  yield
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