|
| 1 | +import os |
| 2 | + |
| 3 | +from core.osutils.command import run |
| 4 | +from core.osutils.command_log_level import CommandLogLevel |
| 5 | +from core.osutils.file import File |
| 6 | +from core.osutils.os_type import OSType |
| 7 | +from core.settings.settings import CURRENT_OS |
| 8 | + |
| 9 | +ANDROID_HOME = os.environ.get('ANDROID_HOME') |
| 10 | +ADB_PATH = os.path.join(ANDROID_HOME, 'platform-tools', 'adb') |
| 11 | + |
| 12 | + |
| 13 | +class Adb(object): |
| 14 | + @staticmethod |
| 15 | + def __find_aapt(): |
| 16 | + """ |
| 17 | + Find aapt tool under $ANDRODI_HOME/build-tools |
| 18 | + :return: Path to appt. |
| 19 | + """ |
| 20 | + aapt_executable = 'aapt' |
| 21 | + if CURRENT_OS is OSType.WINDOWS: |
| 22 | + aapt_executable += '.exe' |
| 23 | + base_path = os.path.join(ANDROID_HOME, 'build-tools') |
| 24 | + return File.find(base_path=base_path, file_name=aapt_executable, exact_match=True) |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def __get_pacakge_id(apk_file): |
| 28 | + """ |
| 29 | + Get package id from apk file. |
| 30 | + :param apk_file: Path to apk file. |
| 31 | + :return: Package identifier. |
| 32 | + """ |
| 33 | + app_id = None |
| 34 | + aapt = Adb.__find_aapt() |
| 35 | + command = aapt + ' dump badging ' + apk_file |
| 36 | + output = run(command=command, log_level=CommandLogLevel.COMMAND_ONLY) |
| 37 | + for line in output.split('\n'): |
| 38 | + if 'package:' in line: |
| 39 | + app_id = line.split('\'')[1] |
| 40 | + return app_id |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def run(command, device_id, timeout=60, log_level=CommandLogLevel.COMMAND_ONLY): |
| 44 | + """ |
| 45 | + Run adb command. |
| 46 | + :param command: Command to run (without adb in front). |
| 47 | + :param device_id: Device id. |
| 48 | + :param timeout: Timeout. |
| 49 | + :param log_level: Log level. |
| 50 | + :return: Output of executed command. |
| 51 | + """ |
| 52 | + return run(ADB_PATH + ' -s ' + device_id + ' ' + command, timeout=timeout, log_level=log_level) |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def uninstall_all_apps(device_id): |
| 56 | + """ |
| 57 | + Uninstall all 3rd party applications. |
| 58 | + :param device_id: Device id. |
| 59 | + """ |
| 60 | + apps = Adb.run(command='shell pm list packages -3', device_id=device_id) |
| 61 | + for line in apps: |
| 62 | + if 'package:' in line: |
| 63 | + app = line.replace('package:', '') |
| 64 | + Adb.uninstall(app_id=app, device_id=device_id) |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def install(apk_file, device_id): |
| 68 | + """ |
| 69 | + Install application. |
| 70 | + :param apk_file: Application under test. |
| 71 | + :param device_id: Device id. |
| 72 | + """ |
| 73 | + output = Adb.run(command='install -r ' + apk_file, device_id=device_id) |
| 74 | + assert 'Success' in output, 'Failed to install {0}. \n Log: \n {1}'.format(apk_file, output) |
| 75 | + print '{0} installed successfully on {1}'.format(apk_file, device_id) |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def uninstall(app_id, device_id): |
| 79 | + """ |
| 80 | + Uninstall application. |
| 81 | + :param app_id: Package identifier (for example org.nativescript.testapp). |
| 82 | + :param device_id: Device id. |
| 83 | + """ |
| 84 | + output = Adb.run(command='shell pm uninstall ' + app_id, device_id=device_id) |
| 85 | + assert 'Success' in output, 'Failed to uninstall {0}. \n Log: \n {1}'.format(app_id, output) |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def monkey(apk_file, device_id): |
| 89 | + """ |
| 90 | + Perform monkey testing. |
| 91 | + :param apk_file: Application under test. |
| 92 | + :param device_id: Device id. |
| 93 | + """ |
| 94 | + Adb.__monkey_kill(device_id) |
| 95 | + app_id = Adb.__get_pacakge_id(apk_file) |
| 96 | + print 'Start monkey testing...' |
| 97 | + output = Adb.run(command='shell monkey -p ' + app_id + ' --throttle 100 -v 100 -s 120', device_id=device_id) |
| 98 | + assert 'No activities found' not in output, '{0} is not available on {1}'.format(app_id, device_id) |
| 99 | + assert 'Monkey aborted due to error' not in output, '{0} crashed! \n Log: \n {1}'.format(app_id, output) |
| 100 | + assert 'Monkey finished' in output, 'Unknown error occurred! \n Log: \n {0}'.format(output) |
| 101 | + print 'Monkey test passed!' |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def __monkey_kill(device_id): |
| 105 | + """ |
| 106 | + Kill running adb monkey instances. |
| 107 | + :param device_id: device id. |
| 108 | + """ |
| 109 | + kill_command = "shell ps | awk '/com\.android\.commands\.monkey/ { system(\"adb shell kill \" $2) }'" |
| 110 | + Adb.run(command=kill_command, device_id=device_id, log_level=CommandLogLevel.SILENT) |
0 commit comments