Class: Playwright::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/playwright/connection.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(transport)
  @transport = transport
  @transport.on_message_received do |message|
    dispatch(message)
  end
  @transport.on_driver_crashed do
    @callbacks.each_value do |callback|
      callback.reject(::Playwright::DriverCrashedError.new)
    end
    raise ::Playwright::DriverCrashedError.new
  end

  @objects = {} # Hash[ guid => ChannelOwner ]
  @waiting_for_object = {} # Hash[ guid => Promise<ChannelOwner> ]
  @callbacks = {} # Hash [ guid => Promise<ChannelOwner> ]
  @root_object = RootChannelOwner.new(self)
  @remote = false
  @tracing_count = 0
end

Instance Attribute Details

#local_utilsObject (readonly)

Returns the value of attribute local_utils.



28
29
30
# File 'lib/playwright/connection.rb', line 28

def local_utils
  @local_utils
end

Instance Method Details

#async_runObject



38
39
40
# File 'lib/playwright/connection.rb', line 38

def async_run
  @transport.async_run
end

#async_send_message_to_server(guid, method, params, metadata: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/playwright/connection.rb', line 62

def async_send_message_to_server(guid, method, params, metadata: nil)
  callback = Concurrent::Promises.resolvable_future

  with_generated_id do |id|
    # register callback promise object first.
    # @see https://github.com/YusukeIwaki/puppeteer-ruby/pull/34
    @callbacks[id] = callback

     = {}
    if 
      [:wallTime] = [:wallTime]
      [:apiName] = [:apiName]
      [:location] = [:stack].first
      [:internal] = ![:apiName]
    end
    .compact!

    message = {
      id: id,
      guid: guid,
      method: method,
      params: replace_channels_with_guids(params),
      metadata: ,
    }

    begin
      @transport.send_message(message)
    rescue => err
      @callbacks.delete(id)
      callback.reject(err)
      raise unless err.is_a?(Transport::AlreadyDisconnectedError)
    end

    if @tracing_count > 0 && ![:stack].empty? && guid != 'localUtils'
      @local_utils.add_stack_to_tracing_no_reply(id, [:stack])
    end
  end

  callback
end

#initialize_playwrightObject



46
47
48
49
50
51
52
# File 'lib/playwright/connection.rb', line 46

def initialize_playwright
  # Avoid Error: sdkLanguage: expected one of (javascript|python|java|csharp)
  # ref: https://github.com/microsoft/playwright/pull/18308
  # ref: https://github.com/YusukeIwaki/playwright-ruby-client/issues/228
  result = send_message_to_server('', 'initialize', { sdkLanguage: 'python' })
  ChannelOwners::Playwright.from(result['playwright'])
end

#mark_as_remoteObject



30
31
32
# File 'lib/playwright/connection.rb', line 30

def mark_as_remote
  @remote = true
end

#remote?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/playwright/connection.rb', line 34

def remote?
  @remote
end

#send_message_to_server(guid, method, params, metadata: nil) ⇒ Object



103
104
105
# File 'lib/playwright/connection.rb', line 103

def send_message_to_server(guid, method, params, metadata: nil)
  async_send_message_to_server(guid, method, params, metadata: ).value!
end

#set_in_tracing(value) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/playwright/connection.rb', line 54

def set_in_tracing(value)
  if value
    @tracing_count += 1
  else
    @tracing_count -= 1
  end
end

#stopObject



42
43
44
# File 'lib/playwright/connection.rb', line 42

def stop
  @transport.stop
end