Class: Capybara::Poltergeist::Client

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

Constant Summary collapse

PHANTOMJS_SCRIPT =
File.expand_path('../client/compiled/main.js', __FILE__)
PHANTOMJS_VERSION =
'1.4.1'
PHANTOMJS_NAME =
'phantomjs'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, inspector = nil, path = nil) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
# File 'lib/capybara/poltergeist/client.rb', line 15

def initialize(port, inspector = nil, path = nil)
  @port      = port
  @inspector = inspector
  @path      = path || PHANTOMJS_NAME

  pid = Process.pid
  at_exit { stop if Process.pid == pid }
end

Instance Attribute Details

#inspectorObject (readonly)

Returns the value of attribute inspector.



13
14
15
# File 'lib/capybara/poltergeist/client.rb', line 13

def inspector
  @inspector
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/capybara/poltergeist/client.rb', line 13

def path
  @path
end

#pidObject (readonly)

Returns the value of attribute pid.



13
14
15
# File 'lib/capybara/poltergeist/client.rb', line 13

def pid
  @pid
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/capybara/poltergeist/client.rb', line 13

def port
  @port
end

Class Method Details

.start(*args) ⇒ Object



7
8
9
10
11
# File 'lib/capybara/poltergeist/client.rb', line 7

def self.start(*args)
  client = new(*args)
  client.start
  client
end

Instance Method Details

#commandObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/capybara/poltergeist/client.rb', line 47

def command
  @command ||= begin
    parts = [path]

    if inspector
      parts << "--remote-debugger-port=#{inspector.port}"
      parts << "--remote-debugger-autorun=yes"
    end

    parts << PHANTOMJS_SCRIPT
    parts << port
    parts
  end
end

#restartObject



42
43
44
45
# File 'lib/capybara/poltergeist/client.rb', line 42

def restart
  stop
  start
end

#startObject



24
25
26
27
# File 'lib/capybara/poltergeist/client.rb', line 24

def start
  check_phantomjs_version
  @pid = Spawn.spawn(*command)
end

#stopObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capybara/poltergeist/client.rb', line 29

def stop
  if pid
    begin
      Process.kill('TERM', pid)
      Process.wait(pid)
    rescue Errno::ESRCH, Errno::ECHILD
      # Zed's dead, baby
    end

    @pid = nil
  end
end