Module: DVLA::Browser::Drivers

Defined in:
lib/dvla/browser/drivers/version.rb,
lib/dvla/browser/drivers/meta_drivers.rb

Constant Summary collapse

VERSION =
'3.0.0'
DRIVER_REGEX =
/^(?:(?<headless>headless)_)?(?<driver>(selenium_(?<browser>chrome|firefox|edge)|cuprite|apparition))$/
OTHER_ACCEPTED_PARAMS =
%i[timeout].freeze
OTHER_DRIVERS =
%i[cuprite apparition].freeze
SELENIUM_ACCEPTED_PARAMS =
%i[remote additional_arguments additional_preferences].freeze
SELENIUM_DRIVERS =
%i[selenium_chrome selenium_firefox selenium_edge].freeze

Class Method Summary collapse

Class Method Details

.method_missing(method, *args, **kwargs) ⇒ Object

Creates methods in the Drivers module that matches the DRIVER_REGEX These methods will register a Driver for use by Capybara in a test pack

Examples:

Basic driver

DVLA::Browser::Drivers.chrome

Driver with additional arguments

DVLA::Browser::Drivers.chrome(remote: 'http://localhost:4444/wd/hub')


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
73
74
75
76
77
78
# File 'lib/dvla/browser/drivers/meta_drivers.rb', line 19

def self.method_missing(method, *args, **kwargs, &)
  if (matches = method.match(DRIVER_REGEX))
    headless = matches[:headless].is_a? String
    driver = matches[:driver].to_sym

    case driver
    when *SELENIUM_DRIVERS
      browser = matches[:browser].to_sym

      kwargs.each do |key, _value|
        LOG.warn { "Key: '#{key}' will be ignored | Use one from: '#{SELENIUM_ACCEPTED_PARAMS}'" } unless SELENIUM_ACCEPTED_PARAMS.include?(key)
      end

      ::Capybara.register_driver method do |app|
        options = Object.const_get("Selenium::WebDriver::#{browser.to_s.capitalize}::Options").new
        options.add_argument('--disable-dev-shm-usage')

        if headless
          options.add_argument('--headless')
          options.add_argument('--no-sandbox')
        end

        browser = :remote if kwargs[:remote]

        kwargs[:additional_arguments] && kwargs[:additional_arguments].each do |argument|
          argument.prepend('--') unless argument.start_with?('--')
          options.add_argument(argument)
        end

        kwargs[:additional_preferences] && kwargs[:additional_preferences].each do |preference|
          key, value = preference.first
          options.add_preference(key, value)
        end

        ::Capybara::Selenium::Driver.new(app, url: kwargs[:remote], browser:, options:)
      end
    else
      kwargs.each do |key, _value|
        LOG.warn { "Key: '#{key}' will be ignored | Use one from: '#{OTHER_ACCEPTED_PARAMS}'" } unless OTHER_ACCEPTED_PARAMS.include?(key)
      end

      ::Capybara.register_driver method do |app|
        Object.const_get("Capybara::#{driver.to_s.capitalize}::Driver").new(
          app,
          headless:,
          timeout: kwargs[:timeout] || 30,
          browser_options: { 'no-sandbox': nil, 'disable-smooth-scrolling': true },
        )
      end
    end

    LOG.info { "Driver set to: '#{method}'" }

    ::Capybara.javascript_driver = method
    ::Capybara.default_driver = method
    ::Capybara.current_driver = method
  else
    super.method_missing(method, *args, &)
  end
end

.respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/dvla/browser/drivers/meta_drivers.rb', line 80

def self.respond_to_missing?(method, *args)
  method.match(DRIVER_REGEX) || super
end