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.

  • debug (Boolean)


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

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

Instance Method Details

#on_message_received(&block) ⇒ Object



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

def on_message_received(&block)
  @on_message = block
end

#runObject

Note:

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

Start ‘playwright-cli run-driver`



41
42
43
44
45
46
47
48
# File 'lib/playwright/transport.rb', line 41

def run
  @stdin, @stdout, @stderr, @thread = Open3.popen3(@driver_executable_path, 'run-driver')

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

  @thread.join
end

#send_message(message) ⇒ Object

Parameters:

  • message (Hash)


24
25
26
27
28
29
30
31
# File 'lib/playwright/transport.rb', line 24

def send_message(message)
  debug_send_message(message) if @debug
  msg = JSON.dump(message)
  @stdin.write([msg.size].pack('V')) # unsigned 32bit, little endian
  @stdin.write(msg)
rescue Errno::EPIPE
  raise AlreadyDisconnectedError.new('send_message failed')
end

#stopObject

Terminate playwright-cli driver.



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

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