Method: Capybara::Node::Finders#all

Defined in:
lib/capybara/node/finders.rb

#all([kind = Capybara.default_selector], locator = nil, **options) ⇒ Capybara::Result #all([kind = Capybara.default_selector], locator = nil, **options) {|element| ... } ⇒ Capybara::Result Also known as: find_all

Find all elements on the page matching the given selector and options.

Both XPath and CSS expressions are supported, but Capybara does not try to automatically distinguish between them. The following statements are equivalent:

page.all(:css, 'a#person_123')
page.all(:xpath, './/a[@id="person_123"]')

If the type of selector is left out, Capybara uses default_selector. It's set to :css by default.

page.all("a#person_123")

Capybara.default_selector = :xpath
page.all('.//a[@id="person_123"]')

The set of found elements can further be restricted by specifying options. It's possible to select elements by their text or visibility:

page.all('a', text: 'Home')
page.all('#menu li', visible: true)

By default Capybara's waiting behavior will wait up to default_max_wait_time seconds for matching elements to be available and then return an empty result if none are available. It is possible to set expectations on the number of results located and Capybara will raise an exception if the number of elements located don't satisfy the specified conditions. The expectations can be set using:

page.assert_selector('p#foo', count: 4)
page.assert_selector('p#foo', maximum: 10)
page.assert_selector('p#foo', minimum: 1)
page.assert_selector('p#foo', between: 1..10)

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time this method will wait is controlled through default_max_wait_time.

Overloads:

  • #all([kind = Capybara.default_selector], locator = nil, **options) {|element| ... } ⇒ Capybara::Result

    Yield Parameters:

    Yield Returns:

    • (Boolean)

      Should the element be considered in the results?

Parameters:

  • kind (Symbol)

    Optional selector type (:css, :xpath, :field, etc.). Defaults to default_selector.

  • locator (String) (defaults to: nil)

    The locator for the specified selector

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • text (String, Regexp)

    Only find elements which contain this text or match this regexp

  • exact_text (String, Regexp, String)

    When String the elements contained text must match exactly, when Boolean controls whether the text option must match exactly. Defaults to exact_text.

  • normalize_ws (Boolean)

    Whether the text/exact_text options are compared against element text with whitespace normalized or as returned by the driver. Defaults to default_normalize_ws.

  • visible (Boolean, Symbol)

    Only find elements with the specified visibility. Defaults to behavior indicated by ignore_hidden_elements.

    • true - only finds visible elements.
    • false - finds invisible and visible elements.
    • :all - same as false; finds visible and invisible elements.
    • :hidden - only finds invisible elements.
    • :visible - same as true; only finds visible elements.
  • obscured (Boolean)

    Only find elements with the specified obscured state:

    • true - only find elements whose centerpoint is not in the viewport or is obscured by another non-descendant element.
    • false - only find elements whose centerpoint is in the viewport and is not obscured by other non-descendant elements.
  • id (String, Regexp)

    Only find elements with an id that matches the value passed

  • class (String, Array<String>, Regexp)

    Only find elements with matching class/classes.

    • Absence of a class can be checked by prefixing the class name with !
    • If you need to check for existence of a class name that starts with ! then prefix with !!

      class:['a', '!b', '!!!c'] # limit to elements with class 'a' and '!c' but not class 'b'

  • style (String, Regexp, Hash)

    Only find elements with matching style. String and Regexp will be checked against text of the elements style attribute, while a Hash will be compared against the elements full style

  • exact (Boolean)

    Control whether is expressions in the given XPath match exactly or partially. Defaults to exact.

  • match (Symbol)

    The matching strategy to use. Defaults to match.

  • wait (false, true, Numeric)

    Maximum time to wait for matching element to appear. Defaults to default_max_wait_time.

  • count (Integer)

    Exact number of matches that are expected to be found

  • maximum (Integer)

    Maximum number of matches that are expected to be found

  • minimum (Integer)

    Minimum number of matches that are expected to be found

  • between (Range)

    Number of matches found must be within the given range

  • allow_reload (Boolean)

    Beta feature - May be removed in any version. When true allows elements to be reloaded if they become stale. This is an advanced behavior and should only be used if you fully understand the potential ramifications. The results can be confusing on dynamic pages. Defaults to false

Returns:

Raises:



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/capybara/node/finders.rb', line 257

def all(*args, allow_reload: false, **options, &optional_filter_block)
  minimum_specified = options_include_minimum?(options)
  options = { minimum: 1 }.merge(options) unless minimum_specified
  options[:session_options] = session_options
  query = Capybara::Queries::SelectorQuery.new(*args, **options, &optional_filter_block)
  result = nil
  begin
    synchronize(query.wait) do
      result = query.resolve_for(self)
      result.allow_reload! if allow_reload
      raise Capybara::ExpectationNotMet, result.failure_message unless result.matches_count?

      result
    end
  rescue Capybara::ExpectationNotMet
    raise if minimum_specified || (result.compare_count == 1)

    Result.new([], nil)
  end
end