class Selenium::Phantomjs::Instance
Constants
- PHANTOMJS_BIN
Attributes
pid_file[R]
Public Class Methods
new(pid_file)
click to toggle source
# File lib/selenium/phantomjs/instance.rb, line 8 def initialize(pid_file) raise "Please install phantomjs first and make sure that phantomjs is in your PATH" if PHANTOMJS_BIN == '' pid_dir = File.dirname(pid_file) FileUtils.mkpath(pid_dir) unless File.directory? pid_dir @pid_file = pid_file end
Public Instance Methods
alive?()
click to toggle source
# File lib/selenium/phantomjs/instance.rb, line 45 def alive? !! pid && begin !! Process.kill(0, pid) rescue Errno::ESRCH, RangeError false end end
die!()
click to toggle source
# File lib/selenium/phantomjs/instance.rb, line 53 def die! Process.kill('TERM', pid) if alive? File.delete(pid_file) if File.exists? pid_file @pid = nil end
pid()
click to toggle source
# File lib/selenium/phantomjs/instance.rb, line 59 def pid @pid ||= if File.exists? pid_file File.open(pid_file, 'r') do |f| begin f.readline.to_s.strip.to_i rescue EOFError nil end end end end
run(args, options = {})
click to toggle source
# File lib/selenium/phantomjs/instance.rb, line 17 def run(args, options = {}) die! arguments = if args.is_a? Hash args.map do |k, v| if v %Q{--#{k}=#{v}} else "--#{k}" end end else Array(args) end raise ArgumentError.new("PhantomJS: interactive shell is not supported.") if arguments.empty? command = [PHANTOMJS_BIN] + arguments @pid = Process.spawn(*command, options) Process.detach(@pid) sleep 1 raise "PhantomJS: failed to start" unless alive? File.open(pid_file, 'w') { |f| f.write @pid } @pid end