Module: QAT::Web::Elements::Waiters

Includes:
Logger, Config, Selector
Included in:
QAT::Web::Elements, Page
Defined in:
lib/qat/web/elements/waiters.rb

Overview

Helper methods for handling timeouts and wait periods

Since:

  • 1.0.0

Instance Method Summary collapse

Methods included from Config

#valid_config?

Methods included from Selector

#selector

Methods included from Configuration

last_access, #last_access, last_access=, #parse_configuration

Instance Method Details

#timeoutsHashWithIndifferentAccess

Returns the timeouts configuration

Returns:

  • (HashWithIndifferentAccess)

Since:

  • 1.0.0



40
41
42
# File 'lib/qat/web/elements/waiters.rb', line 40

def timeouts
  @timeouts
end

#timeouts_config(timeouts) ⇒ HashWithIndifferentAccess

Defines timeouts through a configuration hash

Parameters:

  • timeouts (Hash)

    timeouts configuration

Returns:

  • (HashWithIndifferentAccess)

Since:

  • 1.0.0



33
34
35
36
# File 'lib/qat/web/elements/waiters.rb', line 33

def timeouts_config(timeouts)
  valid_config?(timeouts, 'timeouts')
  @timeouts = HashWithIndifferentAccess.new(timeouts)
end

#timeouts_file(path) ⇒ HashWithIndifferentAccess

Defines timeouts through a configuration file path

Parameters:

  • path (String)

    path to timeouts configuration file

Returns:

  • (HashWithIndifferentAccess)

Raises:

  • (ArgumentError)

Since:

  • 1.0.0



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/qat/web/elements/waiters.rb', line 18

def timeouts_file(path)
  raise(ArgumentError, "File '#{path}' does not exist!") unless File.exist?(path)
  @timeouts_file = path
  begin
    config = YAML.load(File.read(@timeouts_file)) || {}
  rescue Psych::SyntaxError
    log.error "Failed to load file '#{@timeouts_file}'."
    raise
  end
  timeouts_config(config)
end

#wait_until(timeout, klass = nil) { ... } ⇒ Object

Generic wait method

Parameters:

  • timeout (Integer)

    max time to wait

  • klass (Class) (defaults to: nil)

    (nil) an optional exception class to retry on error

Yields:

Since:

  • 1.0.0



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/qat/web/elements/waiters.rb', line 80

def wait_until(timeout, klass = nil, &block)
  default_time = Capybara.default_max_wait_time
  timeout      ||= default_time
  tries        = timeout / default_time
  exceptions   = [Capybara::ElementNotFound, Capybara::ExpectationNotMet]
  exceptions << klass if klass
  Retriable.retriable(on:            exceptions,
                      tries:         tries.ceil,
                      base_interval: default_time) do
    yield
  end
end

#wait_until_not_present(selector, timeout) ⇒ Object

Waits a maximum timeout time until an element is no longer present on page

Parameters:

  • selector (Hash)

    element selector

  • timeout (Integer)

    maximum time to wait

Since:

  • 1.0.0



63
64
65
66
# File 'lib/qat/web/elements/waiters.rb', line 63

def wait_until_not_present(selector, timeout)
  type, value, options = build_selector(selector, { wait: timeout })
  has_no_selector?(type, value, **options)
end

#wait_until_not_visible(selector, timeout) ⇒ Object

Waits a maximum timeout time until an element is no longer visible on page

Parameters:

  • selector (Hash)

    element selector

  • timeout (Integer)

    maximum time to wait

Since:

  • 1.0.0



71
72
73
74
# File 'lib/qat/web/elements/waiters.rb', line 71

def wait_until_not_visible(selector, timeout)
  type, value, options = build_selector(selector, { visible: false, wait: timeout })
  has_no_selector?(type, value, **options)
end

#wait_until_present(selector, timeout) ⇒ Object

Waits a maximum timeout time until an element is present on page

Parameters:

  • selector (Hash)

    element selector

  • timeout (Integer)

    maximum time to wait

Since:

  • 1.0.0



47
48
49
50
# File 'lib/qat/web/elements/waiters.rb', line 47

def wait_until_present(selector, timeout)
  type, value, options = build_selector(selector, { visible: false, wait: timeout })
  has_selector?(type, value, **options)
end

#wait_until_visible(selector, timeout) ⇒ Object

Waits a maximum timeout time until an element is visible on page

Parameters:

  • selector (Hash)

    element selector

  • timeout (Integer)

    maximum time to wait

Since:

  • 1.0.0



55
56
57
58
# File 'lib/qat/web/elements/waiters.rb', line 55

def wait_until_visible(selector, timeout)
  type, value, options = build_selector(selector, { visible: true, wait: timeout })
  has_selector?(type, value, **options)
end