123456789_123456789_123456789_123456789_123456789_

Module: Selenium::WebDriver::DriverExtensions::HasLogEvents Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Defined in: rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb

Constant Summary

Instance Method Summary

::Selenium::WebDriver::Atoms - Included

Instance Method Details

#log_console_events (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb', line 76

def log_console_events
  devtools.runtime.on(:console_api_called) do |params|
    event = DevTools::ConsoleEvent.new(
      type: params['type'],
      timestamp: params['timestamp'],
      args: params['args']
    )

    log_listeners[:console].each do |listener|
      listener.call(event)
    end
  end
end

#log_exception_events (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb', line 90

def log_exception_events
  devtools.runtime.on(:exception_thrown) do |params|
    description = if params.dig('exceptionDetails', 'exception')
                    params.dig('exceptionDetails', 'exception', 'description')
                  else
                    params.dig('exceptionDetails', 'text')
                  end

    event = DevTools::ExceptionEvent.new(
      description: description,
      timestamp: params['timestamp'],
      stacktrace: params.dig('exceptionDetails', 'stackTrace', 'callFrames')
    )

    log_listeners[:exception].each do |listener|
      listener.call(event)
    end
  end
end

#log_listeners (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb', line 72

def log_listeners
  @log_listeners ||= Hash.new { |listeners, kind| listeners[kind] = [] }
end

#log_mutation_event(params) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb', line 120

def log_mutation_event(params)
  payload = JSON.parse(params['payload'])
  elements = find_elements(css: "*[data-__webdriver_id='#{payload['target']}']")
  return if elements.empty?

  event = DevTools::MutationEvent.new(
    element: elements.first,
    attribute_name: payload['name'],
    current_value: payload['value'],
    old_value: payload['oldValue']
  )

  log_listeners[:mutation].each do |log_listener|
    log_listener.call(event)
  end
end

#log_mutation_events (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb', line 110

def log_mutation_events
  devtools.page.enable

  devtools.runtime.add_binding(name: '__webdriver_attribute')
  execute_script(mutation_listener)
  devtools.page.add_script_to_evaluate_on_new_document(source: mutation_listener)

  devtools.runtime.on(:binding_called) { |event| log_mutation_event(event) }
end

#mutation_listener (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb', line 137

def mutation_listener
  @mutation_listener ||= read_atom(:mutationListener)
end

#on_log_event(kind, &block) {|| ... }

Registers listener to be called whenever browser receives a new Console API message such as console.log() or an unhandled exception.

This currently relies on ::Selenium::WebDriver::DevTools so is only supported in ::Selenium::WebDriver::Chromium browsers.

Examples:

Collect console messages

logs = []
driver.on_log_event(:console) do |event|
  logs.push(event)
end

Collect JavaScript exceptions

exceptions = []
driver.on_log_event(:exception) do |event|
  exceptions.push(event)
end

Collect DOM mutations

mutations = []
driver.on_log_event(:mutation) do |event|
  mutations.push(event)
end

Parameters:

  • kind (Symbol)

    :console, :exception or :mutation

  • block (#call)

    which is called when event happens

Yield Parameters:

Raises:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver_extensions/has_log_events.rb', line 59

def on_log_event(kind, &block)
  raise Error::WebDriverError, "Don't know how to handle #{kind} events" unless KINDS.include?(kind)

  enabled = log_listeners[kind].any?
  log_listeners[kind] << block
  return if enabled

  devtools.runtime.enable
  __send__(:"log_#{kind}_events")
end