module Capybara::Helpers

@api private

Public Instance Methods

declension(singular, plural, count) click to toggle source

A poor man's `pluralize`. Given two declensions, one singular and one plural, as well as a count, this will pick the correct declension. This way we can generate grammatically correct error message.

@param [String] singular The singular form of the word @param [String] plural The plural form of the word @param [Integer] count The number of items

# File lib/capybara/helpers.rb, line 68
def declension(singular, plural, count)
  count == 1 ? singular : plural
end
inject_asset_host(html, host: Capybara.asset_host) click to toggle source

Injects a `<base>` tag into the given HTML code, pointing to `Capybara.asset_host`.

@param [String] html HTML code to inject into @param [URL] host (Capybara.asset_host) The host from which assets should be loaded @return [String] The modified HTML code

# File lib/capybara/helpers.rb, line 50
def inject_asset_host(html, host: Capybara.asset_host)
  if host && Nokogiri::HTML(html).css('base').empty?
    match = html.match(/<head[^<]*?>/)
    return html.clone.insert match.end(0), "<base href='#{host}' />" if match
  end
  html
end
monotonic_time() click to toggle source
# File lib/capybara/helpers.rb, line 73
def monotonic_time; Process.clock_gettime Process::CLOCK_MONOTONIC; end
normalize_whitespace(text) click to toggle source

@deprecated Normalizes whitespace space by stripping leading and trailing whitespace and replacing sequences of whitespace characters with a single space.

@param [String] text Text to normalize @return [String] Normalized text

# File lib/capybara/helpers.rb, line 17
def normalize_whitespace(text)
  warn 'DEPRECATED: Capybara::Helpers::normalize_whitespace is deprecated, please update your driver'
  text.to_s.gsub(/[[:space:]]+/, ' ').strip
end
timer(expire_in:) click to toggle source
# File lib/capybara/helpers.rb, line 78
def timer(expire_in:)
  Timer.new(expire_in)
end
to_regexp(text, exact: false, all_whitespace: false, options: nil) click to toggle source

Escapes any characters that would have special meaning in a regexp if text is not a regexp

@param [String] text Text to escape @param [Boolean] exact (false) Whether or not this should be an exact text match @param [Fixnum, Boolean, nil] options Options passed to Regexp.new when creating the Regexp @return [Regexp] Regexp to match the passed in text and options

# File lib/capybara/helpers.rb, line 32
def to_regexp(text, exact: false, all_whitespace: false, options: nil)
  return text if text.is_a?(Regexp)

  escaped = Regexp.escape(text)
  escaped = escaped.gsub('\\ ', '[[:blank:]]') if all_whitespace
  escaped = "\\A#{escaped}\\z" if exact
  Regexp.new(escaped, options)
end