Class: Dill::Checkpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/dill/checkpoint.rb

Overview

A point in time where some condition, or some set of conditions, should be verified.

Defined Under Namespace

Classes: ConditionNotMet, TimeFrozen

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wait_time = Capybara.default_wait_time) ⇒ Checkpoint

Initializes a new Checkpoint.

Parameters:

  • wait_time (defaults to: Capybara.default_wait_time)

    how long this checkpoint will wait for its conditions to be met, in seconds.



33
34
35
# File 'lib/dill/checkpoint.rb', line 33

def initialize(wait_time = Capybara.default_wait_time)
  @wait_time = wait_time
end

Instance Attribute Details

#wait_timeObject (readonly)

Returns the configured wait time, in seconds.

Returns:

  • the configured wait time, in seconds.



12
13
14
# File 'lib/dill/checkpoint.rb', line 12

def wait_time
  @wait_time
end

Class Method Details

.driverObject

Returns the Capybara driver in use.

Returns:

  • the Capybara driver in use.



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

def self.driver
  Capybara.current_session.driver
end

.wait_for(wait_time = Capybara.default_wait_time, raise_errors = true, &block) ⇒ Object

Executes block repeatedly until it returns a “truthy” value or timeout expires.

TODO: Expand documentation.



23
24
25
26
27
# File 'lib/dill/checkpoint.rb', line 23

def self.wait_for(wait_time = Capybara.default_wait_time,
                  raise_errors = true,
                  &block)
  new(wait_time).wait_for(raise_errors, &block)
end

Instance Method Details

#wait_for(raise_errors = true) { ... } ⇒ Object

Waits until the condition encapsulated by the block is met.

Automatically rescues some exceptions (Capybara::ElementNotFound, and driver specific exceptions) until #wait_time is exceeded. At that point it raises whatever exception was raised in the condition block, or ConditionNotMet, if no exception was raised inside the block. However, if raise_errors is set to false, returns false instead of propagating any of the automatically rescued exceptions.

If an “unknown” exception is raised, it is propagated immediately, without waiting for #wait_time to expire.

If a driver that doesn’t support waiting is used, any exception raised is immediately propagated.

Parameters:

  • raise_errors (Boolean) (defaults to: true)

    whether to propagate exceptions that are “rescuable” when #wait_time expires.

Yields:

  • a block encapsulating the condition to be evaluated.

Yield Returns:

  • a truthy value, if condition is met, a falsey value otherwise.

Returns:

  • whatever the condition block returns if the condition is successful. If the condition is not met, returns false if rescue_errors is false.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dill/checkpoint.rb', line 61

def wait_for(raise_errors = true, &condition)
  start

  begin
    yield or raise ConditionNotMet
  rescue *rescuable_errors => e
    if immediate?
      raise e if raise_errors

      return false
    end

    if expired?
      raise e if raise_errors

      return false
    end

    wait

    raise TimeFrozen, 'time appears to be frozen' if time_frozen?

    retry
  end
end