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.


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

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


10
11
12
# File 'lib/capybara/poltergeist/inspector.rb', line 10

def port
  @port
end

Class Method Details

.browser_binary_exists?(browser) ⇒ Boolean

Returns:

  • (Boolean)

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

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


6
7
8
# File 'lib/capybara/poltergeist/inspector.rb', line 6

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

Instance Method Details

#browserObject


17
18
19
# File 'lib/capybara/poltergeist/inspector.rb', line 17

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

#openObject


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

def open
  if browser
    Process.spawn(browser, url)
  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

#urlObject


21
22
23
# File 'lib/capybara/poltergeist/inspector.rb', line 21

def url
  "http://localhost:#{port}/"
end