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.7.0'
PHANTOMJS_NAME =
'phantomjs'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, options = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(port, options = {})
  @port              = port
  @path              = options[:path]              || PHANTOMJS_NAME
  @window_size       = options[:window_size]       || [1024, 768]
  @phantomjs_options = options[:phantomjs_options] || []

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#phantomjs_optionsObject (readonly)

Returns the value of attribute phantomjs_options.



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

def phantomjs_options
  @phantomjs_options
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

#window_sizeObject (readonly)

Returns the value of attribute window_size.



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

def window_size
  @window_size
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



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

def command
  @command ||= begin
    parts = [path]
    parts.concat phantomjs_options
    parts << PHANTOMJS_SCRIPT
    parts << port
    parts.concat window_size
    parts
  end
end

#restartObject



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

def restart
  stop
  start
end

#startObject



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

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

#stopObject



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

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