Class: Capybara::Lightpanda::Client
- Inherits:
-
Object
- Object
- Capybara::Lightpanda::Client
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#ws_url ⇒ Object
readonly
Returns the value of attribute ws_url.
Instance Method Summary collapse
-
#clear_subscriptions ⇒ Object
Drop all event subscriptions without closing the WebSocket.
- #close ⇒ Object
- #closed? ⇒ Boolean
- #command(method, params = {}, async: false, session_id: nil, timeout: nil) ⇒ Object
-
#initialize(ws_url, options) ⇒ Client
constructor
A new instance of Client.
- #off(event, block = nil) ⇒ Object
- #on(event) ⇒ Object
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, ) @ws_url = ws_url @options = @ws = WebSocket.new(ws_url, ) @command_id = 0 @pendings = Concurrent::Hash.new @subscriber = Subscriber.new @mutex = Mutex.new end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/capybara/lightpanda/client.rb', line 12 def @options end |
#ws_url ⇒ Object (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
#clear_subscriptions ⇒ Object
Drop all event subscriptions without closing the WebSocket. Used by Browser#recreate_page so a fresh target’s event handlers don’t pile up on top of the previous target’s subscriptions.
66 67 68 |
# File 'lib/capybara/lightpanda/client.rb', line 66 def clear_subscriptions @subscriber.clear end |
#close ⇒ Object
70 71 72 73 74 75 |
# File 'lib/capybara/lightpanda/client.rb', line 70 def close @ws&.close @message_thread&.join(1) || @message_thread&.kill @subscriber.clear @pendings.clear end |
#closed? ⇒ Boolean
77 78 79 |
# File 'lib/capybara/lightpanda/client.rb', line 77 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) = (method, params, session_id: session_id) if async @ws.(JSON.generate()) return true end pending = Concurrent::IVar.new @pendings[[:id]] = pending @ws.(JSON.generate()) 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([:id]) if 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 |