module Pagetience

Constants

VERSION

Attributes

config[W]
_required_elements[RW]

“Private” methods to avoid naming collision but remain helpful They can be messed with though, if you ever see fit.

_waiting_polling[RW]

“Private” methods to avoid naming collision but remain helpful They can be messed with though, if you ever see fit.

_waiting_timeout[RW]

“Private” methods to avoid naming collision but remain helpful They can be messed with though, if you ever see fit.

browser[R]
element_platform[R]

Public Class Methods

config() click to toggle source
# File lib/pagetience.rb, line 15
def config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source
# File lib/pagetience.rb, line 19
def configure
  yield config
end
included(base) click to toggle source
# File lib/pagetience.rb, line 49
def self.included(base)
  base.extend ClassMethods
end
new(*args) click to toggle source
# File lib/pagetience.rb, line 53
def initialize(*args)
  @browser = args[0]
  set_current_page

  @element_platform = Pagetience.config.platform.init self, args

  @loaded = false
  @_waiting_timeout = _waiting_timeout || Pagetience.config.timeout
  @_waiting_polling = _waiting_polling || Pagetience.config.polling
  @_required_elements = _required_elements || []
  @_present_elements = []
  @_missing_elements = []
  wait_for_required_elements
end

Public Instance Methods

loaded?() click to toggle source
# File lib/pagetience.rb, line 68
def loaded?
  !!@loaded
end
wait_for(opts={}, &block) click to toggle source

Generic waiting method @param [Hash] opts Valid options: :timeout = Time to wait in seconds :polling = How often to poll :expecting = The expected result for the block to return :msg = The exception message if the timeout occurs

# File lib/pagetience.rb, line 129
def wait_for(opts={}, &block)
  opts = {
      timeout: @_waiting_timeout,
      polling: @_waiting_polling,
      expecting: true,
      msg: "Timed out after waiting for #{@_waiting_timeout}s, polling every #{@_waiting_polling}s."
  }.merge(opts) do |key, old, new|
    new.nil? ? old : new
  end
  Pagetience::Meditate.for(opts) { block.call }
end
wait_for_element(sym, timeout=nil, polling=nil) click to toggle source

Wait for an element to be present @param [Symbol] sym Name of the element @param [Fixnum] timeout Time to wait in seconds @param [Fixnum] polling How often to poll

# File lib/pagetience.rb, line 99
def wait_for_element(sym, timeout=nil, polling=nil)
  opts = {
      timeout: timeout,
      polling: polling,
      msg: "Timed out after waiting for the element #{sym} to be present."
  }
  wait_for(opts) { @element_platform.is_element_present? sym }
end
wait_for_required_elements(timeout=nil, polling=nil) click to toggle source

Waits for all elements specified by .required to be present @param [Fixnum] timeout Time to wait in seconds @param [Fixnum] polling How often to poll

# File lib/pagetience.rb, line 75
def wait_for_required_elements(timeout=nil, polling=nil)
  msg = -> do
    %{Timed out after polling every #{@_waiting_polling}s for #{@_waiting_timeout}s waiting for the page to be loaded.
    Elements present: #{@_present_elements}
    Elements missing: #{@_missing_elements}}
  end

  opts = { timeout: timeout, polling: polling, msg: msg }
  wait_for(opts) do
    @_missing_elements = []
    @_required_elements.each do |e|
      if !@element_platform.is_element_present?(e) && !@_missing_elements.include?(e)
        @_missing_elements << e
        @_present_elements = @_required_elements - @_missing_elements
      end
    end
    @loaded = @_missing_elements.empty?
  end
end
wait_for_transition_to(page, timeout=nil, polling=nil) click to toggle source

Wait for a transition to another page @param [Fixnum] timeout Time to wait in seconds @param [Fixnum] polling How often to poll

# File lib/pagetience.rb, line 111
def wait_for_transition_to(page, timeout=nil, polling=nil)
  page = page.new browser
  opts = {
      timeout: timeout,
      polling: polling,
      msg: "Timed out after waiting for the page to transition to #{page}."
  }
  wait_for(opts) { page.loaded? }
  page
end

Private Instance Methods

set_current_page() click to toggle source

Sets .current on the browser

# File lib/pagetience.rb, line 144
def set_current_page
  current_page = self
  @browser.class.send(:define_method, :current_page) { current_page }
end