Class: Capybara::Poltergeist::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/poltergeist/inspector.rb

Constant Summary collapse

BROWSERS =
%w(chromium chromium-browser google-chrome open)
DEFAULT_PORT =
9664

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser = nil, port = DEFAULT_PORT) ⇒ Inspector

Returns a new instance of Inspector.



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

def initialize(browser = nil, port = DEFAULT_PORT)
  @browser = browser.respond_to?(:to_str) ? browser : nil
  @port    = port
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/capybara/poltergeist/inspector.rb', line 12

def port
  @port
end

Class Method Details

.browser_binary_exists?(browser) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# File 'lib/capybara/poltergeist/inspector.rb', line 37

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

.detect_browserObject



8
9
10
# File 'lib/capybara/poltergeist/inspector.rb', line 8

def self.detect_browser
  @browser ||= BROWSERS.find { |name| browser_binary_exists?(name) }
end

Instance Method Details

#browserObject



19
20
21
# File 'lib/capybara/poltergeist/inspector.rb', line 19

def browser
  @browser ||= self.class.detect_browser
end

#open(scheme) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/capybara/poltergeist/inspector.rb', line 27

def open(scheme)
  if browser
    Process.spawn(browser, url(scheme))
  else
    raise Error, "Could not find a browser executable to open #{url(scheme)}. " \
                 "You can specify one manually using e.g. `:inspector => 'chromium'` " \
                 "as a configuration option for Poltergeist."
  end
end

#url(scheme) ⇒ Object



23
24
25
# File 'lib/capybara/poltergeist/inspector.rb', line 23

def url(scheme)
  "#{scheme}://localhost:#{port}/"
end