Sha256: c1e105adfc65c6e3f9dd59fae35a93438de00b8fd6dfac11346585a531836737

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8

# 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::WaitHelper.wait_until(@timeout) { @element.present? }
      @element.send(m, *args, &block)
    end
  end

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

  def present?
    exists? && visible?
  end

  def events
    raise "only supported on WebDriver" unless GlobalConfig.webdriver?
    assert_exists
    browser.execute_script("return arguments[0].data('events')", @element)
  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::WaitHelper.wait_until(timeout) { self.present? }
      yield self
    else
      return WhenPresentDecorator.new(self, timeout)
    end
  end

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

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

end # module ElementExtensions

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
watirsplash-0.2.14 lib/watirsplash/element_extensions.rb
watirsplash-0.2.12 lib/watirsplash/element_extensions.rb
watirsplash-0.2.11 lib/watirsplash/element_extensions.rb
watirsplash-0.2.10 lib/watirsplash/element_extensions.rb
watirsplash-0.2.9 lib/watirsplash/element_extensions.rb
watirsplash-0.2.8 lib/watirsplash/element_extensions.rb
watirsplash-0.2.7 lib/watirsplash/element_extensions.rb
watirsplash-0.2.6 lib/watirsplash/element_extensions.rb