Class: Calabash::Cucumber::Launcher

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

Defined Under Namespace

Classes: CalabashLauncherTimeoutErr

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_target = :simulator) ⇒ Launcher

Returns a new instance of Launcher.


12
13
14
# File 'lib/calabash-cucumber/launcher.rb', line 12

def initialize(device_target=:simulator)
  self.device_target = device_target
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device


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

def device
  @device
end

#device_targetObject

Returns the value of attribute device_target


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

def device_target
  @device_target
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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/calabash-cucumber/launcher.rb', line 35

def active?
  (simulator_target? || device_target?) && (not run_loop.nil?)
end

#app_pathObject


160
161
162
# File 'lib/calabash-cucumber/launcher.rb', line 160

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

#calabash_no_launch?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/calabash-cucumber/launcher.rb', line 23

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

#calabash_no_stop?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/calabash-cucumber/launcher.rb', line 19

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

#calabash_notify(world) ⇒ Object


164
165
166
167
168
# File 'lib/calabash-cucumber/launcher.rb', line 164

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

#device_target?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/calabash-cucumber/launcher.rb', line 27

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

#ensure_connectivityObject


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/calabash-cucumber/launcher.rb', line 98

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
    puts "Waiting for App to be ready"
    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

#ios_major_versionObject


39
40
41
42
# File 'lib/calabash-cucumber/launcher.rb', line 39

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

#ios_versionObject


44
45
46
47
# File 'lib/calabash-cucumber/launcher.rb', line 44

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

#ping_appObject


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/calabash-cucumber/launcher.rb', line 134

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


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

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

  if device_target?
    default_args = {:app => ENV['BUNDLE_ID']}
    target = ENV['DEVICE_TARGET']
    if target != 'DEVICE'
      default_args[:udid] = target
    end
    self.run_loop = RunLoop.run(default_args.merge(args))
  else

    sdk = ENV['SDK_VERSION'] || SimLauncher::SdkDetector.new().latest_sdk_version
    path = Calabash::Cucumber::SimulatorHelper.app_bundle_or_raise(app_path)
    if ENV['RESET_BETWEEN_SCENARIOS']=="1"
      reset_app_jail(sdk, path)
    end

    if simulator_target?
      device = (ENV['DEVICE'] || 'iphone').to_sym
      default_args = {:app => path, :device => device}
      self.run_loop = RunLoop.run(default_args.merge(args))
    else
      ## sim launcher
      Calabash::Cucumber::SimulatorHelper.relaunch(path, sdk, ENV['DEVICE'] || 'iphone', args)
    end

  end
  ensure_connectivity
end

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


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

def reset_app_jail(sdk=nil, path=nil)
  return if device_target?

  sdk = sdk || ENV['SDK_VERSION'] || SimLauncher::SdkDetector.new().latest_sdk_version
  path = 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

#simulator_target?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/calabash-cucumber/launcher.rb', line 31

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

#stopObject


156
157
158
# File 'lib/calabash-cucumber/launcher.rb', line 156

def stop
  RunLoop.stop(run_loop)
end