Module: WebDriverUtils

Defined in:
lib/webdriver_utils/wait.rb,
lib/webdriver_utils/page.rb,
lib/webdriver_utils/version.rb,
lib/webdriver_utils/silence_gem_warnings.rb

Overview

Constant Summary collapse

VERSION =
'0.0.6'
DATE =
'2015-06-07'

Class Method Summary collapse

Class Method Details

._generic_wait(opts = {}, &block) ⇒ Object

bubble - if set, the last exception will be raised if it exists Wait code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/webdriver_utils/wait.rb', line 12

def _generic_wait(opts = {}, &block)
  valid_keys   = [:timeout, :interval, :message, :ignore, :return_if_true, :bubble]
  invalid_keys = []
  opts.keys.each { |key| invalid_keys << key unless valid_keys.include?(key) }
  # [:one, :two] => :one, :two
  fail "Invalid keys #{invalid_keys.to_s[1..-2]}. Valid keys are #{valid_keys.to_s[1..-2]}" unless invalid_keys.empty?

  # ensure negative timeout and interval are set to zero.
  timeout        = opts.fetch(:timeout, 30)
  timeout        = timeout < 0 ? 0 : timeout
  interval       = opts.fetch(:interval, 0.2)
  interval       = interval < 0 ? 0 : interval
  message        = opts[:message]
  ignored        = Array(opts[:ignore] || ::Exception)
  return_if_true = opts[:return_if_true]
  bubble         = !!opts.fetch(:bubble, false)

  start_time = Time.now
  end_time   = start_time + timeout
  last_error = nil

  # design note,  ::TypeError, ::NameError, ::NoMethodError are not
  # rescued then raised like ECONNREFUSED because they can legitimately
  # occur inside a block and self correct depending on the method return
  # value. This is unlike ECONNREFUSED which will always fail.

  # use do..while format to ensure the block always executes at least once
  # even if timeout is zero.
  begin
    begin
      if return_if_true
        result = block.call
        return result if result
      else
        return block.call
      end
    rescue ::Errno::ECONNREFUSED => e
      raise e
    rescue *ignored => last_error # rubocop:disable Lint/HandleExceptions
      # swallowed
    end

    sleep interval
  end until Time.now > end_time

  elapsed_time    = (Time.now - start_time).round
  default_message = "timed out after #{elapsed_time} seconds (timeout: #{timeout})"

  if message
    msg = "#{message.dup} [#{default_message}]"
  else
    msg = default_message
  end

  msg << " (#{last_error.message})" if last_error

  fail_error = last_error if bubble
  fail_error ||= Selenium::WebDriver::Error::TimeOutError

  fail fail_error, msg
end

._process_wait_opts(opts) ⇒ Object

process opts before calling _generic_wait



75
76
77
78
79
# File 'lib/webdriver_utils/wait.rb', line 75

def _process_wait_opts(opts)
  opts = { timeout: opts } if opts.is_a?(Numeric)
  fail 'opts must be a hash' unless opts.is_a? Hash
  opts
end

.define_page_methods(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/webdriver_utils/page.rb', line 8

def define_page_methods opts={}
  page_module   = opts[:page_module] || raise('must set page_module')
  target_class  = opts[:target_class] || raise('must set target_class')
  driver_object = opts[:watir] || opts[:driver] || raise('must set driver')
  page_module.constants.each do |page_class|
    # ButtonsPage => buttons_page
    # https://github.com/rails/rails/blob/daaa21bc7d20f2e4ff451637423a25ff2d5e75c7/activesupport/lib/active_support/inflector/methods.rb#L96
    page_name = page_class.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
    target_class.send(:define_singleton_method, page_name) do
      page_module.const_get(page_class).new driver_object
    end
  end
end

.require_all_pages(glob_path = nil) ⇒ Object



3
4
5
6
# File 'lib/webdriver_utils/page.rb', line 3

def require_all_pages glob_path=nil
  glob_path ||= File.join(Rake.application.original_dir, 'page', '**', '*.rb')
  Dir.glob(glob_path) { |file| require_relative file }
end

.silence_gem_warningsObject



3
4
5
6
7
8
9
# File 'lib/webdriver_utils/silence_gem_warnings.rb', line 3

def silence_gem_warnings
  # https://github.com/appium/ruby_console/blob/1049c515432212f164cfbabd413d6e3d82e4aada/lib/appium_console.rb#L5
  Gem::Specification.class_eval do
    def self.warn args
    end
  end
end