123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::DevTools

Class Method Summary

Instance Method Summary

Constructor Details

.new(url:) ⇒ DevTools

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 31

def initialize(url:)
  @ws = WebSocketConnection.new(url: url)
  @session_id = nil
  start_session
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_args)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 54

def method_missing(method, *_args)
  namespace = "Selenium::DevTools::V#{Selenium::DevTools.version}"
  methods_to_classes = "#{namespace}::METHODS_TO_CLASSES"

  desired_class = if Object.const_defined?(methods_to_classes)
                    # selenium-devtools 0.113 and newer
                    "#{namespace}::#{Object.const_get(methods_to_classes)[method]}"
                  else
                    # selenium-devtools 0.112 and older
                    "#{namespace}::#{method.capitalize}"
                  end

  return unless Object.const_defined?(desired_class)

  self.class.class_eval do
    define_method(method) do
      Object.const_get(desired_class).new(self)
    end
  end

  send(method)
end

Instance Method Details

#callbacks

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 41

def callbacks
  @ws.callbacks
end

#close

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 37

def close
  @ws.close
end

#error_message(error) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 91

def error_message(error)
  [error['code'], error['message'], error['data']].join(': ')
end

#respond_to_missing?(method, *_args) ⇒ Boolean

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 77

def respond_to_missing?(method, *_args)
  desired_class = "Selenium::DevTools::V#{Selenium::DevTools.version}::#{method.capitalize}"
  Object.const_defined?(desired_class)
end

#send_cmd(method, **params)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 45

def send_cmd(method, **params)
  data = {method: method, params: params.compact}
  data[:sessionId] = @session_id if @session_id
  message = @ws.send_cmd(**data)
  raise Error::WebDriverError, error_message(message['error']) if message['error']

  message
end

#start_session (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/devtools.rb', line 84

def start_session
  targets = target.get_targets.dig('result', 'targetInfos')
  page_target = targets.find { |target| target['type'] == 'page' }
  session = target.attach_to_target(target_id: page_target['targetId'], flatten: true)
  @session_id = session.dig('result', 'sessionId')
end