Class: Charai::BrowserLauncher

Inherits:
Object
  • Object
show all
Defined in:
lib/charai/browser_launcher.rb

Instance Method Summary collapse

Constructor Details

#initializeBrowserLauncher

Returns a new instance of BrowserLauncher.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/charai/browser_launcher.rb', line 3

def initialize
  if ::Charai::Util.macos?
    if File.exist?("/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox")
      @firefox_executable_path = "/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox"
    end
  elsif ::Charai::Util.linux?
    if File.exist?("/usr/bin/firefox-devedition")
      @firefox_executable_path = "/usr/bin/firefox-devedition"
    end
  end

  raise 'Firefox Developer Edition is not found.' unless @firefox_executable_path
end

Instance Method Details

#launch(headless: false, debug_protocol: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/charai/browser_launcher.rb', line 17

def launch(headless: false, debug_protocol: false)
  tmpdir = Dir.mktmpdir('charai')
  (tmpdir)

  args = [
    "--remote-debugging-port=0",
    "--profile",
    tmpdir,
    "--no-remote",
  ]
  if ::Charai::Util.macos?
    args << "--foreground"
  end
  if headless
    args << "--headless"
  end

  proc = BrowserProcess.new(
    @firefox_executable_path,
    *args,
    "about:blank",
  )
  at_exit do
    proc.kill
    FileUtils.remove_entry(tmpdir)
  end
  trap(:INT) { proc.kill ; exit 130 }
  trap(:TERM) { proc.kill ; proc.dispose }
  trap(:HUP) { proc.kill ; proc.dispose }

  endpoint = wait_for_ws_endpoint(proc)

  web_socket = WebSocket.new(url: "#{endpoint}/session")
  Browser.new(web_socket, debug_protocol: debug_protocol)
end