Module: Capybara::Chrome::Service

Included in:
Browser
Defined in:
lib/capybara/chrome/service.rb

Constant Summary collapse

CHROME_ARGS =

“–disable-gpu”, ‘–js-flags=“–max-old-space-size=500”’,

[
  "--headless",
  "--enable-automation",
  "--crash-dumps-dir=/tmp",
  "--disable-background-networking",
  "--disable-background-timer-throttling",
  "--disable-breakpad",
  "--disable-client-side-phishing-detection",
  "--disable-default-apps",
  "--disable-dev-shm-usage",
  "--disable-extensions",
  "--disable-features=site-per-process",
  "--disable-hang-monitor",
  "--disable-popup-blocking",
  "--disable-prompt-on-repost",
  "--disable-sync",
  "--disable-translate",
  "--metrics-recording-only",
  "--no-first-run",
  "--no-pings",
  "--safebrowsing-disable-auto-update",
  "--hide-scrollbars",
  "--mute-audio",
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_available_port(host) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/capybara/chrome/service.rb', line 102

def find_available_port(host)
  sleep rand * 0.7 # slight delay to account for concurrent browsers
  server = TCPServer.new(host, 0)
  server.addr[1]
ensure
  server.close if server
end

Instance Method Details

#chrome_argsObject



78
79
80
81
82
# File 'lib/capybara/chrome/service.rb', line 78

def chrome_args
  custom_args = Capybara::Chrome.configuration.chrome_args
  defaults = custom_args.any? ? custom_args : CHROME_ARGS
  defaults + ["--remote-debugging-port=#{@chrome_port}"]
end

#chrome_pathObject



68
69
70
71
72
73
74
75
76
# File 'lib/capybara/chrome/service.rb', line 68

def chrome_path
  case os
  when :macosx
    "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
  when :linux
    # /opt/google/chrome/chrome
    "google-chrome-stable"
  end
end

#chrome_pidObject



31
32
33
# File 'lib/capybara/chrome/service.rb', line 31

def chrome_pid
  @chrome_pid
end

#chrome_running?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/capybara/chrome/service.rb', line 62

def chrome_running?
  socket = TCPSocket.new(@chrome_host, @chrome_port) rescue false
  socket.close if socket
  !!socket
end

#osObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/capybara/chrome/service.rb', line 84

def os
  @os ||= (
    host_os = RbConfig::CONFIG['host_os']
    case host_os
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      :windows
    when /darwin|mac os/
      :macosx
    when /linux/
      :linux
    when /solaris|bsd/
      :unix
    else
      raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
    end
  )
end

#restart_chromeObject



46
47
48
49
50
51
52
# File 'lib/capybara/chrome/service.rb', line 46

def restart_chrome
  stop_chrome
  if chrome_running?
    @chrome_port = find_available_port(@chrome_host)
  end
  start_chrome
end

#start_chromeObject



35
36
37
38
39
40
# File 'lib/capybara/chrome/service.rb', line 35

def start_chrome
  return if chrome_running?
  debug "Starting Chrome", chrome_path, chrome_args
  @chrome_pid = Process.spawn chrome_path, *chrome_args, :out=>"/dev/null"
  at_exit { stop_chrome }
end

#stop_chromeObject



42
43
44
# File 'lib/capybara/chrome/service.rb', line 42

def stop_chrome
  Process.kill "TERM", chrome_pid rescue nil
end

#wait_for_chromeObject



54
55
56
57
58
59
60
# File 'lib/capybara/chrome/service.rb', line 54

def wait_for_chrome
  running = false
  while !running
    running = chrome_running?
    sleep 0.02
  end
end