class Selenium::WebDriver::DevTools

def callbacks

def callbacks
  @ws.callbacks
end

def close

def close
  @ws.close
end

def error_message(error)

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

def initialize(url:)

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

def method_missing(method, *_args)

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

def respond_to_missing?(method, *_args)

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

def send_cmd(method, **params)

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

def start_session

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