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'
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.



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

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

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/capybara/poltergeist/client.rb', line 18

def path
  @path
end

#phantomjs_optionsObject (readonly)

Returns the value of attribute phantomjs_options.



18
19
20
# File 'lib/capybara/poltergeist/client.rb', line 18

def phantomjs_options
  @phantomjs_options
end

#pidObject (readonly)

Returns the value of attribute pid.



18
19
20
# File 'lib/capybara/poltergeist/client.rb', line 18

def pid
  @pid
end

#serverObject (readonly)

Returns the value of attribute server.



18
19
20
# File 'lib/capybara/poltergeist/client.rb', line 18

def server
  @server
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



18
19
20
# File 'lib/capybara/poltergeist/client.rb', line 18

def window_size
  @window_size
end

Class Method Details

.start(*args) ⇒ Object



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

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

Instance Method Details

#commandObject



73
74
75
76
77
78
79
80
# File 'lib/capybara/poltergeist/client.rb', line 73

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

#restartObject



68
69
70
71
# File 'lib/capybara/poltergeist/client.rb', line 68

def restart
  stop
  start
end

#startObject



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

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

  redirect_stdout(write) do
    @pid = Process.spawn(*command.map(&:to_s), pgroup: true)
  end
end

#stopObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/capybara/poltergeist/client.rb', line 45

def stop
  if pid
    begin
      if Capybara::Poltergeist.windows?
        Process.kill('KILL', pid)
      else
        Process.kill('TERM', pid)
        begin
          Timeout.timeout(KILL_TIMEOUT) { Process.wait(pid) }
        rescue Timeout::Error
          Process.kill('KILL', pid)
          Process.wait(pid)
        end
      end
    rescue Errno::ESRCH, Errno::ECHILD
      # Zed's dead, baby
    end

    @out_thread.kill
    @pid = nil
  end
end