Module: Capybara::WaitBeforeClick

Included in:
Node::Element
Defined in:
lib/capybara/wait_before_click.rb,
lib/capybara/wait_before_click/version.rb

Overview

Wait for image to load before clicking automatically

Constant Summary collapse

VERSION =
"0.2.2"

Instance Method Summary collapse

Instance Method Details

#_loggerObject



38
39
40
41
42
43
44
# File 'lib/capybara/wait_before_click.rb', line 38

def _logger
  @_logger ||= if defined?(Rails)
                 Rails.logger
               else
                 Logger.new("/dev/null")
               end
end

#_wait_for_image_loadingObject

rubocop:disable Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/capybara/wait_before_click.rb', line 9

def _wait_for_image_loading # rubocop:disable Metrics/MethodLength
  Timeout.timeout(Capybara.default_max_wait_time) do
    sleep 0.5 until evaluate_script(<<~JS)
      Array.prototype.every.call(
        document.querySelectorAll('img'),
        (e) => e.complete
      )
    JS
  end
rescue Timeout::Error
  _logger.debug "[capybara-wait_before_click]Timeout::Error"
rescue Capybara::NotSupportedByDriverError
  # It comes here when you run it in rack-test, but you can ignore it
rescue ::Selenium::WebDriver::Error::StaleElementReferenceError
  _logger.debug "[capybara-wait_before_click]Selenium::WebDriver::Error::StaleElementReferenceError"
  reload
  _wait_for_image_loading
end

#click(*keys, **options) ⇒ Object



28
29
30
31
# File 'lib/capybara/wait_before_click.rb', line 28

def click(*keys, **options)
  _wait_for_image_loading
  super
end

#hoverObject



33
34
35
36
# File 'lib/capybara/wait_before_click.rb', line 33

def hover
  _wait_for_image_loading
  super
end