Sha256: adbe75386d0d23eb9ab30a01c655f07496a3a19394e4b3ef916372ff21e3b3f0

Contents?: true

Size: 1.9 KB

Versions: 32

Compression:

Stored size: 1.9 KB

Contents

# encoding: utf-8
module Watir

  #
  # Base class for element collections.
  #

  class ElementCollection
    include Enumerable

    def initialize(parent, selector)
      @parent   = parent
      @selector = selector
    end

    #
    # @yieldparam [Watir::Element] element Iterate through the elements in this collection.
    #

    def each(&blk)
      to_a.each(&blk)
    end

    #
    # @return [Fixnum] The number of elements in this collection.
    #

    def length
      elements.length
    end
    alias_method :size, :length

    #
    # Get the element at the given index.
    # Note that this is 0-indexed and not compatible with older Watir implementations.
    #
    # Also note that because of Watir's lazy loading, this will return an Element
    # instance even if the index is out of bounds.
    #
    # @param [Fixnum] n Index of wanted element, 0-indexed
    # @return [Watir::Element] Returns an instance of a Watir::Element subclass
    #

    def [](idx)
      to_a[idx] || element_class.new(@parent, :index => idx)
    end

    #
    # First element of this collection
    #
    # @return [Watir::Element] Returns an instance of a Watir::Element subclass
    #

    def first
      self[0]
    end

    #
    # Last element of the collection
    #
    # @return [Watir::Element] Returns an instance of a Watir::Element subclass
    #

    def last
      self[-1]
    end

    #
    # This collection as an Array
    #
    # @return [Array<Watir::Element>]
    #

    def to_a
      # TODO: optimize - lazy element_class instance?
      @to_a ||= elements.map { |e| element_class.new(@parent, :element => e) }
    end

    private

    def elements
      @elements ||= locator_class.new(
        @parent.wd,
        @selector,
        element_class.attribute_list
      ).locate_all
    end

    # overridable by subclasses
    def locator_class
      ElementLocator
    end

  end # ElementCollection
end # Watir

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
watir-webdriver-0.6.1 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.6.0 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.8 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.7 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.6 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.5 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.4 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.3 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.2 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.1 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.5.0 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.4.1 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.4.0 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.3.9 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.3.8 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.3.7 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.3.6 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.3.5 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.3.4 lib/watir-webdriver/element_collection.rb
watir-webdriver-0.3.3 lib/watir-webdriver/element_collection.rb