Sha256: 5a6ed884fe2d7a93c377a7cc57f541a036c1a36b102768f1b32e7b8bc246fafb

Contents?: true

Size: 1.66 KB

Versions: 8

Compression:

Stored size: 1.66 KB

Contents

# encoding: utf-8

module Watir
# This assumes that Element#visible? is defined
  module ElementExtensions

    #
    # Wraps a {Celerity,Watir}::Element so that any subsequent method calls are
    # put on hold until the element is present on the page.
    #

    class WhenPresentDecorator
      def initialize(element, timeout)
        @element = element
        @timeout = timeout
      end

      def method_missing(m, *args, &block)
        Watir::Wait.until(@timeout) { @element.present? }
        @element.send(m, *args, &block)
      end
    
      # Returns element id
      def id
        Watir::Wait.until(@timeout) { @element.present? }
        @element.id
      end

    end

    #
    # Returns true if the element exists and is visible on the page
    #

    def present?
      exists? && visible? rescue false
    end

    #
    # Waits until the element is present.
    #
    # Optional argument:
    #
    #   timeout   -  seconds to wait before timing out (default: 60)
    #
    #     browser.button(:id, 'foo').when_present.click
    #     browser.div(:id, 'bar').when_present { |div| ... }
    #     browser.p(:id, 'baz').when_present(60).text
    #

    def when_present(timeout = 60)
      if block_given?
        Watir::Wait.until(timeout) { self.present? }
        yield self
      else
        return WhenPresentDecorator.new(self, timeout)
      end
    end

    def wait_until_present(timeout = 60)
      Watir::Wait.until(timeout) { self.present? }
    end

    def wait_while_present(timeout = 60)
      Watir::Wait.while(timeout) { self.present? }
    end

  end # module ElementExtensions
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
watir-classic-3.3.0 lib/watir-classic/element_extensions.rb
watir-classic-3.2.0 lib/watir-classic/element_extensions.rb
watir-classic-3.2.0.rc1 lib/watir-classic/element_extensions.rb
watir-classic-3.1.0 lib/watir-classic/element_extensions.rb
watir-classic-3.0.0 lib/watir-classic/element_extensions.rb
commonwatir-3.0.0.rc3 lib/watir/element_extensions.rb
commonwatir-3.0.0.rc2 lib/watir/element_extensions.rb
commonwatir-3.0.0.rc1 lib/watir/element_extensions.rb