class Simulacrum::Browserstack::Tunnel

Class for handling Browserstack tunnel opening/closing/management

Constants

DEFAULT_OPTIONS

Attributes

open[R]
open?[R]
pid[R]
ports[R]
selenium_remote_url[R]

Public Class Methods

new(username, apikey, ports, options = {}) click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 20
def initialize(username, apikey, ports, options = {})
  @pid = nil
  @open = false
  @username = username
  @apikey = apikey
  @ports = ports
  @options = OpenStruct.new(DEFAULT_OPTIONS.clone.merge!(options))

  create_tunnel
  ensure_open
end

Public Instance Methods

close() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 36
def close
  kill
end

Private Instance Methods

binary_path() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 42
def binary_path
  binary_path = `which BrowserStackLocal`.strip
  if $CHILD_STATUS.success? && File.executable?(binary_path)
    binary_path
  else
    Simulacrum.logger.fail('BrowserStack') { 'BrowserStackLocal binary not found or not executable' }
    fail
  end
end
command() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 58
def command
  cmd = [binary_path]
  cmd << '-skipCheck'    if @options.skip_check == true
  cmd << '-onlyAutomate' if @options.only_automate == true
  cmd << '-force'        if @options.force == true
  cmd << '-v'            if @options.verbose == true
  cmd << @apikey
  cmd << formatted_ports
  cmd.join(' ')
end
create_tunnel() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 52
def create_tunnel
  @process = IO.popen(command)
  @pid = @process.pid
  Simulacrum.logger.debug('BrowserStack') { "Openning tunnel (pid #{@pid})" }
end
ensure_open() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 77
def ensure_open
  Simulacrum.logger.debug('BrowserStack') { 'Waiting for tunnel to open' }
  Timeout.timeout(240) do
    sleep 1 until tunnel_open?
  end
  @open = true
  Simulacrum.logger.debug('BrowserStack') { 'Tunnel open' }
rescue Timeout::Error
  Simulacrum.logger.debug('BrowserStack') { 'Tunnel failed to open, exiting.' }
  exit(1)
end
formatted_ports() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 69
def formatted_ports
  ports.map { |port| "localhost,#{port},0" }.join(',')
end
kill() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 73
def kill
  Process.kill('TERM', @pid) if running?
end
running?() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 93
def running?
  if @pid.nil?
    false
  else
    Process.getpgid(@pid)
    true
  end
rescue Errno::ESRCH
  false
end
tunnel_open?() click to toggle source
# File lib/simulacrum/browserstack/tunnel.rb, line 89
def tunnel_open?
  `curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:45691`.to_i == 200
end