Class: ChromeDebugger::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chrome_debugger/client.rb

Constant Summary collapse

PAGE_LOAD_WAIT =
16
REMOTE_DEBUGGING_PORT =
9222

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



22
23
24
# File 'lib/chrome_debugger/client.rb', line 22

def initialize(opts = {})
  @chrome_path = find_chrome_binary
end

Class Method Details

.open(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/chrome_debugger/client.rb', line 26

def self.open(&block)
  headless = Headless.new
  headless.start
  chrome = ChromeDebugger::Client.new
  chrome.start_chrome
  yield chrome
ensure
  chrome.cleanup
  headless.destroy
end

Instance Method Details

#cleanupObject



55
56
57
58
59
60
61
62
# File 'lib/chrome_debugger/client.rb', line 55

def cleanup
  if @chrome_pid
    Process.kill('-TERM', Process.getpgid(@chrome_pid))
    sleep 3
    FileUtils.rm_rf(@profile_dir) if @profile_dir && File.directory?(@profile_dir)
    @chrome_pid = nil
  end
end

#load_url(url) ⇒ Object



48
49
50
51
52
53
# File 'lib/chrome_debugger/client.rb', line 48

def load_url(url)
  raise "call the start_chrome() method first" unless @chrome_pid
  document = ChromeDebugger::Document.new(url)
  load(document)
  document
end

#start_chromeObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/chrome_debugger/client.rb', line 37

def start_chrome
  @profile_dir = File.join(Dir.tmpdir, SecureRandom.hex(10))
  @chrome_cmd  = "'#{@chrome_path}' --user-data-dir=#{@profile_dir} -remote-debugging-port=#{REMOTE_DEBUGGING_PORT} --no-first-run > /dev/null 2>&1"
  puts @chrome_cmd
  @chrome_pid  = Process.spawn(@chrome_cmd, :pgroup => true)

  until debug_port_listening?
    sleep 0.1
  end
end