Class: Terminus::Client::PhantomJS

Inherits:
Base
  • Object
show all
Defined in:
lib/terminus/client/phantomjs.rb

Constant Summary collapse

BROWSERS =
%w[chromium chromium-browser google-chrome xdg-open open]
DEBUG_PATH =
'/webkit/inspector/inspector.html?page=2'
DEFAULT_COMMAND =
['/usr/bin/env', 'phantomjs']
DEFAULT_WIDTH =
1024
DEFAULT_HEIGHT =
768
PHANTOM_CLIENT =
File.expand_path('../phantom.js', __FILE__)

Instance Attribute Summary

Attributes inherited from Base

#id

Instance Method Summary collapse

Methods inherited from Base

#debug, debugger, save_screenshot, start, #start, #stop

Constructor Details

#initialize(*args) ⇒ PhantomJS

Returns a new instance of PhantomJS.



12
13
14
15
16
# File 'lib/terminus/client/phantomjs.rb', line 12

def initialize(*args)
  @debug_port = TCPServer.new(0).addr[1]
  @debugger   = BROWSERS.map(&method(:detect_browser)).compact.first
  super
end

Instance Method Details

#browser_argsObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/terminus/client/phantomjs.rb', line 18

def browser_args
  command = @options[:command] || DEFAULT_COMMAND
  width   = @options[:width]   || DEFAULT_WIDTH
  height  = @options[:height]  || DEFAULT_HEIGHT

  args = command.dup
  args + ["--remote-debugger-port=#{@debug_port}", "--remote-debugger-autorun=yes",
          PHANTOM_CLIENT, width.to_s, height.to_s,
          @address[2], @port.to_s, @connector.port.to_s]
end

#browser_selectorObject



29
30
31
# File 'lib/terminus/client/phantomjs.rb', line 29

def browser_selector
  {:name => 'PhantomJS'}
end

#debuggerObject

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
# File 'lib/terminus/client/phantomjs.rb', line 33

def debugger
  raise ArgumentError, 'Could not find launchable browser' unless @debugger
  url = "http://#{@address[2]}:#{@debug_port}#{DEBUG_PATH}"
  process = ChildProcess.build(@debugger, url)
  process.start
  puts "Launched WebKit remote debugger at #{url}"
end

#detect_browser(browser) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/terminus/client/phantomjs.rb', line 41

def detect_browser(browser)
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exe = File.join(path, browser)
    return exe if File.executable?(exe)
  end
  nil
end

#save_screenshot(path, options = {}) ⇒ Object



49
50
51
52
# File 'lib/terminus/client/phantomjs.rb', line 49

def save_screenshot(path, options = {})
  message = MultiJson.dump(['save_screenshot', path, options])
  @connector.request(message)
end