Class: Appom::Wait

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/appom/wait.rb

Overview

Provides wait functionality with configurable timeout and retry interval.

The Wait class is used throughout Appom to wait for conditions to become true, such as waiting for elements to appear or disappear, or for elements to reach a certain state.

Examples:

Basic wait usage

wait = Appom::Wait.new(timeout: 10, interval: 0.5)
result = wait.until { some_condition }

Wait for element to be displayed

wait = Appom::Wait.new(timeout: 5)
wait.until { element.displayed? }

Author:

  • Harry.Tran

Since:

  • 0.1.0

Direct Known Subclasses

SmartWait::ConditionalWait

Constant Summary collapse

DEFAULT_TIMEOUT =

Default timeout in seconds

Since:

  • 0.1.0

5
DEFAULT_INTERVAL =

Default retry interval in seconds

Since:

  • 0.1.0

0.25

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

level, level=, #log_debug, #log_element_action, #log_error, #log_info, #log_wait_end, #log_wait_start, #log_warn, #logger

Constructor Details

#initialize(opts = {}) ⇒ Wait

Create a new Wait instance

Examples:

Create wait with custom timeout

wait = Appom::Wait.new(timeout: 10, interval: 1.0)

Parameters:

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

    Options for this instance

Options Hash (opts):

  • :timeout (Numeric) — default: 5

    Seconds to wait before timing out

  • :interval (Numeric) — default: 0.25

    Seconds to sleep between polls

Since:

  • 0.1.0



42
43
44
45
# File 'lib/appom/wait.rb', line 42

def initialize(opts = {})
  @timeout  = opts.fetch(:timeout, DEFAULT_TIMEOUT)
  @interval = opts.fetch(:interval, DEFAULT_INTERVAL)
end

Instance Attribute Details

#intervalObject (readonly)

Since:

  • 0.1.0



31
# File 'lib/appom/wait.rb', line 31

attr_reader :timeout, :interval

#timeoutNumeric (readonly)

Returns The timeout value in seconds.

Returns:

  • (Numeric)

    The timeout value in seconds



31
32
33
# File 'lib/appom/wait.rb', line 31

def timeout
  @timeout
end

Instance Method Details

#until { ... } ⇒ Object

Wait until the given block returns a truthy value

Examples:

Wait for element to appear

wait.until { page.find_element(:id, 'button') }

Wait with exception handling

wait.until do
  element = page.find_element(:id, 'button')
  element.displayed? && element.enabled?
end

Yields:

  • Block to execute repeatedly until it returns true

Returns:

  • (Object)

    The result of the block when it returns truthy

Raises:

  • (WaitError)

    If the timeout is reached before condition is met

  • (AppomError)

    Re-raises any Appom-specific errors from the block

Since:

  • 0.1.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/appom/wait.rb', line 62

def until(&)
  end_time = Time.now + @timeout
  error_message = ''
  last_error = nil
  start_time = Time.now

  log_wait_start('custom condition', @timeout)

  until Time.now > end_time
    begin
      result = yield
      if result
        duration = Time.now - start_time
        log_wait_end('custom condition', duration.round(3), success: true)
        return result
      end
    rescue StandardError => e
      last_error = e
      error_message = e.message
    end

    sleep @interval
  end

  duration = Time.now - start_time
  log_wait_end('custom condition', duration.round(3), success: false)

  # Handle exceptions differently based on type
  if last_error.is_a?(StandardError) && last_error.instance_of?(StandardError)
    # Wrap StandardError in WaitError with message included
    condition = "condition (last error: #{error_message})"
    raise Appom::WaitError.new(condition, @timeout)
  elsif last_error
    # Raise specific exceptions directly (ArgumentError, RuntimeError, etc.)
    raise last_error
  else
    # No exceptions, just condition never became true
    condition = error_message.empty? ? 'condition' : error_message
    raise Appom::WaitError.new(condition, @timeout)
  end
end