def click_ui_button_by_resourceIdMatches(self, key_code, not_id=False, timeout=10, package=''): icount = 0 kill_adb_uiautomator_block_old() self.logout(self.my_func_name(), "%s click..." % key_code) while icount < timeout: unclock_android_o_screen() if package == "" and not app_exception_kill_case(self.app_package): self.logout(self.my_func_name(), "%s test is failed..." % self.app_package) return False if not_id: app_dev = self.device(resourceIdMatches="%s" % key_code) else: app_dev = self.device(resourceIdMatches=".+/%s$" % key_code) if app_dev.exists: app_dev.click() icount = 0 break time.sleep(1) icount += 1 kill_adb_uiautomator_block_old() if icount > 0: return False else: return True
def click_ui_button_by_text(self, key_code="", textcont="", index_nu="", timeout=10): icount = 0 kill_adb_uiautomator_block_old() while icount < timeout: unclock_android_o_screen() if not app_exception_kill_case(self.app_package): self.logout(self.my_func_name(), "%s test is failed..." % self.app_package) return False if not "" == index_nu: if not "" == key_code: app_dev = self.device(index=index_nu, text=key_code) if not "" == textcont: app_dev = self.device(index=index_nu, textContains=textcont) else: if not "" == key_code: app_dev = self.device(text=key_code) if not "" == textcont: app_dev = self.device(textContains=textcont) if app_dev.exists: app_dev.click() icount = 0 break time.sleep(1) icount += 1 kill_adb_uiautomator_block_old() if icount > 0: return False else: return True
def get_score_info_data(self, key_code='', info_code='', index="", timeout=10): icount = 0 score = "" kill_adb_uiautomator_block_old() while icount < timeout: unclock_android_o_screen() if len(index) == 0: app_dev = self.device(resourceIdMatches=".+/%s$" % key_code) else: app_dev = self.device(resourceIdMatches=".+/%s$" % key_code, index=index) if app_dev.exists: try: score = app_dev.info[info_code] except Exception: self.logout(self.my_func_name(), "get score is failed") break time.sleep(1) icount += 1 kill_adb_uiautomator_block_old() return score
def wait_for_complete(self, complete_text, timeout=600, splittime=5, package=''): i = 0 failed_time = 0 if not splittime == 5: time.sleep(splittime) kill_adb_uiautomator_block_old() while i < timeout: unclock_android_o_screen() kill_adb_uiautomator_block() if package == '' and not utiliy.is_app_runing(self.app_package): if failed_time > 2: return False failed_time += 1 continue app_dev = self.device(resourceIdMatches=".+/%s$" % complete_text) if app_dev: if app_dev.exists: self.logout(self.my_func_name(), "%s test finshed..." % self.app_name) return True else: i += 5 time.sleep(5) self.logout(self.my_func_name(), "\r>>>Please wait for test complete. %s (s)" % i) failed_time = 0 else: self.logout(self.my_func_name(), "Timeout for %s test" % self.app_package) return False
def click_ui_button_by_text_or_resid(self, key_code='', resid='', timeout=10): icount = 0 kill_adb_uiautomator_block_old() while icount < timeout: unclock_android_o_screen() if not app_exception_kill_case(self.app_package): self.logout(self.my_func_name(), "%s test is failed..." % self.app_package) return False if resid == "": app_dev = self.device(text=key_code) else: app_dev = self.device(resourceId=resid) if app_dev.exists: app_dev.click() icount = 0 break time.sleep(1) icount += 1 kill_adb_uiautomator_block_old() if icount > 0: return False else: return True
def get_index_from_list(app_dev, index_count, key_value): index_list = [] for nu in range(index_count): kill_adb_uiautomator_block_old() try: string = app_dev.child(index=nu).child( resourceIdMatches='.+/widget_text1').info['text'] if string == key_value: index_list.append(nu) except Exception, e: log_info.logger.error("get index errors: %s" % e)
def screen_on(log_mes): time.sleep(5) kill_adb_uiautomator_block() if device.screen == "off": log_mes.info("unlock screen...") kill_adb_uiautomator_block_old() device.wakeup() #os.system("adb shell input keyevent 82") time.sleep(1) else: log_mes.info("Screen On.")
def screen_on(): if build_version and build_version[0] == '10': os.system("adb shell svc power stayon true") else: time.sleep(5) kill_adb_uiautomator_block() if device.screen == "off": logger.info("unlock screen...") kill_adb_uiautomator_block_old() device.wakeup() time.sleep(1) else: logger.info("Screen On.")
def turn_airplane(state, log_mes): if state == get_state('airplane_mode_on', log_mes): log_mes.info('airplane already turned ' + state) return True for i in range(4): os.system( 'adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS') kill_adb_uiautomator_block_old() device(text="Airplane mode").click.wait() time.sleep(1) os.system('adb shell am force-stop com.android.settings') if state == get_state('airplane_mode_on', log_mes): return True return False
def wait_for_complete(self, complete_text, timeout=1800, splittime=5, ABS=30, skipstep=30, package=''): i = 0 failed_time = 0 if self.watt_operate.watt_flags: self.watt_operate.start_watt() if not splittime == 5: time.sleep(splittime) kill_adb_uiautomator_block_old() while i < timeout: unclock_android_o_screen() kill_adb_uiautomator_block() if package == '' and not app_exception_kill_case(self.app_package): if failed_time > 2: if self.watt_operate.watt_flags: self.watt_operate.stop_watt() return False failed_time += 1 continue app_dev = self.device(resourceIdMatches=".+/%s$" % complete_text) if app_dev: if app_dev.exists: print self.logout(self.my_func_name(), "%s test finshed..." % self.app_name) if self.watt_operate.watt_flags: self.watt_operate.stop_watt() try: self.watt_operate.watt_result_treat(ABS) except Exception, e: self.logout(self.my_func_name(), "watt_operate error %s." % e) return False return True else: i += skipstep time.sleep(skipstep) self.logout(self.my_func_name(), "\r>>>Please wait for test complete. %s (s)" % i) failed_time = 0
def turn_airplane(state): if state == get_state('airplane_mode_on'): logger.info('airplane already turned ' + state) return True for i in range(4): os.system( 'adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS') kill_adb_uiautomator_block_old() if device(text='ACCEPT').exists: logger.info('airplane security tips find') device(text='ACCEPT').click.wait(timeout=20) kill_adb_uiautomator_block_old() device(text="Airplane mode").click.wait() time.sleep(1) os.system('adb shell am force-stop com.android.settings') if state == get_state('airplane_mode_on'): return True return False
def turn_gps(state, log_mes): if state == get_gps_state(log_mes): log_mes.info('gps already turned ' + state) return True for i in range(4): kill_adb_uiautomator_block_old() if state == '0': os.system( 'adb shell am start -a android.settings.LOCATION_SOURCE_SETTINGS' ) if device(resourceIdMatches=".+/switch_widget$", text="ON").exists: kill_adb_uiautomator_block_old() device(resourceIdMatches=".+/switch_widget$", text="ON").click.wait(timeout=20) else: os.system( 'adb shell am start -a android.settings.LOCATION_SOURCE_SETTINGS' ) if device(resourceIdMatches=".+/switch_widget$", text="OFF").exists: kill_adb_uiautomator_block_old() device(resourceIdMatches=".+/switch_widget$", text="OFF").click.wait(timeout=20) os.system('adb shell am force-stop com.android.settings') if state == get_gps_state(log_mes): return True return False
def wait_for_root_page(self, root_text="", decription_text="", timeout=300): failed_time = 0 self.logout(self.my_func_name(), "Wait for %s root page" % self.app_name) #self.logout(self.my_func_name(),"Wait %s ------"%root_text) for i in range(timeout): unclock_android_o_screen() if i % 5 == 0: kill_adb_uiautomator_block_old() kill_uiautomator() if decription_text == "": if self.device(resourceIdMatches=".+/%s$" % root_text).exists: print break else: if self.device(description="%s" % decription_text).exists: print break i += 1 time.sleep(1) if not app_exception_kill_case(self.app_package): if failed_time > 6: self.logout(self.my_func_name(), "%s test is failed..." % self.app_package) return False failed_time += 1 continue sys.stdout.write("\r>>>Please wait for %s root page. %s (s)" % (self.app_name, i)) sys.stdout.flush() failed_time = 0 if i >= timeout: return False else: return True
def set_display_time(log_mes, displaytime=1800000): """ displaytime = 15000 | 30000 | 60000 | 120000| 300000 | 600000 | 1800000 displaytime default is 1800000 """ kill_adb_uiautomator_block_old() if int(get_screen_off_time(log_mes)) == displaytime: if int(displaytime) >= 60000: log_mes.info('screen off time is already %s mins' % (displaytime / 60000)) else: log_mes.info('screen off time is already %s secs' % (displaytime / 1000)) else: os.system('adb shell am start -a android.settings.DISPLAY_SETTINGS') device(text="Sleep").click() kill_adb_uiautomator_block_old() if int(displaytime) >= 60000: device(text="%s minutes" % (displaytime / 60000)).click() else: device(text="%s seconds" % (displaytime / 1000)).click() time.sleep(1) os.system("adb shell am force-stop com.android.settings")
def turn_bluetooth(state): if build_version and build_version[0] == '10': if state == get_state('wifi_on'): logger.info('wifi already turned ' + state) return True else: os.system("adb shell svc wifi enable") else: if state == get_state('bluetooth_on'): logger.info('bluetooth already turned ' + state) return True for i in range(4): if state == '0': os.system( 'adb shell am start -a android.settings.BLUETOOTH_SETTINGS' ) kill_adb_uiautomator_block_old() time.sleep(1) if device(resourceIdMatches=".+/switch_widget$", text="OFF").exists: logger.info('Bluetooth current state is : 0') os.system('adb shell am force-stop com.android.settings') return True else: kill_adb_uiautomator_block_old() device(resourceIdMatches=".+/switch_widget$", text="ON").click.wait(timeout=20) else: kill_adb_uiautomator_block_old() os.system( 'adb shell am start -a android.settings.BLUETOOTH_SETTINGS' ) if device(resourceIdMatches=".+/switch_widget$", text="ON").exists: logger.info('Bluetooth current state is : 1') os.system('adb shell am force-stop com.android.settings') return True else: kill_adb_uiautomator_block_old() device(resourceIdMatches=".+/switch_widget$", text="OFF").click.wait(timeout=20) os.system('adb shell am force-stop com.android.settings') if state == get_state('bluetooth_on'): return True return False
def click_ui_button_by_resourceIdMatches_to_right_resourceIdMatches( self, test_text, key_code, timeout=10): icount = 0 kill_adb_uiautomator_block_old() while icount < timeout: unclock_android_o_screen() if not app_exception_kill_case(self.app_package): self.logout(self.my_func_name(), "%s test is failed..." % self.app_package) return False app_dev = self.device(resourceIdMatches=".+/%s$" % test_text) if app_dev.exists: right_bt = app_dev.right(resourceIdMatches=".+/%s$" % key_code) if right_bt.exists: right_bt.click() icount = 0 break time.sleep(1) icount += 1 kill_adb_uiautomator_block_old() if icount > 0: return False else: return True
def click_ui_button_by_description(self, key_code='', descriptioncon='', index_nu='', timeout=10): icount = 0 kill_adb_uiautomator_block_old() self.logout(self.my_func_name(), "%s click..." % key_code) while icount < timeout: unclock_android_o_screen() if not utiliy.is_app_runing(self.app_package): self.logout(self.my_func_name(), "%s test is failed..." % self.app_package) return False if not "" == index_nu: if not "" == key_code: app_dev = self.device(index=index_nu, description=key_code) if not "" == descriptioncon: app_dev = self.device(index=index_nu, descriptionContains=descriptioncon) else: if not "" == key_code: app_dev = self.device(description=key_code) if not "" == descriptioncon: app_dev = self.device(descriptionContains=descriptioncon) if app_dev.exists: app_dev.click() icount = 0 break time.sleep(1) icount += 1 kill_adb_uiautomator_block_old() if icount > 0: return False else: return True
def change_swith_status(dev, log_mes): kill_adb_uiautomator_block_old() app_dev = dev(resourceIdMatches='.+/list') kill_adb_uiautomator_block_old() if app_dev.exists: index_nu = app_dev.info['childCount'] log_mes.info(index_nu) iter_list = get_index_from_list(app_dev, index_nu, 'Yes') log_mes.info(iter_list) if len(iter_list) > 0: for item in iter_list: kill_adb_uiautomator_block_old() app_dev.child(index=int(item)).child( resourceIdMatches='.+/widget_text1').click() time.sleep(2) if dev(resourceIdMatches=".+/switchWidget$", text="ON").exists: kill_adb_uiautomator_block_old() dev(resourceIdMatches=".+/switchWidget$", text="ON").click.wait(timeout=20) os.system('adb shell input keyevent BACK') time.sleep(1) os.system("adb shell input keyevent HOME")
def turn_wifi(state, log_mes): if state == get_state('wifi_on', log_mes): log_mes.info('wifi already turned ' + state) return True for i in range(4): if state == '0': os.system('adb shell am start -a android.settings.WIFI_SETTINGS') kill_adb_uiautomator_block_old() time.sleep(1) if device(resourceIdMatches=".+/switch_widget$", text="OFF").exists: log_mes.info('Wi-Fi current state is : 0') os.system('adb shell am force-stop com.android.settings') return True else: kill_adb_uiautomator_block_old() device(resourceIdMatches=".+/switch_widget$", text="ON").click.wait(timeout=20) else: os.system('adb shell am start -a android.settings.WIFI_SETTINGS') time.sleep(2) kill_adb_uiautomator_block_old() if device(resourceIdMatches=".+/switch_widget$", text="ON").exists: log_mes.info('Wi-Fi current state is : 1') os.system('adb shell am force-stop com.android.settings') return True else: kill_adb_uiautomator_block_old() device(resourceIdMatches=".+/switch_widget$", text="OFF").click.wait(timeout=20) os.system('adb shell am force-stop com.android.settings') if state == get_state('wifi_on', log_mes): return True return False
def clear_recent(self): kill_adb_uiautomator_block_old() y = int(self.device.info['displaySizeDpY']) kill_adb_uiautomator_block_old() cy = y / 2 x = int(self.device.info['displaySizeDpX']) kill_adb_uiautomator_block_old() cx = x / 2 self.click_by_event("HOME") time.sleep(2) # kill_adb_uiautomator_block() self.click_by_event("KEYCODE_APP_SWITCH") time.sleep(5) kill_adb_uiautomator_block_old() if not self.device.info['currentPackageName'] == "com.android.systemui": self.logout(self.my_func_name(), "recent key click is failed...") return False icount = 0 if not self.device(text="Your recent screens appear here").exists: kill_adb_uiautomator_block_old() while not self.device(resourceIdMatches=".+/dismiss_task$").exists: if not self.device.info[ 'currentPackageName'] == "com.android.systemui": self.logout(self.my_func_name(), "recent key click is failed...") return False if icount > 50: return False icount += 1 self.device.swipe(cx, cy, cx, y, steps=5) self.device(resourceIdMatches=".+/dismiss_task$").click() time.sleep(1) kill_adb_uiautomator_block_old() kill_adb_uiautomator_block_old() self.device(resourceIdMatches=".+/dismiss_task$").click() time.sleep(2) self.click_by_event("BACK")