예제 #1
0
 def __windows(self):
     # todo windows未完成
     for line in U.cmd("netstat -aon|findstr 4700").stdout.readlines():
         pid = line.strip().split(' ')[-1]
         process_name = U.cmd(
             'tasklist|findstr {}'.format(pid)).stdout.read().split(' ')[0]
         U.cmd('taskkill /f /t /im {}'.format(process_name))
예제 #2
0
def clean_appium(port,device):
    #for line in U.cmd('netstat -aon | findstr %d' % port).stdout.readlines():
    line = U.cmd('netstat -aon | findstr %d' % port).stdout.readline()
    pid = line.strip().split(' ')[-1]
    U.cmd('taskkill /f /pid {}'.format(pid))
    L.Logging.success("killed appium %s" % port)
    clean_logcat(device)
    reconnect_device(device)
예제 #3
0
    def __set_pkg_info(self):
        # 获取文件名
        self.apk_name = os.path.basename(self.apk_path)
        aaptpath = os.path.join(os.path.abspath(os.path.join(os.getcwd())),
                                aapt)

        # 获取包名
        cmd = '{} dump badging "{}" | {} package'.format(
            aaptpath, self.apk_path, find_util)
        process = U.cmd(cmd)
        stdout, stderr = process.communicate()
        if stdout is None:
            U.Logging.error("[pkg_info] time out: {}".format(cmd))
        elif "ERROR" in stderr or "error" in stderr:
            U.Logging.error("[pkg_info] cannot execute: {}".format(cmd))
            U.Logging.error("[pkg_info] result: {}".format(stderr))
        else:
            try:
                package_name = re.findall(r"name='([a-zA-Z0-9.*]+)'", stdout)
                self.name = package_name[0]
                self.version_code = re.findall(r"versionCode='([0-9]+)'",
                                               stdout)[0]
            except Exception as e:
                U.Logging.error(
                    "[pkg_info] failed to regex package name from {}. {}".
                    format(stdout, e))
        # 获取启动Activity
        cmd = '{} dump badging "{}" | {} launchable-activity'.format(
            aaptpath, self.apk_path, find_util)
        process = U.cmd(cmd)
        stdout, stderr = process.communicate()
        if stdout is None:
            U.Logging.error("[pkg_info] time out: {}".format(cmd))
        elif "ERROR" in stderr or "error" in stderr:
            U.Logging.error("[pkg_info] cannot execute: {}".format(cmd))
            U.Logging.error("[pkg_info] result: {}".format(stderr))
        else:
            try:
                activity_list = re.findall(r"name='(.+?)'", stdout)
                main_activity = ""
                for activity in activity_list:
                    if not activity.startswith(
                            "com.squareup") and not activity.startswith(
                                "com.github"):
                        main_activity = activity
                        break
                self.activity = main_activity
            except Exception as e:
                U.Logging.error(
                    "[pkg_info] failed to regex main activity from {}. {}".
                    format(stdout, e))

        if self.name and self.activity:
            return True

        return False
예제 #4
0
 def __darwin(self, port, device):
     # for line in U.cmd(
     #     "lsof -i tcp:%s | grep node|awk '{print $2}'" %
     #         str(port)).stdout.readlines():
     #     U.cmd('kill -9 %s' % line.strip())
     #     U.Logging.debug('CleanProcess:Darwin:kill appium')
     for line in U.cmd(
             "ps -A | grep logcat | grep %s" % device).stdout.readlines():
         U.cmd('kill -9 %s' % line.strip())
         U.Logging.debug('CleanProcess:Darwin:kill logcat')
예제 #5
0
 def __linux(self, port, device):
     # linux必须最高权限才可获取到端口
     # for line in U.cmd(
     #     "lsof -i:%s |awk '{print $2}'" %
     #         str(port)).stdout.readlines():
     #     U.cmd('kill -9 %s' % line.strip())
     #     U.Logging.debug('CleanProcess:linux:kill appium')
     for line in U.cmd(
         "ps -ef | grep logcat | grep %s|awk '{print $2}'" %
             device).stdout.readlines():
         U.cmd('kill -9 %s' % line.strip())
         U.Logging.debug('CleanProcess:linux:kill logcat')
예제 #6
0
    def start_appium(self):
        """
        启动appium
        p:appium port
        bp:bootstrap port
        :return: 返回appium端口参数
        """

        aport = random.randint(4700, 4900)
        bpport = random.randint(4700, 4900)
        U.cmd("appium -p %s -bp %s -U %s" %
              (aport, bpport, self.device))  # 启动appium
        U.Logging.debug('start appium :p %s bp %s device:%s' %
                        (aport, bpport, self.device))
        U.sleep(10)
        return aport
예제 #7
0
def get_device():
    android_devices_list = []
    for device in U.cmd('adb devices').stdout.readlines():
        if 'device' in device and 'devices' not in device:
            device = device.split('\t')[0]
            android_devices_list.append(device)

    return android_devices_list
예제 #8
0
def check_environment():
    appium = U.cmd("appium -v").stdout.readline().strip()
    if '1.' not in appium:
        U.Logging.error('appium not in computer')
        exit()
    else:
        U.Logging.info('appium version {}'.format(appium))
    if not public.GetDevice.get_device():
        U.Logging.error('the computer is not connected to any devices')
        exit()
예제 #9
0
def get_device_name():
    """

    :return: 返回一个包含正在连接的设备名称
    """
    device_list = []
    for device in U.cmd('adb devices').stdout.readlines():
        if 'device' in device and 'devices' not in device:
            device = device.split('\t')[0]
            device_list.append(device)
    return device_list
예제 #10
0
def get_android_version(device):
    """

    :param device: devicename
    :return: 返回该设备的安卓版本号
    """
    for version in U.cmd('adb -s %s shell getprop' %
                         device).stdout.readlines():
        if 'ro.build.version.release' in version:
            version = version.split(':')[1].strip().strip('[]')
            L.Logging.info('get device: %s, version: %s' % (device, version))
            return version
예제 #11
0
    def __initialization_arrangement(self):
        l.ProjectLog.set_up()
        self.dl.init()
        al = l.Al(self.serial)
        al.main(self.dl.log_path)

        U.Logging.info("push the monkey jars to %s" % self.serial)
        cmd = 'adb -s %s push conf/lib/framework.jar /sdcard/' % self.serial
        U.cmd(cmd)
        cmd = 'adb  -s %s push conf/lib/monkey.jar /sdcard/' % self.serial
        U.cmd(cmd)
        cmd = 'adb  -s %s push conf/lib/max.config /sdcard/' % self.serial
        U.cmd(cmd)
        cmd = 'adb  -s %s shell rm /sdcard/crash-dump.log' % self.serial
        U.cmd(cmd)
        U.Logging.info("init logs in device %s" % self.serial)
예제 #12
0
 def __start_appium(self, aport, bpport):
     if platform.system() == 'Windows':
         import subprocess
         subprocess.Popen("appium -p %s -bp %s -U %s" %
                          (aport, bpport, self.device),
                          shell=True)
     else:
         appium = U.cmd('appium -p %s -bp %s -U %s' %
                        (aport, bpport, self.device))
         while True:
             appium_line = appium.stdout.readline().strip()
             L.Logging.debug(appium_line)
             if 'listener started' in appium_line:
                 break
예제 #13
0
def check_environment():
    appium = U.cmd("appium -v")
    appium_version = appium.stdout.readlines()[0].strip()
    if '1.' not in appium_version:
        U.Logging.error('appium not in computer')
        exit()
    else:
        U.Logging.info('appium version is %s ' % appium_version)

    if not GetDevice.get_device():
        U.Logging.error('the computer is not connected to any devices')
        exit()
    else:
        U.Logging.info('Device is connecting')
예제 #14
0
 def __darwin_all(self, ):
     for line in U.cmd(
             "ps -A | grep logcat|awk '{print $1}'").stdout.readlines():
         U.cmd('kill -9 %s' % line.strip())
         U.Logging.debug('CleanProcess:Darwin:kill logcat')
     for line in U.cmd(
             "ps -A | grep appium|awk '{print $1}'").stdout.readlines():
         U.cmd('kill -9 %s' % line.strip())
         U.Logging.debug('CleanProcess:Darwin:kill appium')
예제 #15
0
 def start_appium(self):
     aport = random.randint(4700, 4900)
     bpport = random.randint(4700, 4900)
     self.__start_appium(aport, bpport)
     count = 20
     for i in range(count):
         appium = U.cmd('netstat -aon | findstr %d' %
                        aport).stdout.readline()
         if appium:
             L.Logging.debug('start appium :p %s bp %s device: %s' %
                             (aport, bpport, self.device))
             return aport
         else:
             L.Logging.info('waiting start appium 3 seconds')
             U.sleep(3)
예제 #16
0
    def __linux_all(self):
        for line in U.cmd("ps -ef | grep logcat|grep -v grep|awk '{print $2}'"
                          ).stdout.readlines():
            U.cmd('kill -9 %s' % line.strip())
            U.Logging.debug('CleanProcess:linux:kill logcat')

        for line in U.cmd("ps -ef |grep appium |grep -v grep|awk '{print $2}'"
                          ).stdout.readlines():
            U.cmd('kill -9 %s' % line.strip())
            U.Logging.debug('CleanProcess:linux:kill appium')
예제 #17
0
    def __start_driver(self, aport, bpport):
        """
        clear logcat and appium all process
        :return:
        """
        if platform.system() == 'Windows':

            import subprocess
            subprocess.Popen("appium -p %s -bp %s -U %s" %
                             (aport, bpport, self.device), shell=True)

        else:
            appium = U.cmd("appium -p %s -bp %s -U %s" %
                           (aport, bpport, self.device))  # start appium
            while True:
                appium_line = appium.stdout.readline().strip()
                U.Logging.debug(appium_line)
                if 'listener started' in appium_line:
                    break
예제 #18
0
    def __start_driver(self, aport, bpport):
        """
        清理logcat与appium所有进程
        :return:
        """
        if platform.system() == 'Windows':
            import subprocess
            subprocess.Popen("appium -p %s -bp %s -U %s" %
                             (aport, bpport, self.device),
                             shell=True)

        else:
            appium = U.cmd("appium -p %s -bp %s -U %s" %
                           (aport, bpport, self.device))  # 启动appium
            while True:
                appium_line = appium.stdout.readline().strip()
                self.log.info(appium_line)
                U.sleep(1)
                if 'listener started' in appium_line or 'Error: listen' in appium_line:
                    break
예제 #19
0
    def __start_driver(self, aport, bpport):
        """
        清理logcat与appium所有进程
        :return:
        """
        if platform.system() == 'Windows':
            # 在win10启动appium有bug,暂时处理方案
            import subprocess
            subprocess.Popen("appium -p %s -bp %s -U %s" %
                             (aport, bpport, self.device),
                             shell=True)

        else:
            appium = U.cmd("appium -p %s -bp %s -U %s" %
                           (aport, bpport, self.device))  # 启动appium
            while True:
                appium_line = appium.stdout.readline().strip()
                U.Logging.debug(appium_line)
                if 'listener started' in appium_line:
                    break
예제 #20
0
 def __del__(self):
     # windows adb不会释放logc.log需要强制释放一下
     if system is "Windows":
         U.cmd("taskkill /im adb.exe /f")
예제 #21
0
def clean_logcat(device):
    pid = U.cmd('netstat -aon | findstr %s' % device).stdout.readline().strip().split(' ')[-1]
    U.cmd('taskkill /f /pid %s' % pid)
    U.sleep(2)
    L.Logging.success('stop logcat %s' % device)
예제 #22
0
 def cover_install_apks(self, package_name):
     U.cmd('adb install -r {}'.format(self.all_result_path + package_name))