Class: Minibidi::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/minibidi/browser.rb

Defined Under Namespace

Classes: Error, ErrorData

Instance Method Summary collapse

Constructor Details

#initialize(async_websocket_connection) ⇒ Browser

Returns a new instance of Browser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/minibidi/browser.rb', line 7

def initialize(async_websocket_connection)
  @websocket = async_websocket_connection
  @debug_protocol = %w[1 true].include?(ENV['DEBUG'])

  Async do
    while message = async_websocket_connection.read
      handle_received_message_from_websocket(message.to_h)
    end
  end

  bidi_call_async('session.new', {
    capabilities: {
      alwaysMatch: {
        acceptInsecureCerts: false,
        webSocketUrl:true,
      },
    },
  }).wait
end

Instance Method Details

#bidi_call_async(method_, params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/minibidi/browser.rb', line 45

def bidi_call_async(method_, params = {})
  with_message_id do |message_id|
    Async do
      @message_results[message_id] = Async::Variable.new

      send_message_to_websocket({
        id: message_id,
        method: method_,
        params: params,
      })

      value = @message_results[message_id].value
      if value.is_a?(ErrorData)
        raise value.to_error
      else
        value
      end
    end
  end
end

#closeObject



41
42
43
# File 'lib/minibidi/browser.rb', line 41

def close
  bidi_call_async('browser.close').wait
end

#create_browsing_context(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/minibidi/browser.rb', line 27

def create_browsing_context(&block)
  res = bidi_call_async('browsingContext.create', { type: :tab, userContext: :default }).wait
  browsing_context = BrowsingContext.new(self, res[:context])
  if block
    begin
      block.call(browsing_context)
    ensure
      browsing_context.close
    end
  else
    browsing_context
  end
end