Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed installed version checking #280

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions uiautomator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -289,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:
Expand Down Expand Up @@ -328,6 +331,18 @@ 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])
return versionCode
break
except:
return 0


_init_local_port = LOCAL_PORT - 1
Expand Down Expand Up @@ -402,7 +417,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):
Expand Down Expand Up @@ -467,13 +482,12 @@ 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)

while not self.alive and timeout > 0:
time.sleep(0.1)
timeout -= 0.1
Expand All @@ -485,6 +499,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):
Expand Down Expand Up @@ -513,6 +532,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):
Expand Down Expand Up @@ -861,6 +885,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

Expand Down