Sha256: 04bef3095c92a18d23102a893c33a227dd5ea08cd20fe4d8fb35daa3de3a92bb

Contents?: true

Size: 1.64 KB

Versions: 20

Compression:

Stored size: 1.64 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?
    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

20 entries across 20 versions & 1 rubygems

Version Path
commonwatir-2.0.4 lib/watir/element_extensions.rb
commonwatir-2.0.3 lib/watir/element_extensions.rb
commonwatir-2.0.2 lib/watir/element_extensions.rb
commonwatir-2.0.2.rc1 lib/watir/element_extensions.rb
commonwatir-2.0.1 lib/watir/element_extensions.rb
commonwatir-2.0.0 lib/watir/element_extensions.rb
commonwatir-2.0.0.rc3 lib/watir/element_extensions.rb
commonwatir-2.0.0.rc2 lib/watir/element_extensions.rb
commonwatir-1.9.2 lib/watir/element_extensions.rb
commonwatir-1.9.2.rc1 lib/watir/element_extensions.rb
commonwatir-1.9.1 lib/watir/element_extensions.rb
commonwatir-1.9.1.rc1 lib/watir/element_extensions.rb
commonwatir-1.9.0 lib/watir/element_extensions.rb
commonwatir-1.9.0.rc7 lib/watir/element_extensions.rb
commonwatir-1.9.0.rc6 lib/watir/element_extensions.rb
commonwatir-1.9.0.rc5 lib/watir/element_extensions.rb
commonwatir-1.9.0.rc4 lib/watir/element_extensions.rb
commonwatir-1.9.0.rc3 lib/watir/element_extensions.rb
commonwatir-1.9.0.rc2 lib/watir/element_extensions.rb
commonwatir-1.9.0.rc1 lib/watir/element_extensions.rb