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.8.1', '< 3.0']
PHANTOMJS_NAME =
'phantomjs'
KILL_TIMEOUT =

seconds

2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/capybara/poltergeist/client.rb', line 34

def initialize(server, options = {})
  @server            = server
  @path              = Cliver::detect!((options[:path] || PHANTOMJS_NAME),
                                       *PHANTOMJS_VERSION)

  @window_size       = options[:window_size]       || [1024, 768]
  @phantomjs_options = options[:phantomjs_options] || []
  @phantomjs_logger  = options[:phantomjs_logger]  || $stdout

  pid = Process.pid
  at_exit do
    # do the work in a separate thread, to avoid stomping on $!,
    # since other libraries depend on it directly.
    Thread.new do
      stop if Process.pid == pid
    end.join
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



32
33
34
# File 'lib/capybara/poltergeist/client.rb', line 32

def path
  @path
end

#phantomjs_optionsObject (readonly)

Returns the value of attribute phantomjs_options.



32
33
34
# File 'lib/capybara/poltergeist/client.rb', line 32

def phantomjs_options
  @phantomjs_options
end

#pidObject (readonly)

Returns the value of attribute pid.



32
33
34
# File 'lib/capybara/poltergeist/client.rb', line 32

def pid
  @pid
end

#serverObject (readonly)

Returns the value of attribute server.



32
33
34
# File 'lib/capybara/poltergeist/client.rb', line 32

def server
  @server
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



32
33
34
# File 'lib/capybara/poltergeist/client.rb', line 32

def window_size
  @window_size
end

Class Method Details

.process_killer(pid) ⇒ Object

Returns a proc, that when called will attempt to kill the given process. This is because implementing ObjectSpace.define_finalizer is tricky. Hat-Tip to @mperham for describing in detail: www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/



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

def self.process_killer(pid)
  proc do
    begin
      Process.kill('KILL', pid)
    rescue Errno::ESRCH, Errno::ECHILD
    end
  end
end

.start(*args) ⇒ Object



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

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

Instance Method Details

#commandObject



84
85
86
87
88
89
90
91
# File 'lib/capybara/poltergeist/client.rb', line 84

def command
  parts = [path]
  parts.concat phantomjs_options
  parts << PHANTOMJS_SCRIPT
  parts << server.port
  parts.concat window_size
  parts
end

#restartObject



79
80
81
82
# File 'lib/capybara/poltergeist/client.rb', line 79

def restart
  stop
  start
end

#startObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/capybara/poltergeist/client.rb', line 53

def start
  @read_io, @write_io = IO.pipe
  @out_thread = Thread.new {
    while !@read_io.eof? && data = @read_io.readpartial(1024)
      @phantomjs_logger.write(data)
    end
  }

  process_options = {}
  process_options[:pgroup] = true unless Capybara::Poltergeist.windows?

  redirect_stdout do
    @pid = Process.spawn(*command.map(&:to_s), process_options)
    ObjectSpace.define_finalizer(self, self.class.process_killer(@pid))
  end
end

#stopObject



70
71
72
73
74
75
76
77
# File 'lib/capybara/poltergeist/client.rb', line 70

def stop
  if pid
    kill_phantomjs
    @out_thread.kill
    close_io
    ObjectSpace.undefine_finalizer(self)
  end
end