1 # -*- coding:utf-8 -*- 2 import sys 3 import os 4 import time 5 import subprocess 6 from uiautomator import device as d 7 from logs import * 8 9 log = get_log("appmanager") 10 class appManager(object): 11 """ 12 define the app manager,it include the install,unstall and clear the cache 13 14 """ 15 def __init__(self,appname,apptitle): 16 self._appname = appname 17 self._apptitle = apptitle 18 19 def checkAppPath(self): 20 log.debug("check the app_list is exist or not first") 21 filepath = sys.path[0] + "\app_list\" 22 filename = self._appname 23 if os.path.isdir(filepath): 24 if os.path.isfile(os.path.join(filepath,filename)): 25 log.debug("The name of %s apk file is exist,you can install it" %self._appname) 26 return True 27 else: 28 log.debug("The name of %s apk file is not exist" %self._appname) 29 return False 30 else: 31 log.debug("The path of app_list is not exist,will create the path automatically and then put the apk file to this path manually") 32 os.makedirs(sys.path[0] + "\app_list\") 33 34 def appInstall(self): 35 log.debug("Start to install %s" %self._appname) 36 filepath = (os.path.join(sys.path[0],"app_list",self._appname)) 37 cmd = "adb install " + filepath 38 log.debug("The %s is installing now,please wait...." %self._apptitle) 39 # os.popen(cmd) 40 installinfo = subprocess.check_output(cmd).split(" ") 41 #check the output value of first one whether is "Success" or not,if yes return True else return false 42 if installinfo[0] == "Success": 43 log.debug("The installation is successfully") 44 return True 45 else: 46 log.debug("The installation is failed") 47 return False 48 49 def appUnistall(self): 50 """ 51 enter the applist from homepage,and then uninstall the app 52 53 """ 54 d.press("home") 55 if d(description = "Apps").wait.exists(timeout = 1000): 56 d(description = "Apps").click() 57 log.debug("Start to uninstall %s" %self._apptitle) 58 if d(resourceId = "com.tct.launcher:id/quick_remove",description = "Remove").exists: 59 d(resourceId="com.tct.launcher:id/quick_remove", description="Remove").click() 60 if d(packageName="com.tct.launcher").scroll.to(text=self._apptitle): 61 d(text=self._apptitle).click() 62 else: 63 return False 64 time.sleep(1) 65 log.debug("click OK to confirm uninstall") 66 if d(packageName = "com.google.android.packageinstaller",text = self._apptitle).exists: 67 d(resourceId="android:id/button1", text="OK").click() 68 time.sleep(3) 69 if d(packageName = "com.tct.launcher").scroll.to(text = self._apptitle): 70 return False 71 else: 72 log.debug("The %s uninstall successfully" %self._apptitle) 73 return True 74 75 def main(): 76 app_lists = "WPS.apk" 77 app_title = "WPS" 78 test = appManager(app_lists, app_title) 79 i = 0 80 while True: 81 i = i +1 82 log.debug("Loop %d times for test" %i) 83 if test.checkAppPath(): 84 if test.appUnistall(): 85 log.debug("The %s is already exists, will uninstall it first") 86 test.appInstall() 87 else: 88 test.appInstall() 89 90 if __name__ == "__main__": 91 main()
输出如下:
1 2018-07-20 09:51:32,438: [appmanager] [App_manager] Loop 29 times for test 2 2018-07-20 09:51:32,448: [appmanager] [App_manager] check the app_list is exist or not first 3 2018-07-20 09:51:32,448: [appmanager] [App_manager] The name of WPS.apk apk file is exist,you can install it 4 2018-07-20 09:51:35,157: [appmanager] [App_manager] Start to uninstall WPS 5 2018-07-20 09:51:38,319: [appmanager] [App_manager] click OK to confirm uninstall 6 2018-07-20 09:51:44,517: [appmanager] [App_manager] The WPS uninstall successfully 7 2018-07-20 09:51:44,533: [appmanager] [App_manager] Start to install WPS.apk 8 2018-07-20 09:51:44,533: [appmanager] [App_manager] The WPS is installing now,please wait....