def swipe(dev_id: str, x1: int, y1: int, x2: int, y2: int): """ Method for perform swipe by coordinates """ command = "adb -s {dev_id} shell input swipe {x1} {y1} {x2} {y2}".format(dev_id=dev_id, x1=x1, y1=y1, x2=x2, y2=y2) ADB.exec_adb(command)
def tap(dev_id: str, x: int, y: int): """ Method for perform tap by coordinates """ command = "adb -s {dev_id} shell input tap {x} {y}".format(dev_id=dev_id, x=x, y=y) ADB.exec_adb(command)
def close_package(dev_id: str, package: str): """ Method to close package """ command = "adb -s {dev_id} shell am force-stop {package}".format( dev_id=dev_id, package=package) ADB.exec_adb(command)
def install_app(dev_id, path_apk): """ Method to install package to the device. """ command = "adb -s {dev_id} install {path}".format(dev_id=dev_id, path=path_apk) ADB.exec_adb(command)
def screenshot(dev_id: str, path_save: str): """ Make a screenshot TODO: Test """ command = "adb -s {dev_id} shell screencap {path}".format(dev_id=dev_id, path=path_save) ADB.exec_adb(command)
def send_text(dev_id: str, text: str): """ Method perform typing text NOTE: Before use, make sure input text field in selected """ command = "adb -s {dev_id} shell input text {text}".format( dev_id=dev_id, text=text.strip().replace(" ", "%s")) # %s - Space ADB.exec_adb(command)
def clear_package_cache(dev_id, package): """ Method to clean app cashe :dev_id: device id :package: package name """ command = "adb -s {dev_id} shell pm clear {package}".format( dev_id=dev_id, package=package) ADB.exec_adb(command)
def delete(dev_id: str, path_file: str): """ Method to delete file a device :dev_id: Device ID :path_file: Path to file """ command = "adb -s {dev} shell rm {path}".format(dev=dev_id, path=path_file) ADB.exec_adb(command)
def start_package(dev_id: str, package: str): """ Method to start package TODO: Test """ command = "adb -s {dev_id} shell am startservice {package}".format( dev_id=dev_id, package=package) ADB.exec_adb(command)
def execute_keyevent(dev_id, android_keyevent): """ Method to execute Keyevent :dev_id: Device ID :android_keyevent: Android KeyEvent """ command = "adb -s {dev_id} shell input keyevent {keycode}".format( dev_id=dev_id, keycode=android_keyevent.value.get("key_code")) ADB.exec_adb(command)
def save_meminfo(dev_id: str, path: str, ps=""): """ Method save meminfo into txt files :path: path to save file :ps: By default system, pid or package name TODO: Refactor, add Utility """ filename = "sys" if ps == "" else ps command = "adb -s {dev} shell dumpsys meminfo {ps} > {path}.txt".format( dev=dev_id, ps=ps, path=os.path.join(path, filename)) ADB.exec_adb(command)
def set_screen_brightness(dev_id: str, value): """ Method to change screen brightness on the device in range - 0 to 255 """ if value > 255: value = 255 if value < 0: value = 0 command = "adb -s {dev_id} shell settings put system screen_brightness {value}".format(dev_id=dev_id, value=value) ADB.exec_adb(command)
class TestDeviceInfo(object): _devices = ADB.get_connected_devices() assert len(_devices) > 0, "Check connected Device" device = _devices[0] def test_android_version(self): """ Unit test for Android version Return Example - Android Version 6.0.1 """ android_version = DeviceInfo.get_prop(self.device, Properties.ANDROID_VERSION) print(android_version) assert isinstance(android_version, dict) version = android_version.get("Android Version") assert len(version) > 0 def test_getprop(self): """ Unit test for all getprop should return in dict """ getprop = DeviceInfo.all_getprop(self.device) assert isinstance(getprop, dict) assert len(getprop) > 0
def test_get_connected_devices(self): devices = ADB.get_connected_devices() print("NOTE: Execute test with connected devices") print("E: Found Devices {}".format(devices)) assert True assert len(devices) > 0
def test_get_layout(self): # TODO: Make a generator Files.clear_dir(Path.TEST_FILES.value) dev_id = ADB.get_connected_devices()[0] print("I: Device - {}".format(dev_id)) path_to_file = Layout.save_layout(dev_id, Path.TEST_FILES.value) print("I: Path to file - {}".format(path_to_file)) assert os.path.isfile(path_to_file)
def get_list_packages(dev_id: str) -> list: """ Return a list of installed packages on the device """ command = "adb -s {dev_id} shell pm list packages".format( dev_id=dev_id) raw_packages = ADB.get_terminal_output(command) return [x.strip() for x in raw_packages]
def get_meminfo(dev_id, ps="") -> list: """ Method return dumpsys meminfo memory snapshot, by default will return system memory :dev_id: device id :ps: pid or process name """ command = "adb -s {dev} shell dumpsys meminfo {ps}".format(dev=dev_id, ps=ps) return ADB.get_terminal_output(command)
class TestPackageInfo(object): dev_id = ADB.get_connected_devices()[0] def test_is_package_exist(self): notExist = PackageInfo.is_package_exist(self.dev_id, "com.android.not_a_package") assert notExist is False exist = PackageInfo.is_package_exist(self.dev_id, "com.android.vending") assert exist is True
def pull(dev_id: str, path_from: str, path_to: str): """ Print ADB pull execution into terminal :dev_id: device id :path_from: Path to file on the device :path_to: Path to save a file """ command = "adb -s {dev_id} pull {path_from} {path_to}".format( dev_id=dev_id, path_from=path_from, path_to=path_to) output = ADB.get_terminal_output(command) print("I: {}".format(output))
def get_pid(dev_id: str, ps: str) -> str: """ Method return pid of package of process :dev_id: device id :ps: process or package name TODO: Fix for processes with multiple PIDs """ command = "adb -s {dev} shell ps | grep {ps} | cut -d ' ' -f 4".format(dev=dev_id, ps=ps) print(command) pid = ADB.get_terminal_output(command) return pid[0].strip() if len(pid) > 0 else ""
def _dump_layout(dev_id: str) -> str: """ Method call the uiautomator to dump screen layout and Return path to it :dev_id: device id """ command = "adb -s {dev_id} shell uiautomator dump".format( dev_id=dev_id) raw_path = ADB.get_terminal_output(command)[0].strip() if "UI hierchary dumped to" in raw_path: return raw_path.split(":")[1].strip() else: print("E: {}".format(raw_path))
class TestCookbook(object): dev_id = ADB.get_connected_devices()[0] def test_packages_info(self): """ Unit test to check return for Cookbook.get_packages_info() """ packages = Info.get_packages_info(self.dev_id, AndroidKPackage.YOUTUBE, AndroidKPackage.PLAY_STORE) for package in packages: assert len(package.appName) > 0 assert len(package.version) > 0 assert len(package.package) > 0
def all_getprop(dev_id: str) -> dict: """ Return getprop values in format dict{property:value} :dev_id: Device ID """ command = "adb -s {dev} shell getprop".format(dev=dev_id) raw_getprop = ADB.get_terminal_output(command) dict_getprop = {} for line in raw_getprop: raw = line.strip().replace("[", "").replace("]", "").split(":") dict_getprop[raw[0].strip()] = raw[1].strip() return dict_getprop
def push(dec_id: str, path_file: str, path_to: str): """ Print ADB push execution into terminal :dev_id: device id :path_file: Path to file :path_to: Path to save file in the device TODO: Check File existance before push TODO: Test """ command = "adb -s {dev} push {path_file} {path_to}".format( dev=dec_id, path_file=path_file, path_to=path_to) output = ADB.get_terminal_output(command) print("I: {}".format(output))
def get_package_version(dev_id: str, package: str) -> list: """ Return a package version :dev_id: device id :package: package name """ command = "adb -s {dev_id} shell dumpsys package {package} | grep versionName".format( dev_id=dev_id, package=package) raw_out = ADB.get_terminal_output(command) versions = [] for ver in raw_out: versions.append(ver.strip().split("=")[1]) return versions
def get_current_activity(dev_id: str) -> dict: """ Method return dict{activity, package} from current screen """ command = "adb -s {dev} shell dumpsys window windows | grep -E 'mCurrentFocus'".format(dev=dev_id) output = ADB.get_terminal_output(command)[0] if "/" in output: raw = output.strip().split("/") activity = raw[1][:len(raw[1]) - 1:] package = raw[0].split(" ").pop() return {"activity": activity, "package": package} elif "StatusBar" in output: return {"activity": "StatusBar", "package": "AndroidStatusBar"} else: print("W: {}".format(output))
def is_locked(dev_id) -> bool: """ Return is device locked :dev_id: Device ID """ command = "adb -s {dev} shell dumpsys window | grep 'mShowingLockscreen'".format(dev=dev_id) out = ADB.get_terminal_output(command)[0].split() for line in out: raw = line.strip().split("=") if "mDreamingLockscreen" in raw[0]: if raw[1] == "true": return True if "mShowingLockscreen" in raw[0]: if raw[1] == "true": return True return False
def get_display_size(dev_id) -> dict: """ Return a display size in dict{'width': <str>, 'hight': <str>} :dev_id: Device ID """ size = {} command = "adb -s {dev} shell wm size".format(dev=dev_id) out = ADB.get_terminal_output(command) if len(out) == 1 and "Physical size:" in out[0]: raw = out[0].split("x") print(raw) size["width"] = int(raw[0].split(":")[1].strip()) size["height"] = int(raw[1].strip()) else: print("W: {out}".format(out=out)) return size
def grant_permission(dev_id: str, package: str, *android_permissions): """ Method to grant permissions for package TODO: Unit Test :dev_id: device id :package: package name :permission: permission """ for permission in android_permissions: command = "adb -s {dev} shell pm grant {package} {permission}".format( dev=dev_id, package=package, permission=permission.value.get("perm")) out = ADB.get_terminal_output(command) if len(out) > 0: print(out) print( "I: Prmissions {perm} has been granted to Device ID {dev} for package {package}" .format(perm=permission.value.get("name"), dev=dev_id, package=package))
@staticmethod def is_locked(dev_id) -> bool: """ Return is device locked :dev_id: Device ID """ command = "adb -s {dev} shell dumpsys window | grep 'mShowingLockscreen'".format(dev=dev_id) out = ADB.get_terminal_output(command)[0].split() for line in out: raw = line.strip().split("=") if "mDreamingLockscreen" in raw[0]: if raw[1] == "true": return True if "mShowingLockscreen" in raw[0]: if raw[1] == "true": return True return False if __name__ == "__main__": # # system_process = "com.android.systemui" dev_id = ADB.get_connected_devices()[0] x = DeviceInfo.get_current_activity(dev_id) print(x) # size = DeviceInfo.get_display_size(dev_id) # print(size) # print(DeviceInfo.get_package_activity(dev_id, "com.android.vending")) # DeviceInfo.get_prop(dev_id, Properties.BRAND, Properties.MODEL)