Module: Watir::SearchContext

Included in:
Element, ShadowRoot
Defined in:
lib/watir/search_context.rb

Instance Method Summary collapse

Instance Method Details

#assert_existsObject

Raises:



26
27
28
29
30
31
# File 'lib/watir/search_context.rb', line 26

def assert_exists
  locate unless located?
  return if located?

  raise unknown_exception, "unable to locate: #{inspect}"
end

#check_condition(condition, caller) ⇒ Object

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/watir/search_context.rb', line 79

def check_condition(condition, caller)
  Watir.logger.debug "<- `Verifying precondition #{inspect}##{condition} for #{caller}`"
  begin
    condition.nil? ? assert_exists : send(condition)
    Watir.logger.debug "<- `Verified precondition #{inspect}##{condition || 'assert_exists'}`"
  rescue unknown_exception
    raise unless condition.nil?

    Watir.logger.debug "<- `Unable to satisfy precondition #{inspect}##{condition}`"
    check_condition(:wait_for_exists, caller)
  end
end

#element_call(precondition = nil, &block) ⇒ Object

TODO: - this will get addressed with Watir::Executor implementation rubocop:disable Metrics/AbcSize

rubocop:disable Metrics/MethodLength # : #


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/watir/search_context.rb', line 48

def element_call(precondition = nil, &block)
  caller = caller_locations(1, 1)[0].label
  already_locked = browser.timer.locked?
  browser.timer = Wait::Timer.new(timeout: Watir.default_timeout) unless already_locked

  begin
    check_condition(precondition, caller)
    Watir.logger.debug "-> `Executing #{inspect}##{caller}`"
    yield
  rescue unknown_exception => e
    element_call(:wait_for_exists, &block) if precondition.nil?
    raise unknown_exception, e.message + custom_message
  rescue Selenium::WebDriver::Error::StaleElementReferenceError, Selenium::WebDriver::Error::NoSuchElementError
    reset!
    retry
    # TODO: - InvalidElementStateError is deprecated, so no longer calling `raise_disabled`
    # need a better way to handle this
  rescue Selenium::WebDriver::Error::ElementNotInteractableError
    raise_present unless browser.timer.remaining_time.positive?
    raise_present unless %i[wait_for_present wait_for_enabled wait_for_writable].include?(precondition)
    retry
  rescue Selenium::WebDriver::Error::NoSuchWindowError
    raise Exception::NoMatchingWindowFoundException, 'browser window was closed'
  ensure
    Watir.logger.debug "<- `Completed #{inspect}##{caller}`"
    browser.timer.reset! unless already_locked
  end
end

#exists?Boolean Also known as: exist?

Returns true if element exists. Checking for staleness is deprecated

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/watir/search_context.rb', line 12

def exists?
  if located? && stale?
    reset!
  elsif located?
    return true
  end

  assert_exists
  true
rescue Exception::UnknownObjectException, Exception::UnknownFrameException
  false
end

#unknown_exceptionObject



92
93
94
# File 'lib/watir/search_context.rb', line 92

def unknown_exception
  Exception::UnknownObjectException
end

#wait_for_existsObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/watir/search_context.rb', line 33

def wait_for_exists
  return if located? # Performance shortcut

  begin
    @query_scope.wait_for_exists unless @query_scope.is_a? Browser
    wait_until(element_reset: true, &:exists?)
  rescue Wait::TimeoutError
    msg = "timed out after #{Watir.default_timeout} seconds, waiting for #{inspect} to be located"
    raise unknown_exception, msg
  end
end