Module: Capybara::ChromeDevTools::DriverExtensions

Defined in:
lib/capybara/chrome_dev_tools/driver_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chrome_debugging_portObject

Returns the value of attribute chrome_debugging_port.



5
6
7
# File 'lib/capybara/chrome_dev_tools/driver_extensions.rb', line 5

def chrome_debugging_port
  @chrome_debugging_port
end

#crmux_listen_portObject

Returns the value of attribute crmux_listen_port.



6
7
8
# File 'lib/capybara/chrome_dev_tools/driver_extensions.rb', line 6

def crmux_listen_port
  @crmux_listen_port
end

Instance Method Details

#browserObject



63
64
65
66
67
# File 'lib/capybara/chrome_dev_tools/driver_extensions.rb', line 63

def browser
  super.tap do |browser|
    dev_tools if Capybara::ChromeDevTools.enabled
  end
end

#dev_toolsObject



69
70
71
72
73
74
# File 'lib/capybara/chrome_dev_tools/driver_extensions.rb', line 69

def dev_tools
  @dev_tools ||= (
    puts "Connecting to #{crmux_listen_port}..." if Capybara::ChromeDevTools.verbose >= 2
    ChromeRemote.client host: 'localhost', port: crmux_listen_port
  )
end

#find_free_port(port = 0) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/capybara/chrome_dev_tools/driver_extensions.rb', line 18

def find_free_port(port = 0)
  begin
    server = TCPServer.new('127.0.0.1', port)
  rescue Errno::EADDRINUSE
    if port == 0
      raise
    else
      # Lets you pass a preferred port to try first before falling back to 0
      port = 0
      retry
    end
  end
  server.addr[1].tap do |port|
    server.close
  end
end

#initialize(app, opts) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/capybara/chrome_dev_tools/driver_extensions.rb', line 8

def initialize(app, opts)
  #puts "#{self.class}#initialize"

  if opts[:browser] == :chrome and Capybara::ChromeDevTools.enabled
    start_crmux!(opts)
  end

  super
end

#start_crmux!(opts) ⇒ Object

Starts crmux and adds debuggerAddress to opts that points to crmux listen address.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/capybara/chrome_dev_tools/driver_extensions.rb', line 36

def start_crmux!(opts)
  self.chrome_debugging_port = find_free_port(Capybara::ChromeDevTools.preferred_port)
  self.crmux_listen_port     = find_free_port(chrome_debugging_port + 1)
  opts[:options].args << "--remote-debugging-port=#{chrome_debugging_port}"
  #opts[:options].add_preference 'debuggerAddress', "127.0.0.1:#{crmux_listen_port}"

  @debug_crmux = true
  command = "npx crmux #{'-d' if @debug_crmux} \
               --port=#{chrome_debugging_port} \
               --listen=#{crmux_listen_port}"
  puts %(command: #{command}) if Capybara::ChromeDevTools.verbose >= 3
  if @debug_crmux
    spawn_opts = {[:out, :err] => 'log/crmux.log'}
  else
    spawn_opts = {}
  end
  @crmux_pid = spawn(command, spawn_opts)
  puts %(Started crmux [pid #{@crmux_pid}], listening at http://localhost:#{crmux_listen_port}, connected to localhost:#{chrome_debugging_port})
  # You can also get the part later with: page.driver.browser.capabilities["goog:chromeOptions"]["debuggerAddress"]
  sleep 0.1

  at_exit do
    puts "Killing crmux process #{@crmux_pid}..." if Capybara::ChromeDevTools.verbose >= 1
    Process.kill 'TERM', @crmux_pid
  end
end