Class: Capybara::Poltergeist::Client
- Inherits:
-
Object
- Object
- Capybara::Poltergeist::Client
- Defined in:
- lib/capybara/poltergeist/client.rb
Constant Summary collapse
- PHANTOMJS_SCRIPT =
File.('../client/compiled/main.js', __FILE__)
- PHANTOMJS_VERSION =
'1.8.1'
- PHANTOMJS_NAME =
'phantomjs'
- KILL_TIMEOUT =
seconds
2
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#phantomjs_options ⇒ Object
readonly
Returns the value of attribute phantomjs_options.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#window_size ⇒ Object
readonly
Returns the value of attribute window_size.
Class Method Summary collapse
Instance Method Summary collapse
- #command ⇒ Object
-
#initialize(server, options = {}) ⇒ Client
constructor
A new instance of Client.
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(server, options = {}) ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/capybara/poltergeist/client.rb', line 19 def initialize(server, = {}) @server = server @path = [:path] || PHANTOMJS_NAME @window_size = [:window_size] || [1024, 768] @phantomjs_options = [:phantomjs_options] || [] @phantomjs_logger = [:phantomjs_logger] || $stdout pid = Process.pid at_exit { stop if Process.pid == pid } end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/capybara/poltergeist/client.rb', line 17 def path @path end |
#phantomjs_options ⇒ Object (readonly)
Returns the value of attribute phantomjs_options.
17 18 19 |
# File 'lib/capybara/poltergeist/client.rb', line 17 def @phantomjs_options end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
17 18 19 |
# File 'lib/capybara/poltergeist/client.rb', line 17 def pid @pid end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
17 18 19 |
# File 'lib/capybara/poltergeist/client.rb', line 17 def server @server end |
#window_size ⇒ Object (readonly)
Returns the value of attribute window_size.
17 18 19 |
# File 'lib/capybara/poltergeist/client.rb', line 17 def window_size @window_size end |
Class Method Details
.start(*args) ⇒ Object
11 12 13 14 15 |
# File 'lib/capybara/poltergeist/client.rb', line 11 def self.start(*args) client = new(*args) client.start client end |
Instance Method Details
#command ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/capybara/poltergeist/client.rb', line 69 def command parts = [path] parts.concat parts << PHANTOMJS_SCRIPT parts << server.port parts.concat window_size parts end |
#restart ⇒ Object
64 65 66 67 |
# File 'lib/capybara/poltergeist/client.rb', line 64 def restart stop start end |
#start ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/capybara/poltergeist/client.rb', line 30 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)) end end |
#stop ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/capybara/poltergeist/client.rb', line 44 def stop if pid begin Process.kill('TERM', pid) begin Timeout.timeout(KILL_TIMEOUT) { Process.wait(pid) } rescue Timeout::Error Process.kill('KILL', pid) Process.wait(pid) end rescue Errno::ESRCH, Errno::ECHILD # Zed's dead, baby end @out_thread.kill @pid = nil end end |