How to use app_uninstall method in ATX

Best Python code snippet using ATX

activity_traverse_debug.py

Source:activity_traverse_debug.py Github

copy

Full Screen

...26 print('install ' + apkPath + ' fail.')27 return False28 if packageName in visited:29 print('visited ' + packageName)30 d.app_uninstall(packageName)31 return False32 links = deeplinks_dict.get(packageName, None)33 if links is None:34 print('no this package: ' + packageName)35 d.app_uninstall(packageName)36 return False37 total = len(links)38 success = 039 # sess = d.session(packageName)40 d1_activity, d1_package, d1_launcher = getActivityPackage(d)41 if d1_activity is None:42 print('error in get activity')43 d.app_uninstall(packageName)44 return False45 for index, link in enumerate(links):46 cmd = 'adb -s ' + deviceId + ' shell am start -W -a android.intent.action.VIEW -d ' + link47 try:48 p = subprocess.run(cmd, shell=True, timeout=8)49 except subprocess.TimeoutExpired:50 print('cmd timeout')51 d.app_stop(packageName)52 d.sleep(1)53 continue54 d.sleep(3)55 dialogSolver(d)56 d2_activity, d2_package, d2_launcher = getActivityPackage(d)57 if d1_activity != d2_activity or index == 0:58 success += 159 xml1 = d.dump_hierarchy(compressed=True)60 img1 = safeScreenshot(d)61 xmlScreenSaver_single(save_dir, xml1, img1, d2_activity)62 success_activities.append(d2_activity)63 #crash = full_UI_click_test(sess, xml1, cmd)64 # if len(crash) != 0:65 # crash_position[d2_activity] = crash66 # print(d2_activity, str(crash))67 d.app_stop(packageName)68 d.sleep(1)69 # d.app_uninstall(packageName)70 print('\n\n\n' + packageName + ':' + str(total) + ' ' + str(success) + '\n\n\n')71 return packageName, total, success, success_activities72# return status: 0 success, 1 install fail, 2 visited, 3 no package in deeplink, 4 not conversion, 5 other fail73def unit_traverse_phoTab(apkPath, d1, d2, deviceId1, deviceId2, deeplinks_dict, visited, save_dir, collected_packages):74 intent_error_msg = 'Error: Activity not started'75 crash_position = {}76 success_activities = []77 installed1, packageName, mainActivity = installApk(apkPath, device=deviceId1, reinstall=True)78 if installed1 != 0:79 print('install ' + apkPath + ' fail.')80 return 1, packageName, 0, 0, crash_position81 if packageName in visited:82 print('visited ' + packageName)83 d1.app_uninstall(packageName)84 return 2, packageName, 0, 0, crash_position85 if packageName not in collected_packages:86 print('not conversion ' + packageName)87 d1.app_uninstall(packageName)88 return 4, packageName, 0, 0, crash_position89 else:90 save_dir_package = os.path.join(save_dir, packageName)91 if not os.path.exists(save_dir_package):92 os.mkdir(save_dir_package)93 installed2, packageName, mainActivity = installApk(apkPath, device=deviceId2, reinstall=True)94 if installed2 != 0:95 print('install ' + apkPath + ' fail.')96 d1.app_uninstall(packageName)97 return 1, packageName, 0, 0, crash_position98 links = deeplinks_dict.get(packageName, None)99 if links is None:100 print('no this package: ' + packageName)101 d1.app_uninstall(packageName)102 d2.app_uninstall(packageName)103 return 3, packageName, 0, 0, crash_position104 total = len(links)105 success = 0106 # sess = d.session(packageName)107 d1_activity, d1_package, d1_launcher = getActivityPackage(d1)108 d2_activity, d2_package, d2_launcher = getActivityPackage(d2)109 if d1_activity is None:110 print('error in get activity')111 d1.app_uninstall(packageName)112 return 5, packageName, 0, 0, crash_position113 if d2_activity is None:114 print('error in get activity')115 d2.app_uninstall(packageName)116 return 5, packageName, 0, 0, crash_position117 for index, link in enumerate(links):118 cmd1 = 'adb -s ' + deviceId1 + ' shell am start -W -a android.intent.action.VIEW -d ' + link119 cmd2 = 'adb -s ' + deviceId2 + ' shell am start -W -a android.intent.action.VIEW -d ' + link120 try:121 p1 = subprocess.run(cmd1, shell=True, timeout=8, capture_output=True).stdout122 p2 = subprocess.run(cmd2, shell=True, timeout=8, capture_output=True).stdout123 if intent_error_msg in str(p1) or intent_error_msg in str(p2):124 print('intent fail')125 continue126 except subprocess.TimeoutExpired:127 print('cmd timeout')128 d1.app_stop(packageName)129 d1.sleep(1)130 d2.app_stop(packageName)131 d2.sleep(1)132 continue133 d1.sleep(3)134 d2.sleep(3)135 dialogSolver(d1)136 dialogSolver(d2)137 d11_activity, d11_package, d11_launcher = getActivityPackage(d1)138 d22_activity, d22_package, d22_launcher = getActivityPackage(d2)139 if d1_activity != d11_activity and d2_activity != d22_activity:140 success += 1141 xml1 = d1.dump_hierarchy(compressed=True)142 img1 = safeScreenshot(d1)143 xml2 = d2.dump_hierarchy(compressed=True)144 img2 = safeScreenshot(d2)145 size = img2.size146 if size[0] < size[1]:147 print('no tablet adaption')148 d1.app_stop(packageName)149 d2.app_stop(packageName)150 print('uninstall ' + packageName)151 d1.app_uninstall(packageName)152 d2.app_uninstall(packageName)153 continue154 xmlScreenSaver(save_dir_package, xml1, xml2, img1, img2, d11_activity, d22_activity)155 success_activities.append(d11_activity)156 d1.app_stop(packageName)157 d1.sleep(1)158 d2.app_stop(packageName)159 d2.sleep(1)160 d1.app_uninstall(packageName)161 d2.app_uninstall(packageName)162 print('\n\n\n' + packageName + ':' + str(total) + ' ' + str(success) + ' ' + str(success_activities) + '\n\n\n')163 return 0, packageName, total, success, success_activities164def batch_traverse(apkDir, deviceId, deeplinks_dict, save_dir, log=r'debug_log.txt'):165 total = 0166 success = 0167 index = 0168 visited = []169 if not os.path.exists(save_dir):170 os.mkdir(save_dir)171 with open(log, 'r+', encoding='utf8') as f:172 logs = f.readlines()173 for line in logs:174 line = line.split(' ')175 visited.append(line[0])...

Full Screen

Full Screen

runTool.py

Source:runTool.py Github

copy

Full Screen

...30 installed2, packageName, mainActivity = installApk(apkPath, device=tabletDevice)31 if installed2 != 0:32 print('install ' + apkPath + ' fail.')33 d1.app_stop(packageName)34 d1.app_uninstall(packageName)35 return 136 d2.app_start(packageName, use_monkey=True)37 # wait for opening apps on two devices38 d1.sleep(5)39 d2.sleep(5)40 dialogSolver(d1)41 dialogSolver(d2)42 d1_activity, d1_package, d1_launcher = getActivityPackage(d1)43 d2_activity, d2_package, d2_launcher = getActivityPackage(d2)44 if d1_package != d2_package or d1_activity != d2_activity:45 print('pls wait, load sleep time')46 d1.sleep(loadSleepTime)47 d2.sleep(loadSleepTime)48 xml1 = d1.dump_hierarchy(compressed=True)49 xml2 = d2.dump_hierarchy(compressed=True)50 img1 = safeScreenshot(d1)51 img2 = safeScreenshot(d2)52 size = img2.size53 if size[0] < size[1]:54 print('no tablet adaption')55 d1.app_stop(packageName)56 d2.app_stop(packageName)57 print('uninstall ' + packageName)58 d1.app_uninstall(packageName)59 d2.app_uninstall(packageName)60 return 561 clickBounds1 = hierachySolver(xml1, xml2)62 if clickBounds1 is None or len(clickBounds1) == 0:63 print('no the same texts to click, exit')64 d1.app_stop(packageName)65 d2.app_stop(packageName)66 print('uninstall ' + packageName)67 d1.app_uninstall(packageName)68 d2.app_uninstall(packageName)69 return 270 else:71 subSaveDir = os.path.join(saveDir, packageName)72 if not os.path.exists(subSaveDir):73 os.mkdir(subSaveDir)74 xmlScreenSaver(subSaveDir, xml1, xml2, img1, img2, d1_activity, d2_activity)75 for i in clickBounds1:76 # click the same text in two screens77 bounds1 = i[0]78 bounds2 = i[1]79 print('click: ' + str(i[-1]))80 d1.click((bounds1[0] + bounds1[2]) / 2, (bounds1[1] + bounds1[3]) / 2)81 d2.click((bounds2[0] + bounds2[2]) / 2, (bounds2[1] + bounds2[3]) / 2)82 d1.sleep(3)83 d2.sleep(3)84 xml11 = d1.dump_hierarchy(compressed=True)85 xml22 = d2.dump_hierarchy(compressed=True)86 img11 = safeScreenshot(d1)87 img22 = safeScreenshot(d2)88 xmlScreenSaver(subSaveDir, xml11, xml22, img11, img22, d1_activity, d2_activity)89 # swipe forward to collect more data90 d1.swipe_ext(Direction.FORWARD)91 d2.swipe_ext(Direction.FORWARD)92 xml11 = d1.dump_hierarchy(compressed=True)93 xml22 = d2.dump_hierarchy(compressed=True)94 img11 = safeScreenshot(d1)95 img22 = safeScreenshot(d2)96 xmlScreenSaver(subSaveDir, xml11, xml22, img11, img22, d1_activity, d2_activity)97 d1.swipe_ext(Direction.BACKWARD)98 d2.swipe_ext(Direction.BACKWARD)99 # back to the original page100 d1.press('back')101 d2.press('back')102 print('back...')103 dialogSolver(d1)104 dialogSolver(d2)105 d1.app_start(packageName, use_monkey=True)106 d2.app_start(packageName, use_monkey=True)107 dialogSolver(d1)108 dialogSolver(d2)109 d1.sleep(switchSleepTime)110 d2.sleep(switchSleepTime)111 # swipe forward to collect more data112 d1.swipe_ext(Direction.FORWARD)113 d2.swipe_ext(Direction.FORWARD)114 xmla = d1.dump_hierarchy(compressed=True)115 xmlb = d2.dump_hierarchy(compressed=True)116 imga = safeScreenshot(d1)117 imgb = safeScreenshot(d2)118 xmlScreenSaver(subSaveDir, xmla, xmlb, imga, imgb, d1_activity, d2_activity)119 clickBounds2 = hierachySolver(xmla, xmlb)120 if clickBounds2 is None:121 return 0122 for i in clickBounds2:123 if i in clickBounds1:124 continue125 # click the same text in two screens126 bounds1 = i[0]127 bounds2 = i[1]128 print('click: ' + str(i[-1]))129 d1.click((bounds1[0] + bounds1[2]) / 2, (bounds1[1] + bounds1[3]) / 2)130 d2.click((bounds2[0] + bounds2[2]) / 2, (bounds2[1] + bounds2[3]) / 2)131 d1.sleep(3)132 d2.sleep(3)133 xml11 = d1.dump_hierarchy(compressed=True)134 xml22 = d2.dump_hierarchy(compressed=True)135 img11 = d1.screenshot()136 img22 = d2.screenshot()137 xmlScreenSaver(subSaveDir, xml11, xml22, img11, img22, d1_activity, d2_activity)138 # swipe forward to collect more data139 d1.swipe_ext(Direction.FORWARD)140 d2.swipe_ext(Direction.FORWARD)141 xml11 = d1.dump_hierarchy(compressed=True)142 xml22 = d2.dump_hierarchy(compressed=True)143 img11 = d1.screenshot()144 img22 = d2.screenshot()145 xmlScreenSaver(subSaveDir, xml11, xml22, img11, img22, d1_activity, d2_activity)146 d1.swipe_ext(Direction.BACKWARD)147 d2.swipe_ext(Direction.BACKWARD)148 dialogSolver(d1)149 dialogSolver(d2)150 # back to the original page151 d1.press('back')152 d2.press('back')153 print('back...')154 d1.app_start(packageName, use_monkey=True)155 d2.app_start(packageName, use_monkey=True)156 d1.sleep(3)157 d2.sleep(3)158 # d1.swipe_ext(Direction.BACKWARD)159 # d2.swipe_ext(Direction.BACKWARD)160 d1.app_stop(packageName)161 d2.app_stop(packageName)162 print('uninstall ' + packageName)163 d1.app_uninstall(packageName)164 d2.app_uninstall(packageName)165 return 0166def batchUiExplorer():167 # apksDir = r'/Users/hhuu0025/PycharmProjects/uiautomator2/googleplay'168 apksDir = r'E:\old_downloads'169 saveDir = r'saveData'170 device1Id = 'cb8c90f4'171 device2Id = 'R52RA0C2MFF'172 log = r'log.txt'173 # return value 0 success, 1 install fail, 2 no the same texts, 3 time out174 # read test apks175 lines = open(log, 'r', encoding='utf8').readlines()176 apks = {}177 for i in lines:178 i = i.replace('\n', '')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ATX automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful