Class: Capybara::Cuprite::Browser::Client
- Inherits:
-
Object
- Object
- Capybara::Cuprite::Browser::Client
- Defined in:
- lib/capybara/cuprite/browser/client.rb
Defined Under Namespace
Classes: IdError
Instance Method Summary collapse
- #close ⇒ Object
- #command(method, params = {}) ⇒ Object
-
#initialize(browser, ws_url) ⇒ Client
constructor
A new instance of Client.
- #subscribe(event, &block) ⇒ Object
- #wait(id:) ⇒ Object
Constructor Details
#initialize(browser, ws_url) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/capybara/cuprite/browser/client.rb', line 11 def initialize(browser, ws_url) @command_id = 0 @subscribed = Hash.new { |h, k| h[k] = [] } @browser = browser @commands = Queue.new @ws = WebSocket.new(ws_url, @browser.logger) @thread = Thread.new do while = @ws..pop method, params = .values_at("method", "params") if method @subscribed[method].each { |b| b.call(params) } else @commands.push() end end @commands.close end end |
Instance Method Details
#close ⇒ Object
54 55 56 57 |
# File 'lib/capybara/cuprite/browser/client.rb', line 54 def close @ws.close @thread.kill end |
#command(method, params = {}) ⇒ Object
32 33 34 35 36 |
# File 'lib/capybara/cuprite/browser/client.rb', line 32 def command(method, params = {}) = (method, params) @ws.() [:id] end |
#subscribe(event, &block) ⇒ Object
49 50 51 52 |
# File 'lib/capybara/cuprite/browser/client.rb', line 49 def subscribe(event, &block) @subscribed[event] << block true end |
#wait(id:) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/capybara/cuprite/browser/client.rb', line 38 def wait(id:) = Timeout.timeout(@browser.timeout, TimeoutError) { @commands.pop } raise DeadBrowser unless raise IdError if ["id"] != id error, response = .values_at("error", "result") raise BrowserError.new(error) if error response rescue IdError retry end |