pagetience 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d72d9ede5f1719c7d819dac50cd9c96de24af39d
4
- data.tar.gz: 1ffef24be2c258c445a33cf0fe346b44b8f19bd7
3
+ metadata.gz: 047639c7bb485cabfaede710c9398d3d9f469fcc
4
+ data.tar.gz: cf3130de096efde5b85cfb5776213a3b1160161d
5
5
  SHA512:
6
- metadata.gz: 6aa8a51684bf2443a6d9a5108226162f38faf5d442e84bdf1cade8685a1bfda2f20455ffd89b693ab037df9909285c83c19d82a640bc5dc0b7ae15804446dd35
7
- data.tar.gz: 639efc4b0bb053daba6b563011862e9d993db2375d3f21426669d727e1d1d49cf6d5f97e359ea9fb75c6c32f36ea83f6482e34a03e046aae0a0a301ad5699e2b
6
+ metadata.gz: 77dddb513326bd516ccb69b36fc101182afb900b105fe4eb9288dbc794e4528a3f4d6d13ce2461a3b050dbcea5de2b19b87cc46ce9336e30f707e3c97b85c5f9
7
+ data.tar.gz: b7fe6cfc8b03af4fa6f83769b6ecd0bd7c99bcf037dbed82f2ef85ca5e25845b4d1abbfa92aa1690f66638cbd27b62758826b7202ce8533b58ac3256ab917fd3
data/README.md CHANGED
@@ -91,13 +91,32 @@ class SomePage
91
91
  end
92
92
 
93
93
  def wait_longer_for_foo
94
- wait_for_element :foo, 60, 5
94
+ wait_for_element :foo, 60, 5 # wait up to 60 seconds, polling every 5 seconds
95
95
  end
96
96
  end
97
+ ```
98
+
99
+ ## Configuration
100
+ Pagetience can be configured, too.
101
+
102
+ ```ruby
103
+ Pagetience.configure do |config|
104
+ config.timeout = 60
105
+ config.polling = 5
97
106
  end
98
107
  ```
99
108
 
100
- #### Adjusting the Timeout/Polling
109
+ It can then be retrieve easily!
110
+
111
+ ```ruby
112
+ def give_me_a_property
113
+ Pagetience.config.timeout
114
+ end
115
+
116
+ give_me_a_property # => 60
117
+ ````
118
+
119
+ #### Adjusting the Timeout/Polling for a specific Page
101
120
  You can use the `waiting` method to specify how long you want to wait and, optionally, at what interval to poll the page for element visibility.
102
121
 
103
122
  The default timeout is **30** seconds, polling every second.
@@ -0,0 +1,30 @@
1
+ module Pagetience
2
+ class Configuration
3
+ VALID_PROPERTIES = [
4
+ :timeout,
5
+ :polling,
6
+ :platform
7
+ ]
8
+
9
+ attr_accessor *VALID_PROPERTIES
10
+
11
+ # Default timeout in seconds
12
+ DEFAULT_TIMEOUT = 30
13
+
14
+ # Default polling in seconds
15
+ DEFAULT_POLLING = 1
16
+
17
+ # Default element platform
18
+ DEFAULT_PLATFORM = Pagetience::Platform::PageObjectGem
19
+
20
+ def initialize
21
+ @timeout = DEFAULT_TIMEOUT
22
+ @polling = DEFAULT_POLLING
23
+ @platform = DEFAULT_PLATFORM
24
+ end
25
+
26
+ def method_missing(sym, *args)
27
+ raise Pagetience::ConfigurationError, "Unknown property #{sym}."
28
+ end
29
+ end
30
+ end
@@ -5,5 +5,9 @@ module Pagetience
5
5
  def current_page
6
6
  @browser.current_page
7
7
  end
8
+
9
+ def wait_for_element(sym)
10
+ @browser.current_page.wait_for_element sym
11
+ end
8
12
  end
9
13
  end
@@ -29,7 +29,7 @@ module Pagetience
29
29
  @timeout = @timeout - @polling
30
30
  end
31
31
 
32
- raise Pagetience::Exceptions::Timeout, msg unless @latest_result == expected
32
+ raise Pagetience::TimeoutError, msg unless @latest_result == expected
33
33
 
34
34
  @latest_result
35
35
  end
@@ -1,36 +1,34 @@
1
1
  module Pagetience
2
- module ElementPlatforms
3
- class PageObjectGem < Base
4
- attr_reader :page_object_instance
5
-
2
+ module Platform
3
+ class PageObjectGem
6
4
  class << self
7
- def present?(klazz)
8
- klazz.class.ancestors.include? PageObject
9
- end
10
- end
5
+ def init(base, *args)
6
+ args.flatten! if args
11
7
 
12
- def initialize(klazz)
13
- super
8
+ base.class.send(:define_method, :visit) do
9
+ args[1] || false
10
+ end
11
+ base.instance_eval do
12
+ PageObject.instance_method(:initialize).bind(self).call(base.browser, visit)
13
+ end
14
14
 
15
- @page_object_instance = klazz
16
- @browser = @page_object_instance.browser
15
+ self.new base
16
+ end
17
17
  end
18
18
 
19
- def platform_initialize(args=[])
20
- @page_object_instance.class.send(:define_method, :visit) do
21
- args[0] || false
22
- end
23
- @page_object_instance.instance_eval do
24
- PageObject.instance_method(:initialize).bind(self).call(@browser, visit)
25
- end
19
+ attr_reader :page_object, :browser
20
+
21
+ def initialize(page)
22
+ @page_object = page
23
+ @browser = @page_object.browser
26
24
  end
27
25
 
28
26
  def underlying_element_for(sym)
29
- @page_object_instance.send("#{sym}_element").element
27
+ @page_object.send("#{sym}_element").element
30
28
  end
31
29
 
32
30
  def is_element_present?(sym)
33
- @page_object_instance.send("#{sym}_element").visible?
31
+ @page_object.send("#{sym}_element").visible?
34
32
  end
35
33
  end
36
34
  end
@@ -1,3 +1,3 @@
1
1
  module Pagetience
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.2'
3
3
  end
data/lib/pagetience.rb CHANGED
@@ -1,13 +1,26 @@
1
- require 'pagetience/exceptions'
1
+ require 'pagetience/platforms/page-object-gem'
2
+
3
+ require 'pagetience/configuration'
2
4
  require 'pagetience/meditate'
3
5
  require 'pagetience/version'
4
6
 
5
- require 'pagetience/platforms/base'
6
- require 'pagetience/platforms/page-object-gem'
7
+ module Pagetience
8
+ class TimeoutError < StandardError; end
9
+ class PlatformError < StandardError; end
10
+ class ConfigurationError < StandardError; end
7
11
 
8
- require 'pagetience/platforms/element_platforms'
12
+ class << self
13
+ attr_writer :config
14
+
15
+ def config
16
+ @config ||= Configuration.new
17
+ end
18
+
19
+ def configure
20
+ yield config
21
+ end
22
+ end
9
23
 
10
- module Pagetience
11
24
  module ClassMethods
12
25
  def required(*elements)
13
26
  elements.keep_if { |e| e.is_a? Symbol }
@@ -26,30 +39,26 @@ module Pagetience
26
39
  end
27
40
  end
28
41
 
29
- attr_accessor :_waiting_timeout, :_waiting_polling
30
-
31
- attr_reader :browser, :loaded
42
+ # "Private" methods to avoid naming collision but remain helpful
43
+ # They can be messed with though, if you ever see fit.
44
+ attr_accessor :_waiting_timeout, :_waiting_polling, :_required_elements
32
45
 
46
+ attr_reader :browser
33
47
  attr_reader :element_platform
34
- attr_reader :_required_elements
35
48
 
36
49
  def self.included(base)
37
50
  base.extend ClassMethods
38
51
  end
39
52
 
40
- def initialize(browser, *args)
41
- @browser = browser
42
-
43
- current_page = self
44
- @browser.class.send(:define_method, :current_page) { current_page }
53
+ def initialize(*args)
54
+ @browser = args[0]
55
+ set_current_page
45
56
 
46
- determine_platform
47
- @element_platform.platform_initialize args
57
+ @element_platform = Pagetience.config.platform.init self, args
48
58
 
49
59
  @loaded = false
50
- @_waiting_timeout = _waiting_timeout || 30
51
- @_waiting_polling = _waiting_polling || 1
52
-
60
+ @_waiting_timeout = _waiting_timeout || Pagetience.config.timeout
61
+ @_waiting_polling = _waiting_polling || Pagetience.config.polling
53
62
  @_required_elements = _required_elements || []
54
63
  wait_for_required_elements
55
64
  end
@@ -58,11 +67,13 @@ module Pagetience
58
67
  !!@loaded
59
68
  end
60
69
 
70
+ # Waits for all elements specified by .required to be present
71
+ # @param [Fixnum] timeout Time to wait in seconds
72
+ # @param [Fixnum] polling How often to poll
61
73
  def wait_for_required_elements(timeout=nil, polling=nil)
62
74
  opts = {
63
- timeout: timeout || @_waiting_timeout,
64
- polling: polling || @_waiting_polling,
65
- expecting: true,
75
+ timeout: timeout,
76
+ polling: polling,
66
77
  msg: "Timed out after polling every #{:polling}s for #{:timeout}s waiting for the page to be loaded."
67
78
  }
68
79
  wait_for(opts) do
@@ -70,37 +81,57 @@ module Pagetience
70
81
  end
71
82
  end
72
83
 
84
+ # Wait for an element to be present
85
+ # @param [Symbol] sym Name of the element
86
+ # @param [Fixnum] timeout Time to wait in seconds
87
+ # @param [Fixnum] polling How often to poll
73
88
  def wait_for_element(sym, timeout=nil, polling=nil)
74
89
  opts = {
75
- timeout: timeout || @_waiting_timeout,
76
- polling: polling || @_waiting_polling,
77
- expecting: true,
90
+ timeout: timeout,
91
+ polling: polling,
78
92
  msg: "Timed out after waiting for the element #{sym} to be present."
79
93
  }
80
94
  wait_for(opts) { @element_platform.is_element_present? sym }
81
95
  end
82
96
 
97
+ # Wait for a transition to another page
98
+ # @param [Fixnum] timeout Time to wait in seconds
99
+ # @param [Fixnum] polling How often to poll
83
100
  def wait_for_transition_to(page, timeout=nil, polling=nil)
84
101
  page = page.new browser
85
102
  opts = {
86
- timeout: timeout || @_waiting_timeout,
87
- polling: polling || @_waiting_polling,
88
- expecting: true,
103
+ timeout: timeout,
104
+ polling: polling,
89
105
  msg: "Timed out after waiting for the page to transition to #{page}."
90
106
  }
91
107
  wait_for(opts) { page.loaded? }
92
108
  page
93
109
  end
94
110
 
111
+ # Generic waiting method
112
+ # @param [Hash] opts
113
+ # Valid options:
114
+ # :timeout = Time to wait in seconds
115
+ # :polling = How often to poll
116
+ # :expecting = The expected result for the block to return
117
+ # :msg = The exception message if the timeout occurs
95
118
  def wait_for(opts={}, &block)
119
+ opts = {
120
+ timeout: @_waiting_timeout,
121
+ polling: @_waiting_polling,
122
+ expecting: true,
123
+ msg: "Timed out after waiting for #{@_waiting_timeout}s, polling every #{@_waiting_polling}s."
124
+ }.merge(opts) do |key, old, new|
125
+ new.nil? ? old : new
126
+ end
96
127
  Pagetience::Meditate.for(opts) { block.call }
97
128
  end
98
129
 
99
130
  private
100
131
 
101
- def determine_platform
102
- @element_platform = Pagetience::ElementPlatforms::Base.find(self)
103
-
104
- raise Pagetience::Exceptions::Platform, 'Could not determine what element platform is being used.' unless @element_platform
132
+ # Sets .current on the browser
133
+ def set_current_page
134
+ current_page = self
135
+ @browser.class.send(:define_method, :current_page) { current_page }
105
136
  end
106
137
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagetience
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek McNeil
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-29 00:00:00.000000000 Z
11
+ date: 2016-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -152,12 +152,10 @@ files:
152
152
  - README.md
153
153
  - Rakefile
154
154
  - lib/pagetience.rb
155
+ - lib/pagetience/configuration.rb
155
156
  - lib/pagetience/cucumber.rb
156
157
  - lib/pagetience/dsl.rb
157
- - lib/pagetience/exceptions.rb
158
158
  - lib/pagetience/meditate.rb
159
- - lib/pagetience/platforms/base.rb
160
- - lib/pagetience/platforms/element_platforms.rb
161
159
  - lib/pagetience/platforms/page-object-gem.rb
162
160
  - lib/pagetience/version.rb
163
161
  - pagetience.gemspec
@@ -1,9 +0,0 @@
1
- module Pagetience
2
- module Exceptions
3
- class Timeout < StandardError
4
- end
5
-
6
- class Platform < StandardError
7
- end
8
- end
9
- end
@@ -1,30 +0,0 @@
1
- module Pagetience
2
- module ElementPlatforms
3
- class Base
4
- attr_reader :browser
5
-
6
- class << self
7
- def find(klazz)
8
- valid_ancestor = ANCESTORS.find { |a| a.present? klazz }
9
- if valid_ancestor
10
- valid_ancestor.new klazz
11
- end
12
- end
13
- end
14
-
15
- def initialize(*args)
16
- @browser = nil
17
- end
18
-
19
- def platform_initialize(*args); end
20
-
21
- def underlying_element_for(sym)
22
- nil
23
- end
24
-
25
- def is_element_present?(sym)
26
- false
27
- end
28
- end
29
- end
30
- end
@@ -1,5 +0,0 @@
1
- module Pagetience
2
- module ElementPlatforms
3
- ANCESTORS = [Pagetience::ElementPlatforms::PageObjectGem]
4
- end
5
- end