def __init__(self, device, mod, timeout=5000): self.timeout = timeout if isinstance(device, Device): self.device = device else: self.device = connect_device(device) self.logger = createlogger(mod) self.log_path = create_folder() self.config = GetConfigs("common") self.product = Configs("common").get("product", "Info") self.appconfig = AppConfig("appinfo", self.product) self.appconfig.set_section(mod) self.adb = self.device.server.adb self.suc_times = 0 try: self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() if mod == "Email": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i <> 'opentimes': self.test_times += int(self.dicttesttimes[i]) elif mod == "Message": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i == 'opentimes': self.test_times += int(self.dicttesttimes[i]) * 3 else: for test_time in self.dicttesttimes: self.test_times += int(self.dicttesttimes[test_time]) self.logger.info("Trace Total Times " + str(self.test_times)) except: pass
def __init__(self, device,mod,timeout = 5000): self.timeout = timeout if isinstance(device, Device): self.device = device else: self.device = connect_device(device) self.logger = createlogger(mod) self.log_path = create_folder() self.config = GetConfigs("common") self.product = Configs("common").get("product","Info") self.appconfig = AppConfig("appinfo",self.product) self.appconfig.set_section(mod) self.adb = self.device.server.adb self.suc_times = 0 try: self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() if mod == "Email": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i <> 'opentimes': self.test_times += int(self.dicttesttimes[i]) elif mod == "Message": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i == 'opentimes': self.test_times += int(self.dicttesttimes[i])*3 else: for test_time in self.dicttesttimes: self.test_times += int(self.dicttesttimes[test_time]) self.logger.info("Trace Total Times " + str(self.test_times)) except: pass
def createlogger(name): """Create a logger named specified name with the level set in config file. return a logger """ config = GetConfigs("common") lev_key = config.getstr("LOG_FITER","Default","common").upper() lev_dict = {"DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL} logger = logging.getLogger(name) logger.setLevel(lev_dict[lev_key]) ch = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s.%(msecs)03d: [%(levelname)s] [%(name)s] [%(funcName)s] %(message)s', '%y%m%d %H:%M:%S') ch.setFormatter(formatter) logger.addHandler(ch) return logger
def createlogger(name): """Create a logger named specified name with the level set in config file. return a logger """ config = GetConfigs("common") lev_key = config.getstr("LOG_FITER", "Default", "common").upper() lev_dict = { "DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL } logger = logging.getLogger(name) logger.setLevel(lev_dict[lev_key]) ch = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s.%(msecs)03d: [%(levelname)s] [%(name)s] [%(funcName)s] %(message)s', '%y%m%d %H:%M:%S') ch.setFormatter(formatter) logger.addHandler(ch) return logger
class Media(): def __init__(self, device, mod): self.product = Configs("common").get("product","Info") self.device = connect_device(device) self.appconfig = AppConfig("appinfo") self.logger = createlogger(mod) self.camera = Camera(self.device,"media_camera") self.record = Recorder(self.device,"media_recorder") #self.browser = Browser(self.device,"media_browser") self.chrome = Chrome(self.device,"media_chrome") if self.product == "Sprints": self.music = PlayMusic(self.device,"media_music") else: self.music = Music(self.device,"media_music") self.suc_times = 0 self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i.upper() in ('VIDEOTIMES','RECORDER','PHOTOTIMES'): self.test_times +=int(self.dicttesttimes[i]) * 2 self.logger.info('Trace Total Times ' + str(self.test_times)) def record_video(self,times = 1): number = 0 self.logger.debug("Record video 30s "+str(times)+' Times') if self.camera.enter(): try: # self.camera.switch('video') for loop in range(times): self.logger.debug("Taking video") if not self.camera.record_video(30): self.camera.save_fail_img() else: self.suc_times += 1 number +=1 self.logger.info("Trace Success Loop "+ str(loop+1)) self.camera.back_to_camera() except Exception,e: self.camera.save_fail_img() # common.common.log_traceback(traceback.format_exc()) self.camera.back_to_camera() #self.camera.mode('video') self.logger.debug('Record video Test complete') return number
def __init__(self, device, mod): self.product = Configs("common").get("product", "Info") self.device = connect_device(device) self.appconfig = AppConfig("appinfo") self.logger = createlogger(mod) self.camera = Camera(self.device, "media_camera") self.record = Recorder(self.device, "media_recorder") #self.browser = Browser(self.device,"media_browser") self.chrome = Chrome(self.device, "media_chrome") if self.product == "Sprints": self.music = PlayMusic(self.device, "media_music") else: self.music = Music(self.device, "media_music") self.suc_times = 0 self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i.upper() in ('VIDEOTIMES', 'RECORDER', 'PHOTOTIMES'): self.test_times += int(self.dicttesttimes[i]) * 2 self.logger.info('Trace Total Times ' + str(self.test_times))
class Media(): def __init__(self, device, mod): self.product = Configs("common").get("product", "Info") self.device = connect_device(device) self.appconfig = AppConfig("appinfo") self.logger = createlogger(mod) self.camera = Camera(self.device, "media_camera") self.record = Recorder(self.device, "media_recorder") #self.browser = Browser(self.device,"media_browser") self.chrome = Chrome(self.device, "media_chrome") if self.product == "Sprints": self.music = PlayMusic(self.device, "media_music") else: self.music = Music(self.device, "media_music") self.suc_times = 0 self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i.upper() in ('VIDEOTIMES', 'RECORDER', 'PHOTOTIMES'): self.test_times += int(self.dicttesttimes[i]) * 2 self.logger.info('Trace Total Times ' + str(self.test_times)) def record_video(self, times=1): number = 0 self.logger.debug("Record video 30s " + str(times) + ' Times') if self.camera.enter(): try: # self.camera.switch('video') for loop in range(times): self.logger.debug("Taking video") if not self.camera.record_video(30): self.camera.save_fail_img() else: self.suc_times += 1 number += 1 self.logger.info("Trace Success Loop " + str(loop + 1)) self.camera.back_to_camera() except Exception, e: self.camera.save_fail_img() # common.common.log_traceback(traceback.format_exc()) self.camera.back_to_camera() #self.camera.mode('video') self.logger.debug('Record video Test complete') return number
def __init__(self, device, mod): self.product = Configs("common").get("product","Info") self.device = connect_device(device) self.appconfig = AppConfig("appinfo") self.logger = createlogger(mod) self.camera = Camera(self.device,"media_camera") self.record = Recorder(self.device,"media_recorder") #self.browser = Browser(self.device,"media_browser") self.chrome = Chrome(self.device,"media_chrome") if self.product == "Sprints": self.music = PlayMusic(self.device,"media_music") else: self.music = Music(self.device,"media_music") self.suc_times = 0 self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i.upper() in ('VIDEOTIMES','RECORDER','PHOTOTIMES'): self.test_times +=int(self.dicttesttimes[i]) * 2 self.logger.info('Trace Total Times ' + str(self.test_times))
class Common(object): """Provide common functions for all scripts.""" def __init__(self, device, mod, timeout=5000): self.timeout = timeout if isinstance(device, Device): self.device = device else: self.device = connect_device(device) self.logger = createlogger(mod) self.log_path = create_folder() self.config = GetConfigs("common") self.product = Configs("common").get("product", "Info") self.appconfig = AppConfig("appinfo", self.product) self.appconfig.set_section(mod) self.adb = self.device.server.adb self.suc_times = 0 try: self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() if mod == "Email": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i <> 'opentimes': self.test_times += int(self.dicttesttimes[i]) elif mod == "Message": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i == 'opentimes': self.test_times += int(self.dicttesttimes[i]) * 3 else: for test_time in self.dicttesttimes: self.test_times += int(self.dicttesttimes[test_time]) self.logger.info("Trace Total Times " + str(self.test_times)) except: pass def device(self): return self.device def save_fail_img(self, newimg=None): """save fail image to log path. argv: The picture want to save as failed image. """ path = (self.log_path + "\\" + datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + ".png") if newimg is None: self.logger.debug("Take snapshot.") newimg = self.device.screenshot(path) if newimg is None: self.logger.warning("newimg is None.") return False self.logger.error("Fail: %s" % (path)) return True def get_file_num(self, path, format): """get number of file with specified format. """ content = self.adb.shell("ls " + path) num = content.count(format) self.logger.debug("%s file num is %d." % (format, num)) return num def start_activity(self, packet, activity): data = self.device.server.adb.shell("am start -n %s/%s" % (packet, activity)) if data.find("Error") > -1: self.logger.error("Fail: %s/%s" % (packet, activity)) return False return True def start_app(self, name, b_desk=True): '''Call/People/ALL APPS/Messaging/Browser''' self.logger.debug("start app:%s" % (name)) self.device.press.home() if b_desk and self.device(text=name).wait.exists(timeout=2000): self.device(text=name).click() return True elif b_desk and self.device(description=name).exists: self.device(text=name).click() return True elif self.device(description="ALL APPS").exists: self.device(description="ALL APPS").click() self.device().fling.horiz.toBeginning() for loop in range(5): if self.device(description=name).exists: self.device(description=name).click() return True elif self.device(text=name).exists: self.device(text=name).click() return True self.device().fling.horiz.forward() elif self.device(description="Apps").exists: self.device(description="Apps").click() self.device().fling.horiz.toBeginning() for loop in range(5): if self.device(description=name).exists: self.device(description=name).click() return True self.device().fling.horiz.forward() return False def back_to_all_apps(self): """back_to_home. """ for loop in range(4): self.device.press.back() if self.device(text="ALL APPS").wait.exists(timeout=2000): return True elif self.device(text="exit").exists: self.device(text="exit").click() elif self.device(text="Quit").exists: self.device(text="Quit").click() self.device.press.back() def start_all_app(self, num=3): '''Call/People/ALL APPS/Messaging/Browser''' self.logger.debug("start all app") if self.device(description="ALL APPS").exists: self.device(description="ALL APPS").click() elif self.device(description="Apps").exists: self.device(description="Apps").click() self.device().fling.horiz.toBeginning() self.device().fling.horiz.toBeginning() for i in range(num): for j in range( self.device(className="android.widget.TextView").count - 2): if self.device( resourceId= "com.tct.launcher:id/apps_customize_pane_content" ).child(index=0).child(index=i).exists: self.device( resourceId= "com.tct.launcher:id/apps_customize_pane_content" ).child(index=0).child(index=i).child(index=j).click() self.device(text="ALL APPS").wait.gone(timeout=20000) self.back_to_all_apps() self.device().fling.horiz.forward() # for loop in range(5): # if self.device(description=name).exists: # self.device(description=name).click() # return True # self.device().fling.horiz.forward() return False def select_menu_item(self, stritem): self.device.press.menu() self.device.delay(1) self.device(text=stritem).click() self.device.delay(2) def _is_connected(self, type): temp_type = type if type == "ALL": temp_type = "LTE" for i in range(5): if self.adb.get_data_service_state() == temp_type: break self.device.delay(5) else: self.logger.warning("Cannot get %s service." % (type)) self.device.press.back() return False for i in range(5): if self.adb.get_data_connected_status(): return True self.device.delay(5) else: self.logger.warning("Cannot connect %s data." % (type)) self.device.press.back() return False def switch_network(self, type=None): """switch network to specified type. argv: (str)type -- the type of network. """ self.logger.debug("Switch network to %s." % (type)) self.start_activity(self.appconfig("RadioInfo", "package"), self.appconfig("RadioInfo", "activity")) self.device.delay(2) network_type = self.appconfig("RadioInfo", type) print network_type self.device(scrollable=True).scroll.to( text=self.appconfig("RadioInfo", "set")) if self.device(resourceId=self.appconfig.id( "RadioInfo", "id_network")).wait.exists(timeout=2000): self.device(resourceId=self.appconfig.id("RadioInfo", "id_network")).click() self.device(scrollable=True).scroll.to(text=network_type) self.device.delay(1) self.device(text=network_type).click() self._is_connected(type) self.back_to_home() def back_to_home(self): """back_to_home. """ for loop in range(4): self.device.press.back() self.device.delay(1) if self.device(text="Quit").exists: self.device(text="Quit").click() self.device.press.home() def is_playing_video(self): """check if video is playing or not. """ data = self.device.server.adb.shell("dumpsys media.player") if not data: return None if "AudioTrack" in data: self.logger.debug("The video is playing now") return True else: self.logger.debug("The video is not playing.") return False
class Common(object): """Provide common functions for all scripts.""" def __init__(self, device,mod,timeout = 5000): self.timeout = timeout if isinstance(device, Device): self.device = device else: self.device = connect_device(device) self.logger = createlogger(mod) self.log_path = create_folder() self.config = GetConfigs("common") self.product = Configs("common").get("product","Info") self.appconfig = AppConfig("appinfo",self.product) self.appconfig.set_section(mod) self.adb = self.device.server.adb self.suc_times = 0 try: self.mod_cfg = GetConfigs(mod) self.test_times = 0 self.dicttesttimes = self.mod_cfg.get_test_times() if mod == "Email": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i <> 'opentimes': self.test_times += int(self.dicttesttimes[i]) elif mod == "Message": for i in self.dicttesttimes: self.test_times += int(self.dicttesttimes[i]) if i == 'opentimes': self.test_times += int(self.dicttesttimes[i])*3 else: for test_time in self.dicttesttimes: self.test_times += int(self.dicttesttimes[test_time]) self.logger.info("Trace Total Times " + str(self.test_times)) except: pass def device(self): return self.device def save_fail_img(self, newimg = None): """save fail image to log path. argv: The picture want to save as failed image. """ path = (self.log_path + "\\" +datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + ".png") if newimg is None: self.logger.debug("Take snapshot.") newimg = self.device.screenshot(path) if newimg is None: self.logger.warning("newimg is None.") return False self.logger.error("Fail: %s" %(path)) return True def get_file_num(self, path, format): """get number of file with specified format. """ content = self.adb.shell("ls " + path) num = content.count(format) self.logger.debug("%s file num is %d." % (format,num)) return num def start_activity(self,packet,activity): data = self.device.server.adb.shell("am start -n %s/%s"%(packet,activity)) if data.find("Error")>-1: self.logger.error("Fail: %s/%s" %(packet,activity)) return False return True def start_app(self,name,b_desk=True): '''Call/People/ALL APPS/Messaging/Browser''' self.logger.debug("start app:%s" %(name)) self.device.press.home() if b_desk and self.device(text=name).wait.exists(timeout = 2000): self.device(text=name).click() return True elif b_desk and self.device(description=name).exists: self.device(text=name).click() return True elif self.device(description="ALL APPS").exists: self.device(description="ALL APPS").click() self.device().fling.horiz.toBeginning() for loop in range(5): if self.device(description=name).exists: self.device(description=name).click() return True elif self.device(text=name).exists: self.device(text=name).click() return True self.device().fling.horiz.forward() elif self.device(description="Apps").exists: self.device(description="Apps").click() self.device().fling.horiz.toBeginning() for loop in range(5): if self.device(description=name).exists: self.device(description=name).click() return True self.device().fling.horiz.forward() return False def back_to_all_apps(self): """back_to_home. """ for loop in range(4): self.device.press.back() if self.device(text = "ALL APPS").wait.exists(timeout = 2000): return True elif self.device(text = "exit").exists: self.device(text = "exit").click() elif self.device(text = "Quit").exists: self.device(text = "Quit").click() self.device.press.back() def start_all_app(self,num=3): '''Call/People/ALL APPS/Messaging/Browser''' self.logger.debug("start all app") if self.device(description="ALL APPS").exists: self.device(description="ALL APPS").click() elif self.device(description="Apps").exists: self.device(description="Apps").click() self.device().fling.horiz.toBeginning() self.device().fling.horiz.toBeginning() for i in range(num): for j in range(self.device(className="android.widget.TextView").count-2): if self.device(resourceId="com.tct.launcher:id/apps_customize_pane_content").child(index = 0).child(index = i).exists: self.device(resourceId="com.tct.launcher:id/apps_customize_pane_content").child(index = 0).child(index = i).child(index = j).click() self.device(text = "ALL APPS").wait.gone(timeout = 20000) self.back_to_all_apps() self.device().fling.horiz.forward() # for loop in range(5): # if self.device(description=name).exists: # self.device(description=name).click() # return True # self.device().fling.horiz.forward() return False def select_menu_item(self, stritem): self.device.press.menu() self.device.delay(1) self.device(text=stritem).click() self.device.delay(2) def _is_connected(self,type): temp_type = type if type == "ALL": temp_type = "LTE" for i in range(5): if self.adb.get_data_service_state() == temp_type: break self.device.delay(5) else: self.logger.warning("Cannot get %s service." % (type)) self.device.press.back() return False for i in range(5): if self.adb.get_data_connected_status(): return True self.device.delay(5) else: self.logger.warning("Cannot connect %s data." % (type)) self.device.press.back() return False def switch_network(self,type = None): """switch network to specified type. argv: (str)type -- the type of network. """ self.logger.debug("Switch network to %s." % (type)) self.start_activity(self.appconfig("RadioInfo","package"),self.appconfig("RadioInfo","activity")) self.device.delay(2) network_type = self.appconfig("RadioInfo",type) print network_type self.device(scrollable=True).scroll.to(text=self.appconfig("RadioInfo","set")) if self.device(resourceId=self.appconfig.id("RadioInfo","id_network")).wait.exists(timeout = 2000): self.device(resourceId=self.appconfig.id("RadioInfo","id_network")).click() self.device(scrollable=True).scroll.to(text=network_type) self.device.delay(1) self.device(text=network_type).click() self._is_connected(type) self.back_to_home() def back_to_home(self): """back_to_home. """ for loop in range(4): self.device.press.back() self.device.delay(1) if self.device(text = "Quit").exists: self.device(text = "Quit").click() self.device.press.home() def is_playing_video(self): """check if video is playing or not. """ data = self.device.server.adb.shell("dumpsys media.player") if not data: return None if "AudioTrack" in data: self.logger.debug("The video is playing now") return True else: self.logger.debug("The video is not playing.") return False