Class: Selenium::WebDriver::WebSocketConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/common/websocket_connection.rb

Constant Summary collapse

CONNECTION_ERRORS =
[
  Errno::ECONNRESET, # connection is aborted (browser process was killed)
  Errno::EPIPE # broken pipe (browser process was killed)
].freeze
RESPONSE_WAIT_TIMEOUT =
30
RESPONSE_WAIT_INTERVAL =
0.1
MAX_LOG_MESSAGE_SIZE =
9999

Instance Method Summary collapse

Constructor Details

#initialize(url:) ⇒ WebSocketConnection

Returns a new instance of WebSocketConnection.



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

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

  @session_id = nil
  @url = url

  process_handshake
  @socket_thread = attach_socket_listener
end

Instance Method Details

#add_callback(event, &block) ⇒ Object



55
56
57
58
# File 'lib/selenium/webdriver/common/websocket_connection.rb', line 55

def add_callback(event, &block)
  callbacks[event] << block
  block.object_id
end

#callbacksObject



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

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

#closeObject



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

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

#remove_callback(event, id) ⇒ Object



60
61
62
63
64
65
# File 'lib/selenium/webdriver/common/websocket_connection.rb', line 60

def remove_callback(event, id)
  return if callbacks[event].reject! { |callback| callback.object_id == id }

  ids = callbacks[event]&.map(&:object_id)
  raise Error::WebDriverError, "Callback with ID #{id} does not exist for event #{event}: #{ids}"
end

#send_cmd(**payload) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/selenium/webdriver/common/websocket_connection.rb', line 67

def send_cmd(**payload)
  id = next_id
  data = payload.merge(id: id)
  WebDriver.logger.debug "WebSocket -> #{data}"[...MAX_LOG_MESSAGE_SIZE], id: :bidi
  data = JSON.generate(data)
  out_frame = WebSocket::Frame::Outgoing::Client.new(version: ws.version, data: data, type: 'text')
  socket.write(out_frame.to_s)

  wait.until { messages.delete(id) }
end