def get_page_info(plan, app, device): if Setting.TimeModel == 'Limit': time_now = datetime.datetime.now() if (time_now - device.beginCrawlTime).seconds > (Setting.LimitTime * 60): Saver.save_crawler_log_both( plan.logPath, device.logPath, "Step : crawl time out , finish crawl.") del time_now return None Saver.save_crawler_log(device.logPath, "get all nodes in this page") page = PageInfo.Page() result = False time = 0 while not result: try: if time > 2: appController.click_back(device) get_uidump_xml_file(device) break get_uidump_xml_file(device) dom = xml.dom.minidom.parse(device.logPath + '/Uidump.xml') result = True except Exception, e: time += 1 print(str(e)) result = False
def crawl_init_nodes(plan, app, device, page_before_run): Saver.save_crawler_log_both(plan.logPath, device.logPath, "Step : run init nodes") device.update_uncrawled_nodes(page_before_run) if page_before_run.currentActivity != app.mainActivity or page_before_run.package != app.packageName: page_now = pageController.get_page_info(plan, app, device) if page_before_run.clickableNodesNum != 0: device.update_crawl_page(page_before_run.nodesInfoList) if page_before_run.clickableNodesNum > 0: page_now = crawl_clickable_nodes(plan, app, device, page_before_run, page_now, True) if page_before_run.longClickableNodesNum > 0: page_now = crawl_longclickable_nodes(plan, app, device, page_before_run, page_now, True) if page_before_run.editTextsNum > 0: page_now = crawl_edittext(plan, app, device, page_before_run, page_now, False) del plan, app, device, page_before_run return page_now else: Saver.save_crawler_log_both(plan.logPath, device.logPath, 'Is in ' + app.mainActivity) del plan, app, device return page_before_run
def get_page_info(plan, app, device): if Setting.TimeModel == 'Limit': time_now = datetime.datetime.now() if (time_now - device.beginCrawlTime).seconds > (Setting.LimitTime * 60): Saver.save_crawler_log_both( plan.logPath, device.logPath, "Step : crawl time out , finish crawl.") del plan, app, device, time_now return None Saver.save_crawler_log(device.logPath, "get all nodes in this page") page = PageInfo.Page() result = False t = 0 while not result: try: if t > 2: Saver.save_crawler_log( device.logPath, "get page error after 3 times , click back .") appController.click_back(device) time.sleep(1) get_uidump_xml_file(device) break get_uidump_xml_file(device) dom = xml.dom.minidom.parse(device.logPath + '/Uidump.xml') result = True except Exception as e: t += 1 print(str(e)) result = False try: root = dom.documentElement nodes = root.getElementsByTagName('node') Saver.save_crawler_log(device.logPath, len(nodes)) info = get_top_activity_info(device) for node in nodes: n = NodeInfo.Node(node) n.update_current_activity(info['activity']) if n.resource_id in app.firstClickViews: device.save_screen(n, False) appController.tap_node(device, n) page = get_page_info(plan, app, device) page.add_node(device, app, n) del node, n page = appController.close_sys_alert(plan, app, device, page) del result, dom, root, nodes, info, plan, app, device, t return page except Exception as e: print(str(e)) del plan, app, device, t return page
def run_test(plan, app, device): Saver.save_crawler_log_both(plan.logPath, device.logPath, "Step : run test ") device.update_crawl_statue("Running") # init device appController.clean_device_logcat(device) # uninstall & install apk if Setting.UnInstallApk: appController.uninstall_app(device, app.packageName) appController.uninstall_app(device, app.testPackageName) if Setting.InstallApk: appController.install_app(device, app.apkPath) appController.install_app(device, app.testApkPath) # init app init_application(plan, app, device) # begin crawl if Setting.CrawlModel == 'Normal' or Setting.CrawlModel == 'Random': Saver.save_crawler_log_both(plan.logPath, device.logPath, "Step : begin to crawl main nodes") appController.start_activity(device, app.packageName, app.mainActivity) time.sleep(5) page = pageController.get_page_info(plan, app, device) device.update_begin_crawl_time() crawl_main_nodes(plan, app, device, page) del page crawl_activities(plan, app, device) device.endCrawlTime = datetime.datetime.now() Saver.save_logcat(plan, device) # clean unusable files pageController.remove_uidump_xml_file(device) # update & save result Saver.save_crawler_log_both( plan.logPath, device.logPath, "Step : " + device.id + " has Crawled " + str(len(device.hasCrawledNodes)) + " nodes.") Saver.save_crawler_log_both( plan.logPath, device.logPath, "Step : " + device.id + " there are " + str(len(device.unCrawledNodes)) + " unCrawled nodes .") Saver.save_crawler_log_both( plan.logPath, device.logPath, "Step : " + device.id + " has Crawled " + str(len(device.hasCrawledActivities)) + " activities .") if device.crawlStatue == 'Running': device.update_crawl_statue('Passed') if device.crawlStatue == "Passed": plan.passedDevice += 1 else: plan.failedDevice += 1
def init_application(plan, app, device): Saver.save_crawler_log_both(plan.logPath, device.logPath, "Step : init application") if Setting.RunInitNodes: appController.start_activity(device, app.packageName, app.launcherActivity) while True: launcherPage = pageController.get_page_info(plan, app, device) if launcherPage.clickableNodesNum == 0: Saver.save_crawler_log_both(plan.logPath, device.logPath, 'scroll to left') appController.drag_screen_to_left(device) else: Saver.save_crawler_log_both(plan.logPath, device.logPath, 'stop scroll') break Saver.save_crawler_log_both(plan.logPath, device.logPath, 'Step : init nodes run begin') crawl_init_nodes(plan, app, device, launcherPage) del launcherPage if Setting.RunInitCase: run_init_cases(plan, app, device) # when go in mainActivity, will add the nodes in MainActivity to device.unCrawledNodes # if crawl main Nodes , after start mainActivity, these nodes can't be added to the page, will get unCrawlable page device.unCrawledNodes = [] del plan, app, device
def check_activity_after_operation(plan, app, device, crawl_activity, page_before_run, node): Saver.save_crawler_log(device.logPath, "Step : Check page after operation") # if app crashed after crawl , save log & start app ,comtinue if Setting.TimeModel == 'Limit': time_now = datetime.datetime.now() if (time_now - device.beginCrawlTime).seconds > (Setting.LimitTime * 60): Saver.save_crawler_log_both(plan.logPath, device.logPath, "Step : crawl time out , finish crawl.") del plan, app, device, crawl_activity, time_now return None del time_now while True: info = get_top_activity_info(device) package = info['packagename'] activity = info['activity'] if len(package) != 0: break times = 0 while activity != crawl_activity: if not appController.app_is_running(device, app): Saver.save_error_logcat(plan, device) appController.clean_device_logcat(device) HtmlMaker.make_failed_result_html(plan, app) MailSender.send_failed_mail_first(plan, app, device) if not re_crawl_mack_error_node(plan, app, device, page_before_run, node, crawl_activity) and Setting.KeepRun: appController.kill_app(app) appController.start_activity(device, app.packageName, crawl_activity) else: Saver.save_crawler_log_both(plan.logPath, device.logPath, "Step : crawl app " + device.crawlStatue + ', break crawling..') return None Saver.save_crawler_log(device.logPath, 'back to ' + crawl_activity) device.save_screen_jump_out(package, activity) appController.click_back(device) time.sleep(2) times += 1 top_activity_info = get_top_activity_info(device) package = top_activity_info['packagename'] activity = top_activity_info['activity'] if times > 3: Saver.save_crawler_log(device.logPath, "can't back to " + crawl_activity + " after click back 3 times , Restart app") appController.start_activity(device, app.packageName, crawl_activity) top_activity_info = get_top_activity_info(device) top_app_package = top_activity_info['packagename'] if top_app_package == app.packageName: del package, top_activity_info, top_app_package break del plan, app, device, crawl_activity, activity return get_page_info(plan, app, device)
def run_init_cases(plan, app, device): Saver.save_crawler_log_both(plan.logPath, device.logPath, "Step : run init cases") for case in app.initCasesList: command = 'adb -s ' + device.id + ' shell am instrument -w -e class ' + case + ' ' + app.testPackageName + '/'\ + app.testRunner Saver.save_crawler_log_both(plan.logPath, device.logPath, command) os.system(command) del case, command del plan, app, device Saver.save_crawler_log_both(plan.logPath, device.logPath, "Run novice guide finish ...")
def check_page_after_operation(plan, app, device, page_before_run, node): Saver.save_crawler_log(device.logPath, "Step : Check page after operation") # if app crashed after crawl , save log & start app ,continue if Setting.TimeModel == 'Limit': time_now = datetime.datetime.now() if (time_now - device.beginCrawlTime).seconds > (Setting.LimitTime * 60): Saver.save_crawler_log_both( plan.logPath, device.logPath, "Step : crawl time out , finish crawl.") del plan, app, device, time_now return None while True: info = get_top_activity_info(device) package = info['packagename'] activity = info['activity'] if len(package) != 0: del info break del info times = 0 while package != app.packageName: if not appController.app_is_running(device, app): Saver.save_error_logcat(plan, device) appController.clean_device_logcat(device) HtmlMaker.make_failed_result_html(plan, app) MailSender.send_failed_mail_first(plan, app, device) if not re_crawl_mack_error_node( plan, app, device, page_before_run, node, app.launcherActivity) and Setting.KeepRun: appController.kill_app(app) appController.start_activity(device, app.packageName, app.launcherActivity) else: Saver.save_crawler_log_both( plan.logPath, device.logPath, "Step : crawl app " + device.crawlStatue + ', break crawling..') del plan, app, device, package, activity return None Saver.save_crawler_log(device.logPath, 'back to ' + app.packageName) device.save_screen_jump_out(package, activity) appController.click_back(device) times += 1 top_activity_info = get_top_activity_info(device) package = top_activity_info['packagename'] activity = top_activity_info['activity'] if times > 3: Saver.save_crawler_log( device.logPath, "can't back to " + app.packageName + " after click back 3 times , Restart app") appController.start_activity(device, app.packageName, app.launcherActivity) top_activity_info = get_top_activity_info(device) top_app_package = top_activity_info['packagename'] if top_app_package == app.packageName: del top_activity_info, times, top_app_package break # if keyboard shown , click device back btn to close keyboard if appController.keyboard_is_shown(device): appController.click_back(device) if activity == 'com.mob.tools.MobUIShell': Saver.save_crawler_log(device.logPath, "close login web QQ/Weibo") appController.click_back(device) page = get_page_info(plan, app, device) if page is not None and page.currentActivity == app.loginActivity and Setting.Login: app.update_loginactivity_entry(node) page = login_by_account(plan, page, app, device) del plan, app, device, page_before_run, node return page