コード例 #1
0
ファイル: common.py プロジェクト: LiZoRN/flyadb
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
コード例 #2
0
ファイル: common.py プロジェクト: LiZoRN/flyadb
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