Module: Pagetience

Defined in:
lib/pagetience.rb,
lib/pagetience/dsl.rb,
lib/pagetience/version.rb,
lib/pagetience/meditate.rb,
lib/pagetience/configuration.rb,
lib/pagetience/platforms/page-object-gem.rb

Defined Under Namespace

Modules: ClassMethods, DSL, Platform Classes: Configuration, ConfigurationError, Meditate, PlatformError, TimeoutError

Constant Summary collapse

VERSION =
'0.4.2'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject



15
16
17
# File 'lib/pagetience.rb', line 15

def config
  @config ||= Configuration.new
end

Instance Attribute Details

#_required_elementsObject

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



44
45
46
# File 'lib/pagetience.rb', line 44

def _required_elements
  @_required_elements
end

#_waiting_pollingObject

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



44
45
46
# File 'lib/pagetience.rb', line 44

def _waiting_polling
  @_waiting_polling
end

#_waiting_timeoutObject

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



44
45
46
# File 'lib/pagetience.rb', line 44

def _waiting_timeout
  @_waiting_timeout
end

#browserObject (readonly)

Returns the value of attribute browser.



46
47
48
# File 'lib/pagetience.rb', line 46

def browser
  @browser
end

#element_platformObject (readonly)

Returns the value of attribute element_platform.



47
48
49
# File 'lib/pagetience.rb', line 47

def element_platform
  @element_platform
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/pagetience.rb', line 19

def configure
  yield config
end

.included(base) ⇒ Object



49
50
51
# File 'lib/pagetience.rb', line 49

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#initialize(*args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# 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 || []
  wait_for_required_elements
end

#loaded?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/pagetience.rb', line 66

def loaded?
  !!@loaded
end

#wait_for(opts = {}, &block) ⇒ Object

Generic waiting method 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

Parameters:

  • opts (Hash) (defaults to: {})


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pagetience.rb', line 118

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) ⇒ Object

Wait for an element to be present

Parameters:

  • sym (Symbol)

    Name of the element

  • timeout (Fixnum) (defaults to: nil)

    Time to wait in seconds

  • polling (Fixnum) (defaults to: nil)

    How often to poll



88
89
90
91
92
93
94
95
# File 'lib/pagetience.rb', line 88

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) ⇒ Object

Waits for all elements specified by .required to be present

Parameters:

  • timeout (Fixnum) (defaults to: nil)

    Time to wait in seconds

  • polling (Fixnum) (defaults to: nil)

    How often to poll



73
74
75
76
77
78
79
80
81
82
# File 'lib/pagetience.rb', line 73

def wait_for_required_elements(timeout=nil, polling=nil)
  opts = {
      timeout: timeout,
      polling: polling,
      msg: "Timed out after polling every #{:polling}s for #{:timeout}s waiting for the page to be loaded."
  }
  wait_for(opts) do
    @loaded = true unless @_required_elements.any? { |e| !@element_platform.is_element_present? e }
  end
end

#wait_for_transition_to(page, timeout = nil, polling = nil) ⇒ Object

Wait for a transition to another page

Parameters:

  • timeout (Fixnum) (defaults to: nil)

    Time to wait in seconds

  • polling (Fixnum) (defaults to: nil)

    How often to poll



100
101
102
103
104
105
106
107
108
109
# File 'lib/pagetience.rb', line 100

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