From 842104d406a24ad357fa07b8d23f2416ba342afb Mon Sep 17 00:00:00 2001 From: Giancarlo Date: Thu, 9 May 2019 11:18:22 +0200 Subject: [PATCH 1/3] Fixed version checking --- uiautomator/__init__.py | 56 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/uiautomator/__init__.py b/uiautomator/__init__.py index 677b0b7..111952f 100644 --- a/uiautomator/__init__.py +++ b/uiautomator/__init__.py @@ -38,6 +38,8 @@ __author__ = "Xiaocong He" __all__ = ["device", "Device", "rect", "point", "Selector", "JsonRPCError"] +u2_version_code=3 + def U(x): if sys.version_info.major == 2: @@ -109,6 +111,7 @@ def __call__(self, *args, **kwargs): data["params"] = kwargs jsonresult = {"result": ""} if os.name == "nt": +# print 'start post ' + str(data) res = self.pool.urlopen("POST", self.url, headers={"Content-Type": "application/json"}, @@ -328,6 +331,19 @@ def version(self): '''adb version''' match = re.search(r"(\d+)\.(\d+)\.(\d+)", self.raw_cmd("version").communicate()[0].decode("utf-8")) return [match.group(i) for i in range(4)] + + def getVersionCode(self, packageName): + '''adb dumpsys package myPackageName''' + try: + stdout = self.cmd('shell','dumpsys', 'package', packageName).communicate()[0].decode() + for line in stdout.splitlines(): + if line.startswith(" versionCode"): + versionCode = int(line.split('=')[1][0]) + print(f'VersionCode: {versionCode}') + return versionCode + break + except: + return 0 _init_local_port = LOCAL_PORT - 1 @@ -402,7 +418,7 @@ def push(self): def install(self): base_dir = os.path.dirname(__file__) for apk in self.__apk_files: - self.adb.cmd("install", "-r -t", os.path.join(base_dir, apk)).wait() + self.adb.cmd("install", "-r", "-t", os.path.join(base_dir, apk)).wait() @property def jsonrpc(self): @@ -467,13 +483,13 @@ def start(self, timeout=5): ["-c", "com.github.uiautomatorstub.Stub"] )) else: - self.install() + if self.checkVersion(): + self.install() cmd = ["shell", "am", "instrument", "-w", - "com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner"] - + "com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner"] self.uiautomator_process = self.adb.cmd(*cmd) self.adb.forward(self.local_port, self.device_port) - + time.sleep(4) while not self.alive and timeout > 0: time.sleep(0.1) timeout -= 0.1 @@ -485,6 +501,11 @@ def ping(self): return self.__jsonrpc().ping() except: return None + + def checkVersion(self): + ''' check uiautomator apk version ''' + need_upgrade = u2_version_code > self.adb.getVersionCode('com.github.uiautomator') + return need_upgrade @property def alive(self): @@ -513,6 +534,11 @@ def stop(self): self.adb.cmd("shell", "kill", "-9", line.split()[index]).wait() except: pass + try: + self.adb.cmd("shell", "am", "force-stop", 'com.github.uiautomator').wait() + except: + pass + @property def stop_uri(self): @@ -861,6 +887,26 @@ def _wait(action, timeout=1000, package_name=None): def exists(self, **kwargs): '''Check if the specified ui object by kwargs exists.''' return self(**kwargs).exists + + @property + def toast(self): + devive_self = self + + class _Toast(object): + def on(self): + return devive_self.server.jsonrpc.toast('on') + + def off(self): + return devive_self.server.jsonrpc.toast('off') + + def __call__(self, action): + if action == "on": + return self.on() + elif action == "off": + return self.off() + else: + raise AttributeError("Invalid parameter: %s" % action) + return _Toast() Device = AutomatorDevice From 7bb50fa352b9a259eb807d4a1baae7bdcf78dc25 Mon Sep 17 00:00:00 2001 From: Giancarlo Date: Thu, 9 May 2019 11:19:59 +0200 Subject: [PATCH 2/3] --- uiautomator/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/uiautomator/__init__.py b/uiautomator/__init__.py index 111952f..5699b6c 100644 --- a/uiautomator/__init__.py +++ b/uiautomator/__init__.py @@ -489,7 +489,6 @@ def start(self, timeout=5): "com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner"] self.uiautomator_process = self.adb.cmd(*cmd) self.adb.forward(self.local_port, self.device_port) - time.sleep(4) while not self.alive and timeout > 0: time.sleep(0.1) timeout -= 0.1 From 677c108afdd55814d99f59995e646e74c9a2653d Mon Sep 17 00:00:00 2001 From: Giancarlo Date: Fri, 10 May 2019 14:28:15 +0200 Subject: [PATCH 3/3] Fixed stuck shell --- uiautomator/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/uiautomator/__init__.py b/uiautomator/__init__.py index 5699b6c..12cc8e2 100644 --- a/uiautomator/__init__.py +++ b/uiautomator/__init__.py @@ -292,7 +292,7 @@ def raw_cmd(self, *args): cmd_line = [self.adb()] + self.adbHostPortOptions + list(args) if os.name != "nt": cmd_line = [" ".join(cmd_line)] - return subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + return subprocess.Popen(cmd_line, shell=False, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def device_serial(self): if not self.default_serial: @@ -339,7 +339,6 @@ def getVersionCode(self, packageName): for line in stdout.splitlines(): if line.startswith(" versionCode"): versionCode = int(line.split('=')[1][0]) - print(f'VersionCode: {versionCode}') return versionCode break except: