| 87 | |
| 88 | |
| 89 | class AndroidController: |
| 90 | def __init__(self, device): |
| 91 | self.device = device |
| 92 | self.screenshot_dir = configs["ANDROID_SCREENSHOT_DIR"] |
| 93 | self.xml_dir = configs["ANDROID_XML_DIR"] |
| 94 | self.width, self.height = self.get_device_size() |
| 95 | self.backslash = "\\" |
| 96 | |
| 97 | def get_device_size(self): |
| 98 | adb_command = f"adb -s {self.device} shell wm size" |
| 99 | result = execute_adb(adb_command) |
| 100 | if result != "ERROR": |
| 101 | return map(int, result.split(": ")[1].split("x")) |
| 102 | return 0, 0 |
| 103 | |
| 104 | def get_screenshot(self, prefix, save_dir): |
| 105 | cap_command = f"adb -s {self.device} shell screencap -p " \ |
| 106 | f"{os.path.join(self.screenshot_dir, prefix + '.png').replace(self.backslash, '/')}" |
| 107 | pull_command = f"adb -s {self.device} pull " \ |
| 108 | f"{os.path.join(self.screenshot_dir, prefix + '.png').replace(self.backslash, '/')} " \ |
| 109 | f"{os.path.join(save_dir, prefix + '.png')}" |
| 110 | result = execute_adb(cap_command) |
| 111 | if result != "ERROR": |
| 112 | result = execute_adb(pull_command) |
| 113 | if result != "ERROR": |
| 114 | return os.path.join(save_dir, prefix + ".png") |
| 115 | return result |
| 116 | return result |
| 117 | |
| 118 | def get_xml(self, prefix, save_dir): |
| 119 | dump_command = f"adb -s {self.device} shell uiautomator dump " \ |
| 120 | f"{os.path.join(self.xml_dir, prefix + '.xml').replace(self.backslash, '/')}" |
| 121 | pull_command = f"adb -s {self.device} pull " \ |
| 122 | f"{os.path.join(self.xml_dir, prefix + '.xml').replace(self.backslash, '/')} " \ |
| 123 | f"{os.path.join(save_dir, prefix + '.xml')}" |
| 124 | result = execute_adb(dump_command) |
| 125 | if result != "ERROR": |
| 126 | result = execute_adb(pull_command) |
| 127 | if result != "ERROR": |
| 128 | return os.path.join(save_dir, prefix + ".xml") |
| 129 | return result |
| 130 | return result |
| 131 | |
| 132 | def back(self): |
| 133 | adb_command = f"adb -s {self.device} shell input keyevent KEYCODE_BACK" |
| 134 | ret = execute_adb(adb_command) |
| 135 | return ret |
| 136 | |
| 137 | def tap(self, x, y): |
| 138 | adb_command = f"adb -s {self.device} shell input tap {x} {y}" |
| 139 | ret = execute_adb(adb_command) |
| 140 | return ret |
| 141 | |
| 142 | def text(self, input_str): |
| 143 | input_str = input_str.replace(" ", "%s") |
| 144 | input_str = input_str.replace("'", "") |
| 145 | adb_command = f"adb -s {self.device} shell input text {input_str}" |
| 146 | ret = execute_adb(adb_command) |
no outgoing calls
no test coverage detected