2012-02-29 00:02:43 +00:00
|
|
|
module Capybara::Poltergeist
|
|
|
|
class Inspector
|
2012-10-06 16:57:46 +01:00
|
|
|
BROWSERS = %w(chromium chromium-browser google-chrome open)
|
|
|
|
DEFAULT_PORT = 9664
|
2012-02-29 00:02:43 +00:00
|
|
|
|
|
|
|
def self.detect_browser
|
2012-09-15 18:22:08 +02:00
|
|
|
@browser ||= BROWSERS.find { |name| browser_binary_exists?(name) }
|
2012-02-29 00:02:43 +00:00
|
|
|
end
|
|
|
|
|
2012-10-06 16:57:46 +01:00
|
|
|
attr_reader :port
|
|
|
|
|
|
|
|
def initialize(browser = nil, port = DEFAULT_PORT)
|
2012-02-29 00:02:43 +00:00
|
|
|
@browser = browser.respond_to?(:to_str) ? browser : nil
|
2012-10-06 16:57:46 +01:00
|
|
|
@port = port
|
2012-02-29 00:02:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def browser
|
|
|
|
@browser ||= self.class.detect_browser
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
2014-05-10 06:50:03 +09:00
|
|
|
"//localhost:#{port}/"
|
2012-02-29 00:02:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def open
|
|
|
|
if browser
|
2013-01-26 15:37:47 +00:00
|
|
|
Process.spawn(browser, url)
|
2012-02-29 00:02:43 +00:00
|
|
|
else
|
|
|
|
raise Error, "Could not find a browser executable to open #{url}. " \
|
|
|
|
"You can specify one manually using e.g. `:inspector => 'chromium'` " \
|
|
|
|
"as a configuration option for Poltergeist."
|
|
|
|
end
|
|
|
|
end
|
2012-09-15 18:22:08 +02:00
|
|
|
|
|
|
|
def self.browser_binary_exists?(browser)
|
|
|
|
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
|
|
|
|
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
|
|
|
exts.each { |ext|
|
|
|
|
exe = "#{path}#{File::SEPARATOR}#{browser}#{ext}"
|
|
|
|
return exe if File.executable? exe
|
|
|
|
}
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
2012-02-29 00:02:43 +00:00
|
|
|
end
|
|
|
|
end
|