Class: ChromeRemote::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chrome_remote/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws_url, logger = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
# File 'lib/chrome_remote/client.rb', line 8

def initialize(ws_url, logger = nil)
  @ws = WebSocketClient.new(ws_url)
  @handlers = Hash.new { |hash, key| hash[key] = [] }
  @logger = logger || Logger.new(nil)
  @last_id = 0
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



6
7
8
# File 'lib/chrome_remote/client.rb', line 6

def handlers
  @handlers
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/chrome_remote/client.rb', line 6

def logger
  @logger
end

#wsObject (readonly)

Returns the value of attribute ws.



6
7
8
# File 'lib/chrome_remote/client.rb', line 6

def ws
  @ws
end

Instance Method Details

#listenObject



34
35
36
# File 'lib/chrome_remote/client.rb', line 34

def listen
  read_until { false }
end

#listen_until(&block) ⇒ Object



30
31
32
# File 'lib/chrome_remote/client.rb', line 30

def listen_until(&block)
  read_until { block.call }
end

#on(event_name, &block) ⇒ Object



26
27
28
# File 'lib/chrome_remote/client.rb', line 26

def on(event_name, &block)
  handlers[event_name] << block
end

#send_cmd(command, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/chrome_remote/client.rb', line 15

def send_cmd(command, params = {})
  msg_id = generate_unique_id
  payload = {method: command, params: params, id: msg_id}.to_json

  logger.info "SEND ► #{payload}"
  ws.send_msg(payload)

  msg = read_until { |msg| msg["id"] == msg_id }
  msg["result"]
end

#wait_for(event_name = nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/chrome_remote/client.rb', line 38

def wait_for(event_name=nil)
  if event_name
    msg = read_until { |msg| msg["method"] == event_name }
  elsif block_given?
    msg = read_until { |msg| yield(msg["method"], msg["params"]) }
  end
  msg["params"]
end