Module: Capybara::Helpers Private

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/capybara/helpers.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#declension(singular, plural, count) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • singular (String)

    The singular form of the word

  • plural (String)

    The plural form of the word

  • count (Integer)

    The number of items



120
121
122
123
124
125
126
# File 'lib/capybara/helpers.rb', line 120

def declension(singular, plural, count)
  if count == 1
    singular
  else
    plural
  end
end

#expects_none?(options = {}) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if a count of 0 is valid for the given options hash. Returns false if options hash does not specify any count options.

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/capybara/helpers.rb', line 77

def expects_none?(options={})
  if [:count, :maximum, :minimum, :between].any? { |k| options.has_key? k }
    matches_count?(0,options)
  else
    false
  end
end

#failure_message(description, options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a failure message given a description of the query and count options.

Parameters:

  • description (String)

    Description of a query

  • [Range] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/capybara/helpers.rb', line 96

def failure_message(description, options={})
  message = "expected to find #{description}"
  if options[:count]
    message << " #{options[:count]} #{declension('time', 'times', options[:count])}"
  elsif options[:between]
    message << " between #{options[:between].first} and #{options[:between].last} times"
  elsif options[:maximum]
    message << " at most #{options[:maximum]} #{declension('time', 'times', options[:maximum])}"
  elsif options[:minimum]
    message << " at least #{options[:minimum]} #{declension('time', 'times', options[:minimum])}"
  end
  message
end

#inject_asset_host(html) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • html (String)

    HTML code to inject into

Returns:

  • (String)

    The modified HTML code



42
43
44
45
46
47
48
49
# File 'lib/capybara/helpers.rb', line 42

def inject_asset_host(html)
  if Capybara.asset_host && Nokogiri::HTML(html).css("base").empty? 
     match = html.match(/<head[^<]*?>/)
      html.clone.insert match.end(0), "<base href='#{Capybara.asset_host}' />"
  else
    html
  end
end

#matches_count?(count, options = {}) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if the given count matches the given count options. Defaults to true if no options are specified. If multiple options are provided, it tests that all conditions are met; however, if :count is supplied, all other options are ignored.

Parameters:

  • count (Integer)

    The actual number. Should be coercible via Integer()

  • [Range] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/capybara/helpers.rb', line 64

def matches_count?(count, options={})
  return (Integer(options[:count]) == count)     if options[:count]
  return false if options[:maximum] && (Integer(options[:maximum]) < count)
  return false if options[:minimum] && (Integer(options[:minimum]) > count)
  return false if options[:between] && !(options[:between] === count)
  return true
end

#normalize_whitespace(text) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • text (String)

    Text to normalize

Returns:

  • (String)

    Normalized text



18
19
20
# File 'lib/capybara/helpers.rb', line 18

def normalize_whitespace(text)
  text.to_s.gsub(/[[:space:]]+/, ' ').strip
end

#to_regexp(text) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • text (String)

    Text to escape

Returns:

  • (String)

    Escaped text



30
31
32
# File 'lib/capybara/helpers.rb', line 30

def to_regexp(text)
  text.is_a?(Regexp) ? text : Regexp.new(Regexp.escape(normalize_whitespace(text)))
end