Sha256: fe161cc2331e53c84faf6fa1d16890e4377c68328a5fb77302308fa50e23a80f

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 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)
        unless @element.respond_to?(m)
          raise NoMethodError, "undefined method `#{m}' for #{@element.inspect}:#{@element.class}"
        end

        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

6 entries across 6 versions & 1 rubygems

Version Path
commonwatir-1.8.1 lib/watir/element_extensions.rb
commonwatir-1.8.1.rc1 lib/watir/element_extensions.rb
commonwatir-1.8.0 lib/watir/element_extensions.rb
commonwatir-1.8.0.rc1 lib/watir/element_extensions.rb
commonwatir-1.7.1 lib/watir/element_extensions.rb
commonwatir-1.7.0 lib/watir/element_extensions.rb