capybara-maleficent 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: cfbcc71d8ada1e5f7ddf2fb70bb49469f8e40a30
4
- data.tar.gz: 6b78006bcf7d237663123e96f1d2746a2f07d655
3
+ metadata.gz: b4abcd6ff72afa32a781cb14dbc05438a1d345df
4
+ data.tar.gz: 116b62e770c02a77da50cf43374264907ef2bf10
5
5
  SHA512:
6
- metadata.gz: 2de676fc848823e78636caa79de294b1a762dfe650f550e010f2faca6eb52856d300e52f36ce9000767cf51f83d71b7e94b5422888473b6eff43fa5f9db67054
7
- data.tar.gz: a48b6c95260a9fbd1ddf777c997e2fa739a4a6c0d90d40cc163404f8b198426216174cda112332df1f4f47c17996eeef1ca8df8d934b724c8dc8360987a004ec
6
+ metadata.gz: b74d54dd452882d65b3e1b195baf08dac8867f2538a4f415f84bcf5a366937a582e3ab9b1a30c146b4904ad4d36a125d8b1ec71cf9093385b005132323696025
7
+ data.tar.gz: 7bc24fcd7030ea844a7d4351d059c6789f8d0e797e9a3945205bceefbd1ad833b4f71c1178d3253f67055b0425454eae1d672a26e392e5463c4322ac9ec1d94c
@@ -7,13 +7,15 @@ module Capybara
7
7
  #
8
8
  # A method that wraps the given block in a retry sleep strategy. Of particular use for waiting on Capybara.
9
9
  #
10
- # @param [#sleep] the_sleeper - What will be doing the sleeping; default is Kernel
11
- # @param [Array<Integer>] sleep_durations - The size of the array represents the max number of sleep attempts, each element represents how long
12
- # @param [Array<Exceptions] handled_exceptions - Which exceptions should the sleep injector handle
10
+ # @param the_sleeper [#sleep] What will be doing the sleeping; default is Kernel
11
+ # @param logger [#debug, #info]
12
+ # @param sleep_durations [Array<Integer>] The size of the array represents the max number of sleep attempts, each element represents how long
13
+ # @param handled_exceptions [Array<Exceptions] Which exceptions should the sleep injector handle
13
14
  # @yield The block of code for which we will allow sleeping
14
15
  # @return The value of the given block
15
16
  # @see ./spec/lib/capybara/maleficent_spec.rb for specifications of behavior
16
- def self.with_sleep_injection(the_sleeper: Kernel, sleep_durations: configuration.sleep_durations, handled_exceptions: configuration.handled_exceptions)
17
+ def self.with_sleep_injection(the_sleeper: Kernel, logger: configuration.logger, sleep_durations: configuration.sleep_durations, handled_exceptions: configuration.handled_exceptions)
18
+ logger.debug "Starting Capybara::Maleficent.with_sleep_injection"
17
19
  sleep_durations = sleep_durations.clone
18
20
  last_try_sleep_duration = sleep_durations.pop
19
21
  return_value = nil
@@ -23,13 +25,21 @@ module Capybara
23
25
  return_value = yield.tap { success = true }
24
26
  break if success
25
27
  rescue *handled_exceptions
28
+ logger.info "Sleeping for #{sleep_duration} via Capybara::Maleficent.with_sleep_injection (for #{handled_exceptions.inspect})"
26
29
  the_sleeper.sleep(sleep_duration)
27
30
  next
28
31
  end
29
32
  end
30
- return return_value if success
31
- the_sleeper.sleep(last_try_sleep_duration)
32
- yield
33
+ if success
34
+ logger.debug "Ending Capybara::Maleficent.with_sleep_injection"
35
+ return return_value
36
+ else
37
+ logger.info "Sleeping for #{last_try_sleep_duration}via Capybara::Maleficent.with_sleep_injection (for #{handled_exceptions.inspect}). This is the last try."
38
+ the_sleeper.sleep(last_try_sleep_duration)
39
+ yield.tap do
40
+ logger.debug "Ending Capybara::Maleficent.with_sleep_injection"
41
+ end
42
+ end
33
43
  end
34
44
 
35
45
  def self.default_sleeper
@@ -1,4 +1,5 @@
1
1
  require 'active_support/core_ext/array/wrap'
2
+ require 'capybara/maleficent/logger'
2
3
 
3
4
  module Capybara
4
5
  module Maleficent
@@ -7,10 +8,13 @@ module Capybara
7
8
  def initialize(**kargs)
8
9
  self.sleep_durations = kargs.fetch(:sleep_durations) { [3, 10] }
9
10
  self.handled_exceptions = kargs.fetch(:handled_exceptions) { [] }
11
+ self.logger = kargs.fetch(:logger) { Capybara::Maleficent::Logger.new }
10
12
  end
11
13
 
12
14
  attr_accessor :handled_exceptions
13
15
 
16
+ attr_accessor :logger
17
+
14
18
  # Used to determine the number of sleeps to attempt, and the duration of each of those sleeps.
15
19
  # @return An array of integers
16
20
  attr_reader :sleep_durations
@@ -0,0 +1,21 @@
1
+ module Capybara
2
+ module Maleficent
3
+ class Logger
4
+ def info(*args)
5
+ $stdout.puts("INFO: #{args.inspect}")
6
+ end
7
+
8
+ def debug(*args)
9
+ $stdout.puts("DEBUG: #{args.inspect}")
10
+ end
11
+
12
+ def error(*args)
13
+ $stdout.puts("ERROR: #{args.inspect}")
14
+ end
15
+
16
+ def warn(*args)
17
+ $stdout.puts("WARN: #{args.inspect}")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -5,36 +5,27 @@ unless defined?(Capybara::Maleficent::Spindle)
5
5
  module Capybara::Maleficent::Spindle
6
6
  end
7
7
 
8
- require "capybara/rspec/matchers"
9
8
  require "capybara/node/matchers"
10
-
11
- # Re-open these classes as `super` was not working
12
- class Capybara::RSpecMatchers::Matcher
13
- def wrap_matches?(actual)
14
- Capybara::Maleficent.with_sleep_injection(handled_exceptions: [Capybara::ExpectationNotMet]) do
15
- wrap(actual)
16
- end
17
- rescue Capybara::ExpectationNotMet => e
18
- @failure_message = e.message
19
- return false
20
- end
21
- end
22
-
23
9
  module Capybara::Node::Matchers
24
- def has_selector?(*args, &optional_filter_block)
25
- Capybara::Maleficent.with_sleep_injection(handled_exceptions: [Capybara::ExpectationNotMet]) do
26
- assert_selector(*args, &optional_filter_block)
27
- end
28
- rescue Capybara::ExpectationNotMet
29
- false
30
- end
31
-
32
- def has_no_selector?(*args, &optional_filter_block)
33
- Capybara::Maleficent.with_sleep_injection(handled_exceptions: [Capybara::ExpectationNotMet]) do
34
- assert_no_selector(*args, &optional_filter_block)
10
+ # I was encountering issues with module inclusion and super method. This is
11
+ # an alternative to super. I grab the method (in this case instance method
12
+ # because it's a module), redefine the method to wrap the original method.
13
+ #
14
+ # http://blog.jayfields.com/2008/04/alternatives-for-redefining-methods.html
15
+ [
16
+ :assert_selector,
17
+ :assert_no_selector,
18
+ :assert_matches_selector,
19
+ :assert_not_matches_selector,
20
+ :assert_text,
21
+ :assert_no_text
22
+ ].each do |method_name|
23
+ original_method = instance_method(method_name)
24
+ define_method method_name do |*args, &block|
25
+ Capybara::Maleficent.with_sleep_injection(handled_exceptions: [Capybara::ExpectationNotMet]) do
26
+ original_method.bind(self).call(*args, &block)
27
+ end
35
28
  end
36
- rescue Capybara::ExpectationNotMet
37
- false
38
29
  end
39
30
  end
40
31
  end
@@ -1,5 +1,5 @@
1
1
  module Capybara
2
2
  module Maleficent
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-maleficent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Friesen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-21 00:00:00.000000000 Z
11
+ date: 2017-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -102,6 +102,7 @@ files:
102
102
  - lib/capybara-maleficent.rb
103
103
  - lib/capybara/maleficent.rb
104
104
  - lib/capybara/maleficent/configuration.rb
105
+ - lib/capybara/maleficent/logger.rb
105
106
  - lib/capybara/maleficent/spindle.rb
106
107
  - lib/capybara/maleficent/version.rb
107
108
  - lib/capybara_maleficent.rb