Sha256: a5577e8d436f66ca94ec52da2156a0cdc98fcb39b45a7beeb24b1c8aee2b4eb3

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module PageObject
  module Elements
    class Table < Element
      include Enumerable

      #
      # iterator that yields with a PageObject::Elements::TableRow
      #
      # @return [PageObject::Elements::TableRow]
      #
      def each(&block)
        row_items.each(&block)
      end

      alias_method :first_row, :first

      #
      # return the last row
      #
      # @return PageObject::Elements::TableRow
      #
      def last_row
        self[-1]
      end

      #
      # Return the PageObject::Elements::TableRow for the index provided.  Index
      # is zero based.  If the index provided is a String then it
      # will be matched with the text from any column. The text can be a substring of the full column text.
      #
      # @return [PageObject::Elements::TableRow]
      #
      def [](what)
        idx = find_index(what)
        idx && row_items[idx]
      end

      #
      # Returns the number of rows in the table.
      #
      def rows
        row_items.size
      end

      protected

      def row_items
        meth = strategy == :descendants ? :trs : :rows
        @row_items ||= element.send(meth).map do |obj|
          ::PageObject::Elements::TableRow.new(obj)
        end
      end

      def strategy
        :children
      end

      def find_index(what)
        return what if what.is_a? Integer
        row_items.find_index do |row|
          row.cell(text: /#{what}/).exist?
        end
      end
    end

    ::PageObject::Elements.tag_to_class[:table] = ::PageObject::Elements::Table
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
page-object-2.2.1 lib/page-object/elements/table.rb