Class: Playwright::Transport

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

Overview

Defined Under Namespace

Classes: AlreadyDisconnectedError

Instance Method Summary collapse

Constructor Details

#initialize(playwright_cli_executable_path:) ⇒ Transport

Returns a new instance of Transport.

Parameters:

  • playwright_cli_executable_path (String)

    path to playwright-cli.



11
12
13
14
15
# File 'lib/playwright/transport.rb', line 11

def initialize(playwright_cli_executable_path:)
  @driver_executable_path = playwright_cli_executable_path
  @debug = ENV['DEBUG'].to_s == 'true' || ENV['DEBUG'].to_s == '1'
  @mutex = Mutex.new
end

Instance Method Details

#async_runObject

Note:

This method blocks until playwright-cli exited. Consider using Thread or Future.

Start ‘playwright-cli run-driver`



48
49
50
51
52
53
54
# File 'lib/playwright/transport.rb', line 48

def async_run
  @stdin, @stdout, @stderr, @thread = run_driver_with_open3
  @stdin.binmode  # Ensure Strings are written 1:1 without encoding conversion, necessary for integer values

  Thread.new { handle_stdout }
  Thread.new { handle_stderr }
end

#on_driver_crashed(&block) ⇒ Object



21
22
23
# File 'lib/playwright/transport.rb', line 21

def on_driver_crashed(&block)
  @on_driver_crashed = block
end

#on_message_received(&block) ⇒ Object



17
18
19
# File 'lib/playwright/transport.rb', line 17

def on_message_received(&block)
  @on_message = block
end

#send_message(message) ⇒ Object

Parameters:

  • message (Hash)


28
29
30
31
32
33
34
35
36
37
# File 'lib/playwright/transport.rb', line 28

def send_message(message)
  debug_send_message(message) if @debug
  msg = JSON.dump(message)
  @mutex.synchronize {
    @stdin.write([msg.bytes.length].pack('V')) # unsigned 32bit, little endian, real byte size instead of chars
    @stdin.write(msg) # write UTF-8 in binary mode as byte stream
  }
rescue Errno::EPIPE, IOError
  raise AlreadyDisconnectedError.new('send_message failed')
end

#stopObject

Terminate playwright-cli driver.



40
41
42
43
# File 'lib/playwright/transport.rb', line 40

def stop
  [@stdin, @stdout, @stderr].each { |io| io.close unless io.closed? }
  @thread&.terminate
end