Class: BrowserStack::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/browserstack/local.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = ENV["BROWSERSTACK_ACCESS_KEY"]) ⇒ Local

Returns a new instance of Local.



10
11
12
13
14
15
16
# File 'lib/browserstack/local.rb', line 10

def initialize(key = ENV["BROWSERSTACK_ACCESS_KEY"])
  @key = key
  @user_arguments = []
  @logfile = File.join(Dir.pwd, "local.log")
  @is_windows = RbConfig::CONFIG['host_os'].match(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/)
  @exec = @is_windows ? "call" : "exec";
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



8
9
10
# File 'lib/browserstack/local.rb', line 8

def pid
  @pid
end

Instance Method Details

#add_args(key, value = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/browserstack/local.rb', line 18

def add_args(key, value=nil)
  if key == "key"
    @key = value
  elsif key == "v" && value.to_s != "false"
    @verbose_flag = "-vvv"
  elsif key == "force" && value.to_s != "false"
    @force_flag = "-force"
  elsif key == "only" && value.to_s != "false"
    @only_flag = "-only"
  elsif key == "onlyAutomate" && value.to_s != "false"
    @only_automate_flag = "-onlyAutomate"
  elsif key == "forcelocal" && value.to_s != "false"
    @force_local_flag = "-forcelocal"
  elsif key == "localIdentifier"
    @local_identifier_flag = value
  elsif key == "f"
    @folder_flag = "-f"
    @folder_path = value
  elsif key == "proxyHost"
    @proxy_host = value
  elsif key == "proxyPort"
    @proxy_port = value
  elsif key == "proxyUser"
    @proxy_user = value
  elsif key == "proxyPass"
    @proxy_pass = value
  elsif key == "hosts"
    @hosts = value
  elsif key == "logfile"
    @logfile = value
  elsif key == "binarypath"
    @binary_path = value
  elsif key == "forceproxy" && value.to_s != "false"
    @force_proxy_flag = "-forceproxy"
  else
    if value.to_s.downcase.eql?("true")
      @user_arguments << "-#{key}"
    else
      @user_arguments += ["-#{key}", value]
    end
  end
end

#commandObject



120
121
122
# File 'lib/browserstack/local.rb', line 120

def command
  start_command
end

#isRunningObject



104
105
106
# File 'lib/browserstack/local.rb', line 104

def isRunning
  return true if (!@pid.nil? && Process.kill(0, @pid)) rescue false
end

#start(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/browserstack/local.rb', line 61

def start(options = {})
  options.each_pair do |key, value|
    self.add_args(key, value)
  end

  @binary_path = if @binary_path.nil?
      BrowserStack::LocalBinary.new.binary_path
    else
      @binary_path
    end
  
  if @is_windows
    system("echo > #{@logfile}")
  else
    system("echo '' > '#{@logfile}'")
  end

  if defined? spawn
    @process = IO.popen(start_command_args)
  else
    @process = IO.popen(start_command)
  end

  while true
    begin
      line = @process.readline
    rescue EOFError => e
      sleep 1
      next
    end

    data = JSON.parse(line) rescue {"message" => "Unable to parse daemon mode JSON output"}
    if data['state'].to_s != "connected"
      @process.close
      raise BrowserStack::LocalException.new(data["message"]["message"])
      return
    else
      @pid = data["pid"]
      break
    end
  end
end

#start_commandObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/browserstack/local.rb', line 124

def start_command
  cmd = "#{@binary_path} -d start -logFile '#{@logfile}' #{@folder_flag} #{@key} #{@folder_path} #{@force_local_flag}"
  cmd += " -localIdentifier #{@local_identifier_flag}" if @local_identifier_flag
  cmd += " #{@only_flag} #{@only_automate_flag}"
  cmd += " -proxyHost #{@proxy_host}" if @proxy_host
  cmd += " -proxyPort #{@proxy_port}" if @proxy_port
  cmd += " -proxyUser #{@proxy_user}" if @proxy_user
  cmd += " -proxyPass #{@proxy_pass}" if @proxy_pass
  cmd += " #{@force_proxy_flag} #{@force_flag} #{@verbose_flag} #{@hosts} #{@user_arguments.join(" ")} 2>&1"
  cmd.strip
end

#start_command_argsObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/browserstack/local.rb', line 136

def start_command_args
  args = [@binary_path, "-d", "start", "-logFile", @logfile, @key, @folder_flag, @folder_path, @force_local_flag]
  args += ["-localIdentifier", @local_identifier_flag] if @local_identifier_flag
  args += [@only_flag, @only_automate_flag]
  args += ["-proxyHost", @proxy_host] if @proxy_host
  args += ["-proxyPort", @proxy_port] if @proxy_port
  args += ["-proxyUser", @proxy_user] if @proxy_user
  args += ["-proxyPass", @proxy_pass] if @proxy_pass
  args += [@force_proxy_flag, @force_flag, @verbose_flag, @hosts]
  args += @user_arguments

  args = args.select {|a| a.to_s != "" }
  args.push(:err => [:child, :out])
  args
end

#stopObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/browserstack/local.rb', line 108

def stop
  return if @pid.nil?
  @process.close
  if defined? spawn
    @process = IO.popen(stop_command_args)
  else
    @process = IO.popen(stop_command)
  end
  @process.close
  @pid = nil
end

#stop_commandObject



152
153
154
155
156
157
158
# File 'lib/browserstack/local.rb', line 152

def stop_command
  if @local_identifier_flag
    return "#{@binary_path} -d stop -localIdentifier #{@local_identifier_flag}".strip
  else
    return "#{@binary_path} -d stop".strip
  end
end

#stop_command_argsObject



160
161
162
163
164
165
166
# File 'lib/browserstack/local.rb', line 160

def stop_command_args
  args = ["#{@binary_path}", "-d", "stop"]
  args +=  ["-localIdentifier", "#{@local_identifier_flag}"] if @local_identifier_flag
  args = args.select {|a| a.to_s != "" }
  args.push(:err => [:child, :out])
  args
end