Class: Calabash::Cucumber::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/calabash-cucumber/launcher.rb

Defined Under Namespace

Classes: CalabashLauncherTimeoutErr, StartError

Constant Summary collapse

@@launcher =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLauncher

Returns a new instance of Launcher.



37
38
39
# File 'lib/calabash-cucumber/launcher.rb', line 37

def initialize
  @@launcher = self
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



9
10
11
# File 'lib/calabash-cucumber/launcher.rb', line 9

def device
  @device
end

#launch_argsObject

Returns the value of attribute launch_args.



10
11
12
# File 'lib/calabash-cucumber/launcher.rb', line 10

def launch_args
  @launch_args
end

#run_loopObject

Returns the value of attribute run_loop.



8
9
10
# File 'lib/calabash-cucumber/launcher.rb', line 8

def run_loop
  @run_loop
end

Class Method Details

.launcherObject



29
30
31
# File 'lib/calabash-cucumber/launcher.rb', line 29

def self.launcher
  @@launcher ||= Launcher.new
end

.launcher_if_usedObject



33
34
35
# File 'lib/calabash-cucumber/launcher.rb', line 33

def self.launcher_if_used
  @@launcher
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


362
363
364
# File 'lib/calabash-cucumber/launcher.rb', line 362

def active?
  not run_loop.nil?
end

#app_pathObject



354
355
356
# File 'lib/calabash-cucumber/launcher.rb', line 354

def app_path
  ENV['APP_BUNDLE_PATH'] || (defined?(APP_BUNDLE_PATH) && APP_BUNDLE_PATH) || ENV['APP']
end

#calabash_no_launch?Boolean

Returns:

  • (Boolean)


326
327
328
# File 'lib/calabash-cucumber/launcher.rb', line 326

def calabash_no_launch?
  ENV['NO_LAUNCH']=='1'
end

#calabash_no_stop?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/calabash-cucumber/launcher.rb', line 322

def calabash_no_stop?
  calabash_no_launch? or ENV['NO_STOP']=="1"
end

#calabash_notify(world) ⇒ Object



297
298
299
300
301
# File 'lib/calabash-cucumber/launcher.rb', line 297

def calabash_notify(world)
  if world.respond_to?(:on_launch)
    world.on_launch
  end
end

#default_launch_argsObject



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
103
104
105
# File 'lib/calabash-cucumber/launcher.rb', line 65

def default_launch_args
  # APP_BUNDLE_PATH
  # BUNDLE_ID
  # APP (unifies APP_BUNDLE_PATH, BUNDLE_ID)
  # DEVICE_TARGET
  # SDK_VERSION
  # RESET_BETWEEN_SCENARIOS
  # DEVICE
  # NO_LAUNCH
  # NO_STOP

  args = {
      :launch_method => default_launch_method,
      :reset => reset_between_scenarios?,
      :bundle_id => ENV['BUNDLE_ID'],
      :device => device_env,
      :no_stop => calabash_no_stop?,
      :no_launch => calabash_no_launch?,
      :sdk_version => sdk_version
  }

  #:device_target will be set

  if run_with_instruments?(args) && !simulator_target?
    device_tgt = ENV['DEVICE_TARGET']
    if detect_connected_device? && (device_tgt.nil? || device_tgt.downcase == 'device')
      device_tgt = RunLoop::Core.detect_connected_device
    end

    if device_tgt
      args[:device_target] = args[:udid] = device_tgt
    else
      args[:device_target] = 'simulator'
    end
  else
    args[:device_target] = 'simulator'
  end


  args
end

#default_launch_methodObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/calabash-cucumber/launcher.rb', line 111

def default_launch_method
  return :instruments unless sdk_version || use_instruments_env?
  return :instruments if sdk_version.start_with?('7') # Only instruments supported for iOS7+
  sim_detector = SimLauncher::SdkDetector.new()
  available = sim_detector.available_sdk_versions.reject { |v| v.start_with?('7') }
  if available.include?(sdk_version)
    :sim_launcher
  else
    :instruments
  end
end

#detect_app_bundle_from_args(args) ⇒ Object



192
193
194
195
196
197
198
199
# File 'lib/calabash-cucumber/launcher.rb', line 192

def detect_app_bundle_from_args(args)
  if args[:device_target]=='simulator'
    device_xamarin_build_dir = 'iPhoneSimulator'
  else
    device_xamarin_build_dir = 'iPhone'
  end
  Calabash::Cucumber::SimulatorHelper.detect_app_bundle(nil, device_xamarin_build_dir)
end

#detect_bundle_idObject



312
313
314
315
316
317
318
319
320
# File 'lib/calabash-cucumber/launcher.rb', line 312

def detect_bundle_id
  begin
    bundle_path = Calabash::Cucumber::SimulatorHelper.app_bundle_or_raise(app_path)
    plist_path = File.join(bundle_path, 'Info.plist')
    info_plist_as_hash(plist_path)['CFBundleIdentifier']
  rescue => e
    raise "Unable to automatically find bundle id. Please set BUNDLE_ID environment variable. #{e}"
  end
end

#detect_bundle_id_from_app_bundle(args) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/calabash-cucumber/launcher.rb', line 201

def detect_bundle_id_from_app_bundle(args)
  if args[:app] && File.directory?(args[:app])
    # Derive bundle id from bundle_dir
    plist_as_hash = info_plist_from_bundle_path(args[:app])
    if plist_as_hash
      plist_as_hash['CFBundleIdentifier']
    end
  else
    args[:app]
  end
end

#detect_connected_device?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/calabash-cucumber/launcher.rb', line 107

def detect_connected_device?
    return ENV['DETECT_CONNECTED_DEVICE'] != '0'
end

#detect_device_from_args(args) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/calabash-cucumber/launcher.rb', line 170

def detect_device_from_args(args)
  if args[:app] && File.directory?(args[:app])
    # Derive bundle id from bundle_dir
    plist_as_hash = info_plist_from_bundle_path(args[:app])
    if plist_as_hash
      device_family = plist_as_hash['UIDeviceFamily']
      if device_family
        first_device = device_family.first
        if first_device == 2
          return 'ipad'
        else
          return 'iphone'
        end
      end
    end
  else
    args[:app]
  end


end

#device_envObject



350
351
352
# File 'lib/calabash-cucumber/launcher.rb', line 350

def device_env
  ENV['DEVICE']
end

#device_target?Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/calabash-cucumber/launcher.rb', line 330

def device_target?
  (ENV['DEVICE_TARGET'] != nil) && (not simulator_target?)
end

#ensure_connectivityObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/calabash-cucumber/launcher.rb', line 233

def ensure_connectivity
  begin
    max_retry_count = (ENV['MAX_CONNECT_RETRY'] || 10).to_i
    timeout = (ENV['CONNECT_TIMEOUT'] || 30).to_i
    retry_count = 0
    connected = false
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      puts "Waiting for App to be ready"
    end
    until connected do
      raise "MAX_RETRIES" if retry_count == max_retry_count
      retry_count += 1
      begin
        Timeout::timeout(timeout, CalabashLauncherTimeoutErr) do
          until connected
            begin
              connected = (ping_app == '200')
              break if connected
            rescue Exception => e
              #p e
              #retry
            ensure
              sleep 1 unless connected
            end
          end
        end
      rescue CalabashLauncherTimeoutErr => e
        puts "Timed out...Retry.."
      end
    end
  rescue RuntimeError => e
    p e
    msg = "Unable to make connection to Calabash Server at #{ENV['DEVICE_ENDPOINT']|| "http://localhost:37265/"}\n"
    msg << "Make sure you don't have a firewall blocking traffic to #{ENV['DEVICE_ENDPOINT']|| "http://localhost:37265/"}.\n"
    raise msg
  end
end

#info_plist_as_hash(plist_path) ⇒ Object



304
305
306
307
308
309
310
# File 'lib/calabash-cucumber/launcher.rb', line 304

def info_plist_as_hash(plist_path)
  unless File.exist?(plist_path)
    raise "Unable to find Info.plist: #{plist_path}"
  end
  parsedplist = CFPropertyList::List.new(:file => plist_path)
  CFPropertyList.native_types(parsedplist.value)
end

#info_plist_from_bundle_path(bundle_path) ⇒ Object



213
214
215
216
# File 'lib/calabash-cucumber/launcher.rb', line 213

def info_plist_from_bundle_path(bundle_path)
  plist_path = File.join(bundle_path, 'Info.plist')
  info_plist_as_hash(plist_path) if File.exist?(plist_path)
end

#inspectObject



366
367
368
369
370
371
372
# File 'lib/calabash-cucumber/launcher.rb', line 366

def inspect
  msg = ["#{self.class}: Launch Method #{launch_args[:launch_method]}"]
  if run_with_instruments?(self.launch_args) && self.run_loop
    msg << "Log file: #{self.run_loop[:log_file]}"
  end
  msg.join("\n")
end

#ios_major_versionObject



41
42
43
44
# File 'lib/calabash-cucumber/launcher.rb', line 41

def ios_major_version
  return nil if device.nil? or device.ios_version.nil?
  device.ios_major_version
end

#ios_versionObject



46
47
48
49
# File 'lib/calabash-cucumber/launcher.rb', line 46

def ios_version
  return nil if device.nil?
  device.ios_version
end

#new_run_loop(args) ⇒ Object

Raises:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/calabash-cucumber/launcher.rb', line 218

def new_run_loop(args)
  last_err = nil
  3.times do
    begin
      return RunLoop.run(args)
    rescue RunLoop::TimeoutError => e
      last_err = e
      if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
        puts "retrying run loop..."
      end
    end
  end
  raise StartError.new(last_err)
end

#ping_appObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/calabash-cucumber/launcher.rb', line 271

def ping_app
  url = URI.parse(ENV['DEVICE_ENDPOINT']|| "http://localhost:37265/")

  http = Net::HTTP.new(url.host, url.port)
  res = http.start do |sess|
    sess.request Net::HTTP::Get.new(ENV['CALABASH_VERSION_PATH'] || "version")
  end
  status = res.code
  begin
    http.finish if http and http.started?
  rescue Exception => e

  end

  if status == '200'
    version_body = JSON.parse(res.body)
    self.device = Calabash::Cucumber::Device.new(url, version_body)
  end

  status
end

#relaunch(args = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/calabash-cucumber/launcher.rb', line 123

def relaunch(args={})
  RunLoop.stop(run_loop) if run_loop

  args = default_launch_args.merge(args)

  args[:app] = args[:app] || args[:bundle_id] || app_path || detect_app_bundle_from_args(args)


  if args[:app]
    if File.directory?(args[:app])
      args[:app] = File.expand_path(args[:app])
    else
      # args[:app] is not a directory so must be a bundle id
      if args[:device_target] == 'simulator' ## bundle id set, but simulator target
        args[:app] = app_path || detect_app_bundle_from_args(args)
      end
    end
  end

  unless args[:app]
    if args[:device_target]=='simulator'
      device_xamarin_build_dir = 'iPhoneSimulator'
    else
      device_xamarin_build_dir = 'iPhone'
    end
    args[:app] = Calabash::Cucumber::SimulatorHelper.app_bundle_or_raise(app_path, device_xamarin_build_dir)
  end

  args[:bundle_id] ||= detect_bundle_id_from_app_bundle(args)

  args[:device] ||= detect_device_from_args(args)


  reset_app_jail if args[:reset]

  if run_with_instruments?(args)
    self.run_loop = new_run_loop(args)
  else
    # run with sim launcher
    sdk = sdk_version || SimLauncher::SdkDetector.new().available_sdk_versions.reverse.find { |x| !x.start_with?('7') }
    path = Calabash::Cucumber::SimulatorHelper.app_bundle_or_raise(app_path)
    Calabash::Cucumber::SimulatorHelper.relaunch(path, sdk, args[:device].to_s, args)
  end
  self.launch_args = args
  ensure_connectivity
end

#reset_app_jail(sdk = nil, path = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/calabash-cucumber/launcher.rb', line 51

def reset_app_jail(sdk=nil, path=nil)
  sdk ||= sdk_version || SimLauncher::SdkDetector.new().latest_sdk_version
  path ||= Calabash::Cucumber::SimulatorHelper.app_bundle_or_raise(app_path)

  app = File.basename(path)
  bundle = `find "#{ENV['HOME']}/Library/Application Support/iPhone Simulator/#{sdk}/Applications/" -type d -depth 2 -name "#{app}" | head -n 1`
  return if bundle.empty? # Assuming we're already clean

  sandbox = File.dirname(bundle)
  ['Library', 'Documents', 'tmp'].each do |dir|
    FileUtils.rm_rf(File.join(sandbox, dir))
  end
end

#reset_between_scenarios?Boolean

Returns:

  • (Boolean)


346
347
348
# File 'lib/calabash-cucumber/launcher.rb', line 346

def reset_between_scenarios?
  ENV['RESET_BETWEEN_SCENARIOS']=="1"
end

#run_with_instruments?(args) ⇒ Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/calabash-cucumber/launcher.rb', line 358

def run_with_instruments?(args)
  args[:launch_method] == :instruments
end

#sdk_versionObject



338
339
340
# File 'lib/calabash-cucumber/launcher.rb', line 338

def sdk_version
  ENV['SDK_VERSION']
end

#simulator_target?Boolean

Returns:

  • (Boolean)


334
335
336
# File 'lib/calabash-cucumber/launcher.rb', line 334

def simulator_target?
  ENV['DEVICE_TARGET'] == 'simulator'
end

#stopObject



293
294
295
# File 'lib/calabash-cucumber/launcher.rb', line 293

def stop
  RunLoop.stop(run_loop)
end

#use_instruments_env?Boolean

Returns:

  • (Boolean)


342
343
344
# File 'lib/calabash-cucumber/launcher.rb', line 342

def use_instruments_env?
  ENV['LAUNCH_VIA'] == 'instruments'
end