Class: Capybara::Lightpanda::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/lightpanda/client.rb,
lib/capybara/lightpanda/client/subscriber.rb,
lib/capybara/lightpanda/client/web_socket.rb

Defined Under Namespace

Classes: Subscriber, WebSocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws_url, options) ⇒ Client

Returns a new instance of Client.



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

def initialize(ws_url, options)
  @ws_url = ws_url
  @options = options
  @ws = WebSocket.new(ws_url, options)
  @command_id = 0
  @pendings = Concurrent::Hash.new
  @subscriber = Subscriber.new
  @mutex = Mutex.new

  start_message_thread
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/capybara/lightpanda/client.rb', line 12

def options
  @options
end

#ws_urlObject (readonly)

Returns the value of attribute ws_url.



12
13
14
# File 'lib/capybara/lightpanda/client.rb', line 12

def ws_url
  @ws_url
end

Instance Method Details

#closeObject



63
64
65
66
67
68
# File 'lib/capybara/lightpanda/client.rb', line 63

def close
  @ws&.close
  @message_thread&.join(1) || @message_thread&.kill
  @subscriber.clear
  @pendings.clear
end

#closed?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/capybara/lightpanda/client.rb', line 70

def closed?
  @ws.closed?
end

#command(method, params = {}, async: false, session_id: nil, timeout: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capybara/lightpanda/client.rb', line 26

def command(method, params = {}, async: false, session_id: nil, timeout: nil)
  message = build_message(method, params, session_id: session_id)

  if async
    @ws.send_message(JSON.generate(message))
    return true
  end

  pending = Concurrent::IVar.new
  @pendings[message[:id]] = pending

  @ws.send_message(JSON.generate(message))

  effective_timeout = timeout || @options.timeout
  response = pending.value!(effective_timeout)

  if response.nil?
    raise DeadBrowserError, "Browser closed during #{method}" if @ws.closed?

    raise TimeoutError, "Command #{method} timed out after #{effective_timeout}s"
  end

  handle_error(response) if response["error"]

  response["result"]
ensure
  @pendings.delete(message[:id]) if message
end

#off(event, block = nil) ⇒ Object



59
60
61
# File 'lib/capybara/lightpanda/client.rb', line 59

def off(event, block = nil)
  @subscriber.unsubscribe(event, block)
end

#on(event) ⇒ Object



55
56
57
# File 'lib/capybara/lightpanda/client.rb', line 55

def on(event, &)
  @subscriber.subscribe(event, &)
end