Class: Selenium::WebDriver::DevTools

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/devtools.rb,
lib/selenium/webdriver/devtools/request.rb,
lib/selenium/webdriver/devtools/response.rb,
lib/selenium/webdriver/devtools/console_event.rb,
lib/selenium/webdriver/devtools/pinned_script.rb,
lib/selenium/webdriver/devtools/mutation_event.rb,
lib/selenium/webdriver/devtools/exception_event.rb

Defined Under Namespace

Classes: ConsoleEvent, ExceptionEvent, MutationEvent, PinnedScript, Request, Response

Constant Summary collapse

RESPONSE_WAIT_TIMEOUT =
30
RESPONSE_WAIT_INTERVAL =
0.1

Instance Method Summary collapse

Constructor Details

#initialize(url:) ⇒ DevTools

Returns a new instance of DevTools.


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/selenium/webdriver/devtools.rb', line 33

def initialize(url:)
  @callback_threads = ThreadGroup.new

  @messages = []
  @session_id = nil
  @url = url

  process_handshake
  @socket_thread = attach_socket_listener
  start_session
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_args) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/selenium/webdriver/devtools.rb', line 74

def method_missing(method, *_args)
  desired_class = "Selenium::DevTools::V#{Selenium::DevTools.version}::#{method.capitalize}"
  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

#callbacksObject


51
52
53
# File 'lib/selenium/webdriver/devtools.rb', line 51

def callbacks
  @callbacks ||= Hash.new { |callbacks, event| callbacks[event] = [] }
end

#closeObject


45
46
47
48
49
# File 'lib/selenium/webdriver/devtools.rb', line 45

def close
  @callback_threads.list.each(&:exit)
  @socket_thread.exit
  socket.close
end

#respond_to_missing?(method, *_args) ⇒ Boolean

Returns:

  • (Boolean)

87
88
89
90
# File 'lib/selenium/webdriver/devtools.rb', line 87

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) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/selenium/webdriver/devtools.rb', line 55

def send_cmd(method, **params)
  id = next_id
  data = {id: id, method: method, params: params.reject { |_, v| v.nil? }}
  data[:sessionId] = @session_id if @session_id
  data = JSON.generate(data)
  WebDriver.logger.debug "DevTools -> #{data}"

  out_frame = WebSocket::Frame::Outgoing::Client.new(version: ws.version, data: data, type: 'text')
  socket.write(out_frame.to_s)

  message = wait.until do
    @messages.find { |m| m['id'] == id }
  end

  raise Error::WebDriverError, error_message(message['error']) if message['error']

  message
end