Module: Kopflos

Defined in:
lib/kopflos/xvfb.rb,
lib/kopflos.rb

Overview

heavily inspired by selenium-client

https://github.com/jodell/selenium-client/blob/da0f3dfbc05a6377c0cada09a3d650daf1261415/lib/xvfb/xvfb.rb

Defined Under Namespace

Classes: AlreadyRunning, Xvfb

Constant Summary collapse

EnvVar =
'KOPFLOS'

Class Method Summary collapse

Class Method Details

.disabled_by_env_variable?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/kopflos.rb', line 46

def self.disabled_by_env_variable?
  %w(false disabled 0 disable no).include?(ENV[EnvVar].to_s)
end

.disabled_by_switch_file?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/kopflos.rb', line 50

def self.disabled_by_switch_file?
  File.exists?( switch_file_path )
end

.log(message) ⇒ Object



58
59
60
61
62
# File 'lib/kopflos.rb', line 58

def self.log(message)
  if ENV['LOG']
    STDERR.puts "#{self}: #{message}"
  end
end

.reset!Object



41
42
43
44
# File 'lib/kopflos.rb', line 41

def self.reset!
  stop
  @server = nil
end

.running?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/kopflos.rb', line 37

def self.running?
  @server && @server.running?
end

.start(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kopflos.rb', line 8

def self.start(opts={})
  if disabled_by_env_variable?
    log "disabled through environment variable #{EnvVar} (set to anything else than 'false' or '0')"
    return
  end
  if disabled_by_switch_file?
    log "disabled through switch file #{switch_file_path} (unlink to hide the frakkin browser windows again)"
    return
  end
  if running?
    unless opts[:reuse]
      raise AlreadyRunning, "there is already a server running: #{@server}"
    end
  else
    @server = Xvfb.new(opts)
    @server.start
    @server
  end
rescue Xvfb::NotSupported => e
  log "your platform is not supported (yet): #{RUBY_PLATFORM}"
end

.stopObject



30
31
32
33
34
35
# File 'lib/kopflos.rb', line 30

def self.stop
  if running?
    @server.stop
    @server = nil
  end
end

.switch_file_pathObject



54
55
56
# File 'lib/kopflos.rb', line 54

def self.switch_file_path
  'kopflos_disabled'
end