forked from malini/marketplace_tests
/
app_checker.py
431 lines (401 loc) · 18.4 KB
/
app_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
60
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
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import base64
import json
import logging
from optparse import OptionParser
import os
import shutil
import socket
import sys
import time
from gaiatest import GaiaApps, GaiaApp, GaiaDevice
from marionette import Marionette
from marionette.errors import ScriptTimeoutException, TimeoutException, MarionetteException, JavascriptException
from marionette.wait import Wait
from mozdevice import DeviceManagerADB
import moznetwork
#TODO refactor this god object
class TestRun(object):
def __init__(self, adb="adb", serial=None):
self.test_results = {}
self.test_results_file = None
self.m = None
self.gaia_apps = None
self.screenshot_path = None
self.logcat_path = None
self.app_name = None
self.attempt = None
self.num_apps = None
self.device = None
self.serial = serial
self.port = None
self.run_log = logging.getLogger('marketplace-test')
if self.serial:
self.dm = DeviceManagerADB(adbPath=adb, deviceSerial=serial)
else:
self.dm = DeviceManagerADB(adbPath=adb)
def reset_marionette(self):
try:
self.m.delete_session()
except Exception:
pass
self.m = None
self.get_marionette()
def get_marionette(self):
if not self.m:
self.m = Marionette(port=self.port)
self.m.start_session()
self.device = GaiaDevice(self.m)
self.device.add_device_manager(self.dm)
self.gaia_apps = GaiaApps(self.m)
else:
tries = 5
while tries > 0:
try:
self.m.get_url()
break
except MarionetteException as e:
if "Please start a session" in str(e):
time.sleep(5)
self.m = Marionette(port=self.port)
self.m.start_session()
self.device = GaiaDevice(self.m)
self.device.add_device_manager(self.dm)
self.gaia_apps = GaiaApps(self.m)
tries -= 1
else:
raise e
else:
self.run_log.error("Can't connect to marionette, rebooting")
self.restart_device()
return self.m
def write_to_file(self, data):
with open("%s.tmp" % self.test_results_file, "w") as f:
f.write(data)
shutil.copyfile("%s.tmp" % self.test_results_file, self.test_results_file)
def add_values(self, key, value):
if self.serial:
self.test_results["%s_%s" % (key, self.serial)] = value
else:
self.test_results["%s" % key] = value
def add_result(self, passed=False, status=None, uninstalled_failure=False):
values = {}
if status:
if not passed:
values["status"] = "FAILED: %s" % status
else:
values["status"] = "PASS"
if self.screenshot_path:
values["screenshot"] = self.screenshot_path
if self.logcat_path:
values["logcat"] = self.logcat_path
if uninstalled_failure:
values["uninstalled_failure"] = uninstalled_failure
entry = "%s_%s" % (self.app_name, self.attempt)
self.test_results[entry] = values
def launch_with_manifest(self, manifest):
self.m.switch_to_frame()
result = self.m.execute_async_script("GaiaApps.launchWithManifestURL('%s')" % manifest, script_timeout=30000)
if result == False:
raise Exception("launch timed out")
app = GaiaApp(frame=result.get('frame'),
src=result.get('src'),
name=result.get('name'),
origin=result.get('origin'))
if app.frame_id is None:
raise Exception("failed to launch; there is no app frame")
self.m.switch_to_frame(app.frame_id)
return app
def uninstall_with_manifest(self, manifest):
self.m.switch_to_frame()
script = """
GaiaApps.locateWithManifestURL('%s',
null,
function uninstall(app) {
navigator.mozApps.mgmt.uninstall(app);
marionetteScriptFinished(true);
});
"""
result = self.m.execute_async_script(script % manifest, script_timeout=60000)
if result != True:
self.add_result(status="Failed to uninstall app with url '%s'" % manifest)
return app
def forward_port(self):
# get unused port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 0))
addr, port = s.getsockname()
s.close()
dm_tries = 0
self.run_log.info("using port %s" % port)
while dm_tries < 20:
if self.dm.forward("tcp:%d" % port, "tcp:2828") == 0:
break
dm_tries += 1
time.sleep(3)
else:
return False
self.port = port
return True
def restart_device(self, restart_tries=0):
self.run_log.info("rebooting")
# TODO restarting b2g doesn't seem to work... reboot then
while restart_tries < 3:
restart_tries += 1
self.dm.reboot(wait=True)
self.run_log.info("forwarding")
if not self.forward_port():
self.run_log.error("couldn't forward port in time, rebooting")
continue
self.m = Marionette(port=self.port)
if not self.m.wait_for_port(180):
self.run_log.error("couldn't contact marionette in time, rebooting")
continue
time.sleep(1)
self.m.start_session()
try:
Wait(self.m, timeout=240).until(lambda m: m.find_element("id", "lockscreen-container").is_displayed())
# It retuns a little early
time.sleep(2)
self.device = GaiaDevice(self.m)
self.device.add_device_manager(self.dm)
self.device.unlock()
self.gaia_apps = GaiaApps(self.m)
except (MarionetteException, IOError, socket.error) as e:
self.run_log.error("got exception: %s, going to retry" % e)
try:
self.m.delete_session()
except:
# at least attempt to clear the session if possible
pass
continue
break
else:
raise Exception("Couldn't restart the device in time, even after 3 tries")
def readystate_wait(self, app):
try:
Wait(self.get_marionette(), timeout=30).until(lambda m: m.execute_script("return window.document.readyState;") == "complete")
except ScriptTimeoutException as e:
return False
return True
def record_icons(self):
self.device.touch_home_button()
icons = self.m.find_elements("class name", "icon")
self.num_apps = len(icons)
def check_if_app_installed(self, timeout=180):
# TODO: Find a better way to do this than checking homescreen
# I hope there is one...
self.device.touch_home_button()
icons = self.m.find_elements("class name", "icon")
start = time.time()
end = start + 180
found_icon = None
claims_its_loaded = 0 # this is used in case 'loading' isn't applied immediately to the icon
while time.time() < end:
if not found_icon:
icons = self.m.find_elements("class name", "icon")
# We can't do set comparison b/c references change
if len(icons) > self.num_apps:
for icon in icons:
if "loading" in icon.get_attribute("innerHTML"):
found_icon = icon
break
else:
claims_its_loaded += 1
if claims_its_loaded == 3:
return True
else:
if "loading" not in found_icon.get_attribute("innerHTML"):
return True
time.sleep(2)
return False
def cli():
parser = OptionParser(usage="usage: %prog [options] manifestFile")
parser.add_option("--device", dest="device", default=None,
help="Device id of the device you want to test against. "\
"If not passed in, it will assume there is only one " \
"device plugged in and will use that one.")
parser.add_option("--adb-path", dest="adb_path", default="adb",
help="path to adb executable. If not passed, we assume "\
"that 'adb' is on the path")
parser.add_option("--range", dest="chunk",
help="Runs the given index range of the manifest")
(options, args) = parser.parse_args()
manifest_file = "manifest.json"
if args:
manifest_file = args[0]
test_run = TestRun(adb=options.adb_path, serial=options.device)
run_log_file = None
if options.chunk:
options.chunk = options.chunk.split(",")
run_log_file = "run_log_%s.log" % int(options.chunk[0])
test_run.test_results_file = "test_results_%s.json" % int(options.chunk[0])
else:
run_log_file = "run_log.log"
test_run.test_results_file = "test_results.json"
test_run.run_log.setLevel(logging.INFO)
fh = logging.FileHandler(run_log_file)
fh.setLevel(logging.DEBUG)
test_run.run_log.addHandler(fh)
test_run.run_log.info("results_file: %s" % test_run.test_results_file)
# Load manifest
apps = None
with open(manifest_file, 'r') as f:
apps = json.loads(f.read())
# use given chunk if applicable
if options.chunk:
apps = apps[int(options.chunk[0]):int(options.chunk[1])]
test_run.add_values("chunk", options.chunk)
test_run.forward_port()
test_run.get_marionette()
test_run.gaia_apps.kill_all()
install_script = """
var installUrl = '%s';
var request = window.navigator.mozApps.%s(installUrl);
request.onsuccess = function () {
window.wrappedJSObject.marionette_install_result = true;
};
request.onerror = function () {
window.wrappedJSObject.marionette_install_result = this.error.name;
};
"""
# TODO: assumes adb is on path
if not os.path.exists("logcats"):
os.makedirs("logcats")
logcat = test_run.dm.getLogcat()
test_run.device.unlock()
# Get first logact before tests start
with open("logcats/before_test_%s.log" % int(time.time()), "w") as f:
for line in logcat:
f.write(line)
start_time = time.time()
for app in apps:
# clear logcat
app_name = app["app_name"]
test_run.app_name = app_name
installed = False
# Checkpoint
test_run.add_values("Total Time", time.time() - start_time)
test_run.write_to_file(json.dumps(test_run.test_results))
for attempt in [1, 2]:
test_run.attempt = attempt
exception_occurred = False
test_run.screenshot_path = None
test_run.logcat_path = "logcats/%s_%d_%s.log" % (app_name, attempt, int(time.time()))
test_run.run_log.info("testing: %s %s" % (app_name, attempt))
try:
test_run.record_icons()
# launch (or switch to) marketplace, wait 3 minutes for successful launch
if not installed:
test_run.run_log.info('installing %s' % app_name)
# go to system app
test_run.get_marionette().switch_to_frame()
marketplace_app = test_run.gaia_apps.launch("Browser", switch_to_frame=True, launch_timeout=60000)
test_run.get_marionette().navigate("https://marketplace.firefox.com")
if not test_run.readystate_wait("marketplace"):
continue
# trigger the install
if app["is_packaged"]:
test_run.get_marionette().execute_script(install_script % (app["app_manifest"], "installPackage"), script_timeout=30000)
else:
test_run.get_marionette().execute_script(install_script % (app["app_manifest"], "install"), script_timeout=30000)
# go to system app
test_run.get_marionette().switch_to_frame()
# click Install
for el in test_run.get_marionette().find_elements("tag name", "button"):
if "Install" == el.text:
el.tap()
# get back into marketplace
test_run.gaia_apps.switch_to_displayed_app()
# Did the app get installed?
result = test_run.get_marionette().execute_script("return window.wrappedJSObject.marionette_install_result")
if result != True:
test_run.add_result(status="failed to install, didn't get onsuccess: %s" % result)
continue
# switch to system frame to check if the app *fully* installed
test_run.get_marionette().switch_to_frame()
try:
Wait(test_run.get_marionette(), timeout=120).until(lambda m:
m.execute_script('return window.wrappedJSObject.Applications.getByManifestURL("%s");' % app["app_manifest"]) != None)
Wait(test_run.get_marionette(), timeout=120).until(lambda m:
m.execute_script('return window.wrappedJSObject.Applications.getByManifestURL("%s").manifest != undefined;' % app["app_manifest"]))
# DOMRequest onsuccess for install returns too quickly, and so does the Applications manager
if not test_run.check_if_app_installed():
raise TimeoutException("Didn't install the app in time")
except (TimeoutException, ScriptTimeoutException) as e:
test_run.add_result(status="failed to install: %s" % e)
continue
installed = True
test_run.run_log.info('launching')
try:
# ensure we're in system frame
test_run.get_marionette().switch_to_frame()
app_under_test = test_run.launch_with_manifest(app["app_manifest"])
except Exception as e:
test_run.add_result(status=str(e))
continue
if not test_run.readystate_wait(app_name):
test_run.add_result(status="timed out waiting for %s" % app)
continue
test_run.run_log.info('launched')
shot = test_run.get_marionette().screenshot()
img = base64.b64decode(shot.encode('ascii'))
if not os.path.exists("screenshots"):
os.makedirs("screenshots")
test_run.screenshot_path = "screenshots/%s_%d_%s.png" % (app_name, attempt, int(time.time()))
with open(test_run.screenshot_path, "w") as f:
f.write(img)
test_run.device.touch_home_button()
# go back to system app
test_run.get_marionette().switch_to_frame()
test_run.gaia_apps.kill_all()
test_run.add_result(passed=True)
except (TimeoutException, JavascriptException, socket.error, IOError) as e:
#NOTE: JavascriptExceptions are added because if we get in a weird
# state in gaia, we get errors when we execute JS, so we reboot
exception_occurred = True
test_run.add_result(status="Connection timeout, restarting phone")
test_run.run_log.error("connection error occurred, restarting %s" % e)
test_run.restart_device()
continue
except ScriptTimeoutException as e:
exception_occurred = True
test_run.add_result(status="Script timeout: %s running next test" % e)
test_run.run_log.error("Script timeout: %s running next test" % e)
continue
except MarionetteException as e:
exception_occurred = True
test_run.add_result(status="Script error: %s running next test" % e)
test_run.run_log.error("Marionette script error: %s running next test" % e)
test_run.run_log.error('restarting')
# if we can't get a session here, we reset
test_run.restart_device()
continue
except (KeyboardInterrupt, Exception) as e:
exception_occurred = True
entry = "%s_%s" % (app_name, attempt)
if not test_run.test_results.has_key(entry):
test_run.add_result(status="Unknown failure: %s" % e)
test_run.add_values("Suite failure when running %s" % app_name, str(e))
test_run.add_values("Total Time", time.time() - start_time)
test_run.write_to_file(json.dumps(test_run.test_results))
raise e
finally:
if (attempt == 2) or exception_occurred:
try:
test_run.uninstall_with_manifest(app["app_manifest"])
installed = False
except Exception as e:
entry = "%s_%s" % (app_name, attempt)
if not test_run.test_results.has_key(entry):
test_run.test_results(status="Failed to uninstall", uninstalled_failure=str(e))
else:
test_run.add_values("Uninstalled", "FAILED: %s" % str(e))
logcat = test_run.dm.getLogcat()
test_run.dm._checkCmd(["logcat", "-c"])
with open(test_run.logcat_path, "w") as f:
for line in logcat:
f.write(line)
test_run.add_values("Total Time", time.time() - start_time)
test_run.write_to_file(json.dumps(test_run.test_results))
if __name__ == "__main__":
cli()