site_prism 3.0.beta → 3.0

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
  SHA256:
3
- metadata.gz: c72504b3ebac7b0209d022bfe8851bc1cedfebfae96b1647ae5fdb043282c49e
4
- data.tar.gz: c6432642fb9b5ec2a7a3405a5011e66c8f76c673b5eb14971a3117ef48eda3b4
3
+ metadata.gz: e9eaed0d98cdcffc5d23f9c8cadf03b5cb343a5f8e65b7f9d96334442112c115
4
+ data.tar.gz: 5286b415093777314940312fff696f080b980eefdb4f9d9f4134559da56155ea
5
5
  SHA512:
6
- metadata.gz: 75baa0fcc7a0844bc8eda684fcb7943bf67421e30f30b8e9d200c26021f7f6d303fae4d7b43355a89784e19578916113effc5fceb1ce9a1678b5b77ce3ccff50
7
- data.tar.gz: bf020114f3bb95a330dcfb05d056f39bb299a7135ea80c75896bda135c5dd67058fe60549a624de6dc8da91cc2ee834b759556163c40ecf33e856045cbcca72b
6
+ metadata.gz: 889e8c4aad04e3ba37d2742cdaeae1234a1665297c2fd295495fedf91eeefc7b0e21a118a63c4b86a7d2492a07685b92cf39792b6bc781a3e5e82b03720090ae
7
+ data.tar.gz: 828b1e6e1693c506dc082babb9bc51049a28d32aa04ea37483f8a2d89cde8e63a6392b0f4b85b52d33d22b084234d6b3ecb81e3941e6b4e7a4fe688c5469285c
data/LICENSE.md CHANGED
@@ -1,4 +1,5 @@
1
- Copyright (c) 2011-2018, Nathaniel Ritmeyer
1
+ Copyright (c) 2011-2018, Nathaniel Ritmeyer & the Site Prism team
2
+
2
3
  All rights reserved.
3
4
 
4
5
  Redistribution and use in source and binary forms, with or without
@@ -4,17 +4,27 @@ require 'site_prism/error'
4
4
  require 'addressable/template'
5
5
 
6
6
  module SitePrism
7
- autoload :ElementContainer, 'site_prism/element_container'
7
+ autoload :AddressableUrlMatcher, 'site_prism/addressable_url_matcher'
8
8
  autoload :ElementChecker, 'site_prism/element_checker'
9
+ autoload :ElementContainer, 'site_prism/element_container'
10
+ autoload :Logger, 'site_prism/logger'
9
11
  autoload :Page, 'site_prism/page'
10
12
  autoload :Section, 'site_prism/section'
11
13
  autoload :Waiter, 'site_prism/waiter'
12
- autoload :AddressableUrlMatcher, 'site_prism/addressable_url_matcher'
13
14
 
14
15
  class << self
16
+ attr_writer :enable_logging
17
+
15
18
  def configure
16
- warn 'SitePrism configuration is now removed.'
17
- warn 'All options fed directly from Capybara.'
19
+ yield self
20
+ end
21
+
22
+ def logger
23
+ @logger ||= SitePrism::Logger.new.create
24
+ end
25
+
26
+ def enable_logging
27
+ @enable_logging ||= false
18
28
  end
19
29
  end
20
30
  end
@@ -33,6 +33,7 @@ module SitePrism
33
33
  def matches?(url, expected_mappings = {})
34
34
  actual_mappings = mappings(url)
35
35
  return false unless actual_mappings
36
+
36
37
  expected_mappings.empty? ||
37
38
  all_expected_mappings_match?(expected_mappings, actual_mappings)
38
39
  end
@@ -76,13 +77,16 @@ module SitePrism
76
77
  def component_matches(component, uri)
77
78
  component_template = component_templates[component]
78
79
  return {} unless component_template
80
+
79
81
  component_url = uri.public_send(component).to_s
80
82
  mappings = component_template.extract(component_url)
81
83
  return mappings if mappings
84
+
82
85
  # to support Addressable's expansion of queries
83
86
  # ensure it's parsing the fragment as appropriate (e.g. {?params*})
84
87
  prefix = component_prefixes[component]
85
88
  return nil unless prefix
89
+
86
90
  component_template.extract(prefix + component_url)
87
91
  end
88
92
 
@@ -90,12 +94,11 @@ module SitePrism
90
94
  # the template slugs with nonsense strings.
91
95
  def to_substituted_uri
92
96
  url = pattern
93
- substitutions.each_pair do |slug, value|
94
- url = url.sub(slug, value)
95
- end
97
+ substitutions.each_pair { |slug, value| url = url.sub(slug, value) }
96
98
  begin
97
99
  Addressable::URI.parse(url)
98
100
  rescue Addressable::URI::InvalidURIError
101
+ SitePrism.logger.warn("Ensure you don't use templated port numbers.")
99
102
  raise SitePrism::InvalidUrlMatcherError
100
103
  end
101
104
  end
@@ -3,11 +3,11 @@
3
3
  module SitePrism
4
4
  module ElementChecker
5
5
  def all_there?
6
- elements_to_check.all? { |element| present?(element) }
6
+ elements_to_check.all? { |element| there?(element) }
7
7
  end
8
8
 
9
9
  def elements_present
10
- mapped_items.select { |item_name| present?(item_name) }
10
+ mapped_items.select { |item_name| there?(item_name) }
11
11
  end
12
12
 
13
13
  private
@@ -26,7 +26,7 @@ module SitePrism
26
26
  self.class.mapped_items.uniq
27
27
  end
28
28
 
29
- def present?(element)
29
+ def there?(element)
30
30
  send("has_#{element}?")
31
31
  end
32
32
  end
@@ -14,31 +14,44 @@ module SitePrism
14
14
 
15
15
  def raise_if_block(obj, name, has_block, type)
16
16
  return unless has_block
17
- warn "Type passed in: #{type}"
18
17
 
19
- raise SitePrism::UnsupportedBlockError, "#{obj.class}##{name}"
18
+ SitePrism.logger.debug("Type passed in: #{type}")
19
+ SitePrism.logger.warn('section / iFrame can only accept blocks.')
20
+ SitePrism.logger.error("#{obj.class}##{name} does not accept blocks")
21
+
22
+ raise SitePrism::UnsupportedBlockError
20
23
  end
21
24
 
22
25
  # Sanitize method called before calling any SitePrism DSL method or
23
26
  # meta-programmed method. This ensures that the Capybara query is correct.
27
+ #
24
28
  # Accepts any combination of arguments sent at DSL definition or runtime
25
29
  # and combines them in such a way that Capybara can operate with them.
26
- # Initially it will duplicate all locators and run-time arguments,
27
- # then it will combine them with any visibility arguments if defined.
28
30
  def merge_args(find_args, runtime_args, visibility_args = {})
29
31
  find_args = find_args.dup
30
32
  runtime_args = runtime_args.dup
33
+ options = visibility_args.dup
34
+ SitePrism.logger.debug("Initial args: #{find_args}, #{runtime_args}.")
31
35
 
32
- options = visibility_args
33
- options.merge!(find_args.pop) if find_args.last.is_a? Hash
34
- options.merge!(runtime_args.pop) if runtime_args.last.is_a? Hash
35
- options[:wait] = wait_time unless wait_key_present?(options)
36
+ recombine_args(find_args, runtime_args, options)
36
37
 
37
38
  return [*find_args, *runtime_args] if options.empty?
38
39
 
39
40
  [*find_args, *runtime_args, options]
40
41
  end
41
42
 
43
+ # Options re-combiner. This takes the original inputs and combines
44
+ # them such that there is only one hash passed as a final argument
45
+ # to Capybara.
46
+ #
47
+ # If the hash is empty, then the hash is omitted from the payload sent
48
+ # to Capybara, and the find / runtime arguments are sent alone.
49
+ def recombine_args(find_args, runtime_args, options)
50
+ options.merge!(find_args.pop) if find_args.last.is_a? Hash
51
+ options.merge!(runtime_args.pop) if runtime_args.last.is_a? Hash
52
+ options[:wait] = wait_time unless wait_key_present?(options)
53
+ end
54
+
42
55
  def wait_key_present?(options)
43
56
  options.key?(:wait)
44
57
  end
@@ -114,7 +127,7 @@ module SitePrism
114
127
 
115
128
  def build(name, *find_args)
116
129
  if find_args.empty?
117
- create_no_selector(name)
130
+ create_error_method(name)
118
131
  else
119
132
  add_to_mapped_items(name)
120
133
  yield
@@ -136,7 +149,7 @@ module SitePrism
136
149
 
137
150
  def create_helper_method(proposed_method_name, *find_args)
138
151
  if find_args.empty?
139
- create_no_selector(proposed_method_name)
152
+ create_error_method(proposed_method_name)
140
153
  else
141
154
  yield
142
155
  end
@@ -168,6 +181,7 @@ module SitePrism
168
181
  define_method(method_name) do |*runtime_args|
169
182
  args = merge_args(find_args, runtime_args, visible: true)
170
183
  return true if element_exists?(*args)
184
+
171
185
  raise SitePrism::ElementVisibilityTimeoutError
172
186
  end
173
187
  end
@@ -179,14 +193,17 @@ module SitePrism
179
193
  define_method(method_name) do |*runtime_args|
180
194
  args = merge_args(find_args, runtime_args, visible: true)
181
195
  return true if element_does_not_exist?(*args)
196
+
182
197
  raise SitePrism::ElementInvisibilityTimeoutError
183
198
  end
184
199
  end
185
200
  end
186
201
 
187
- def create_no_selector(method_name)
188
- define_method(method_name) do
189
- raise SitePrism::InvalidElementError, "#{self.class}##{method_name}"
202
+ def create_error_method(name)
203
+ SitePrism.logger.debug("#{name} has come from an item with 0 locators.")
204
+
205
+ define_method(name) do
206
+ raise SitePrism::InvalidElementError
190
207
  end
191
208
  end
192
209
 
@@ -16,40 +16,28 @@ module SitePrism
16
16
  class NoUrlMatcherForPageError < PageLoadError; end
17
17
 
18
18
  # The URL matcher was not recognised as a Regex or String and as such
19
- # it couldn't be parsed by Addressable
19
+ # it couldn't be parsed by Addressable. It also could be caused by
20
+ # the usage of templated port numbers - which aren't supported
20
21
  # Formerly known as `InvalidUrlMatcher`
21
- class InvalidUrlMatcherError < PageLoadError
22
- def message
23
- warn 'Templated port numbers are unsupported.'
24
-
25
- 'Your URL and/or matcher could not be interpreted.'
26
- end
27
- end
22
+ class InvalidUrlMatcherError < PageLoadError; end
28
23
 
29
24
  # A SitePrism defined DSL item was defined without a selector
30
25
  # Formerly known as `NoSelectorForElement`
31
- class InvalidElementError < SitePrismError
32
- def message
33
- "#{super} has been derived from an item with no selectors defined."
34
- end
35
- end
26
+ class InvalidElementError < SitePrismError; end
36
27
 
37
28
  # The condition that was being evaluated inside the block did not evaluate
38
29
  # to true within the time limit
39
30
  # Formerly known as `TimeoutException`
40
31
  class TimeoutError < SitePrismError; end
41
32
 
42
- # These errors are not yet migrated and are fired from their source
43
- # They are raised when the meta-programmed method has not yielded true
44
- # in the prescribed time limit
45
- # Formerly known as `TimeOutWaitingForExistenceError`,
46
- # `TimeOutWaitingForNonExistenceError`
47
- # `TimeOutWaitingForElementVisibility` and
48
- # `TimeOutWaitingForElementInvisibility` respectively
49
-
50
- class ExistenceTimeoutError < TimeoutError; end
51
- class NonExistenceTimeoutError < TimeoutError; end
33
+ # The wait_until_*_visible meta-programmed method didn't evaluate to true
34
+ # within the prescribed time limit
35
+ # Formerly known as `TimeOutWaitingForElementVisibility`
52
36
  class ElementVisibilityTimeoutError < TimeoutError; end
37
+
38
+ # The wait_until_*_invisible meta-programmed method didn't evaluate to true
39
+ # within the prescribed time limit
40
+ # Formerly known as `TimeOutWaitingForElementInvisibility`
53
41
  class ElementInvisibilityTimeoutError < TimeoutError; end
54
42
 
55
43
  # Generic Block validation family of errors inherit from this error
@@ -57,32 +45,13 @@ module SitePrism
57
45
 
58
46
  # A Block was passed to the method, which it cannot interpret
59
47
  # Formerly known as `UnsupportedBlock`
60
- class UnsupportedBlockError < BlockError
61
- def message
62
- warn 'section and iframe are the only items which can accept a block.'
63
-
64
- "#{super} does not accept blocks."
65
- end
66
- end
48
+ class UnsupportedBlockError < BlockError; end
67
49
 
68
- # A Block was required, but not passed into the iframe at runtime
50
+ # A Block was required, but not supplied
69
51
  # Formerly known as `BlockMissingError`
70
- class MissingBlockError < BlockError
71
- def message
72
- 'You can only use iFrames in a block context - Please pass in a block.'
73
- end
74
- end
52
+ class MissingBlockError < BlockError; end
75
53
 
76
- # A page was loaded via #load - And then failed one of the load validations
77
- # that was either pre-defined or defined by the user
54
+ # A page was loaded then failed one of the validations defined by the user
78
55
  # Formerly known as `NotLoadedError`
79
- class FailedLoadValidationError < PageLoadError
80
- def message
81
- if super == self.class.to_s
82
- 'Failed to load - No reason specified.'
83
- else
84
- "Failed to load. Reason: #{super}"
85
- end
86
- end
87
- end
56
+ class FailedLoadValidationError < PageLoadError; end
88
57
  end
@@ -53,19 +53,22 @@ module SitePrism
53
53
  # Executes the given block after the page is loaded.
54
54
  #
55
55
  # The loadable object instance is yielded into the block.
56
- #
57
- # @param block [&block] The block to be executed once the page
58
- # has finished loading.
59
- def when_loaded(&_block)
56
+ def when_loaded
60
57
  # Get original loaded value, in case we are nested
61
58
  # inside another when_loaded block.
62
59
  previously_loaded = loaded
63
- message = 'A block was expected, but none received.'
64
- raise ArgumentError, message unless block_given?
65
60
 
66
- # Within the block, cache loaded? to optimize performance.
61
+ # Ensure we have passed in a block. else stop the system.
62
+ # This would be because a user has not defined the load validation
63
+ # correctly (i.e. with a block argument)
64
+ throw_missing_block_error unless block_given?
65
+
66
+ # Within the block, check (and cache) loaded?, to see whether the
67
+ # page has indeed loaded according to the rules defined by the user.
67
68
  self.loaded = loaded?
68
69
 
70
+ # If the page hasn't loaded. Then crash and return the error message.
71
+ # If one isn't defined, just return the Error code.
69
72
  raise SitePrism::FailedLoadValidationError, load_error unless loaded
70
73
 
71
74
  yield self
@@ -99,5 +102,11 @@ module SitePrism
99
102
  passed
100
103
  end
101
104
  end
105
+
106
+ def throw_missing_block_error
107
+ SitePrism.logger.debug('This code point should not be reachable')
108
+ SitePrism.logger.error('A block was expected, but none received.')
109
+ raise SitePrism::MissingBlockError
110
+ end
102
111
  end
103
112
  end
@@ -0,0 +1,35 @@
1
+ require 'logger'
2
+
3
+ module SitePrism
4
+ # To enable full logging
5
+ # SitePrism.enable_logging = true
6
+ #
7
+ # To disable all logging (Done by default)
8
+ # SitePrism.enable_logging = false
9
+ #
10
+ # To Manually log a message
11
+ # SitePrism.logger.info('Information')
12
+ # SitePrism.logger.debug('Input debug message')
13
+ class Logger
14
+ def create(output = $stdout)
15
+ logger = ::Logger.new(output)
16
+ logger.progname = 'SitePrism'
17
+ logger.level = log_level
18
+ logger.formatter = proc do |severity, time, progname, msg|
19
+ "#{time.strftime('%F %T')} - #{severity} - #{progname} - #{msg}\n"
20
+ end
21
+
22
+ logger
23
+ end
24
+
25
+ private
26
+
27
+ def log_level
28
+ if SitePrism.enable_logging
29
+ 0 # This is equivalent to debug standard logging
30
+ else
31
+ 5 # This is equivalent to unknown (or no), logging
32
+ end
33
+ end
34
+ end
35
+ end
@@ -9,6 +9,22 @@ module SitePrism
9
9
  include Loadable
10
10
  include ElementContainer
11
11
 
12
+ class << self
13
+ attr_reader :url
14
+
15
+ def set_url(page_url)
16
+ @url = page_url.to_s
17
+ end
18
+
19
+ def set_url_matcher(page_url_matcher)
20
+ @url_matcher = page_url_matcher
21
+ end
22
+
23
+ def url_matcher
24
+ @url_matcher || url
25
+ end
26
+ end
27
+
12
28
  # When instantiating the page. A default validation will be added to all
13
29
  # validations you define and add to the Page Class.
14
30
  # When calling #load, all of the validations that are set will be ran
@@ -34,6 +50,7 @@ module SitePrism
34
50
  else
35
51
  expanded_url = url(expansion_or_html)
36
52
  raise SitePrism::NoUrlForPageError unless expanded_url
53
+
37
54
  visit expanded_url
38
55
  when_loaded(&block) if block_given?
39
56
  end
@@ -42,8 +59,8 @@ module SitePrism
42
59
  def displayed?(*args)
43
60
  expected_mappings = args.last.is_a?(::Hash) ? args.pop : {}
44
61
  seconds = !args.empty? ? args.first : Capybara.default_max_wait_time
45
-
46
62
  raise SitePrism::NoUrlMatcherForPageError unless url_matcher
63
+
47
64
  begin
48
65
  Waiter.wait_until_true(seconds) { url_matches?(expected_mappings) }
49
66
  rescue SitePrism::TimeoutError
@@ -61,24 +78,9 @@ module SitePrism
61
78
  end
62
79
  end
63
80
 
64
- def self.set_url(page_url)
65
- @url = page_url.to_s
66
- end
67
-
68
- def self.set_url_matcher(page_url_matcher)
69
- @url_matcher = page_url_matcher
70
- end
71
-
72
- class << self
73
- attr_reader :url
74
- end
75
-
76
- def self.url_matcher
77
- @url_matcher || url
78
- end
79
-
80
81
  def url(expansion = {})
81
82
  return nil if self.class.url.nil?
83
+
82
84
  Addressable::Template.new(self.class.url).expand(expansion).to_s
83
85
  end
84
86
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SitePrism
4
- VERSION = '3.0.beta'.freeze
4
+ VERSION = '3.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: site_prism
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.beta
4
+ version: '3.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nat Ritmeyer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-09-15 00:00:00.000000000 Z
12
+ date: 2018-10-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
@@ -17,34 +17,34 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '2.4'
20
+ version: '2.5'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '2.4'
27
+ version: '2.5'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: capybara
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: '2.15'
34
+ version: '2.17'
35
35
  - - "<"
36
36
  - !ruby/object:Gem::Version
37
- version: '3.6'
37
+ version: '4.0'
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: '2.15'
44
+ version: '2.17'
45
45
  - - "<"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.6'
47
+ version: '4.0'
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: cucumber
50
50
  requirement: !ruby/object:Gem::Requirement
@@ -161,6 +161,7 @@ files:
161
161
  - lib/site_prism/element_container.rb
162
162
  - lib/site_prism/error.rb
163
163
  - lib/site_prism/loadable.rb
164
+ - lib/site_prism/logger.rb
164
165
  - lib/site_prism/page.rb
165
166
  - lib/site_prism/section.rb
166
167
  - lib/site_prism/version.rb
@@ -183,9 +184,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
184
  version: '2.2'
184
185
  required_rubygems_version: !ruby/object:Gem::Requirement
185
186
  requirements:
186
- - - ">"
187
+ - - ">="
187
188
  - !ruby/object:Gem::Version
188
- version: 1.3.1
189
+ version: '0'
189
190
  requirements: []
190
191
  rubyforge_project:
191
192
  rubygems_version: 2.7.7