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

#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

#send_cmd(**payload) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/selenium/webdriver/common/websocket_connection.rb', line 55

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