Example #1
0
def start_server():
    cmd = 'taskkill /F /IM node.exe'
    logging.info(cmd)
    os.system(cmd)
    logging.info('启动appium服务')
    try:
        cd = 'start /b appium -a 127.0.0.1 -p 4723 --bootstrap-port 9517 --session-override --command-timeout 600'
        logging.info(cd)
        subprocess.call(cd,
                        shell=True,
                        stdout=open(
                            Path.log_path() + runtime.test_start_time() +
                            'appium.log', 'w'),
                        stderr=subprocess.STDOUT)
        appium_server_url = 'http://localhost:4723/wd/hub/'
        logging.info(appium_server_url)
        time.sleep(5)
        response = requests.get(appium_server_url)
        print(response.status_code)
        if response.status_code is 404:
            logging.info('appium服务启动成!!')
        else:
            raise Exception
    except Exception as a:
        logging.error('启动appium服务失败 %s' % a)
Example #2
0
def log_config():
    try:
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)  # Log等级总开关
        # 第二步,创建一个handler,用于写入日志文件
        logfile = Path.log_path() + runtime.test_start_time() + '.log'
        fh = logging.FileHandler(logfile, mode='w+')
        fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关
        # 第三步,再创建一个handler,用于输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)  # 输出到console的log等级的开关
        # 第四步,定义handler的输出格式
        formatter = logging.Formatter(
            "%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s"
        )
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)
        # 第五步,将logger添加到handler里面
        logger.addHandler(fh)
        logger.addHandler(ch)
        logging.info('测试开始时间:%s' % runtime.test_start_time())
        # logging.basicConfig(level=logging.DEBUG,
        #                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
        #                     datefmt='%a, %d %b %Y %H:%M:%S',
        #                     filename=Path.log_path()+runtime.test_start_time()+'.log',
        #                     filemode='w')
    except Exception as e:
        print(e)
        raise Custom_exception.LogConfigError
Example #3
0
def exception_handling(e,
                       index=None,
                       test_name=None,
                       method_name=None,
                       op=None):
    """
    错误处理
    :param e: 报错内容
    :param index: 用例编号
    :param test_name: 测试用例名称
    :param method_name: 测试用例对应方法
    :param op: 操作驱动
    :return:
    """
    global l
    logging.error(e)
    path = Path.report_path() + runtime.test_start_time() + '_error'
    mkdir_log_directory.mk_dir(path)  # 创建错误日志目录
    path1 = Path.log_path() + runtime.test_start_time() + '.log'
    if index:
        log_error = path + '\\' + test_name.decode('utf8') + '.txt'  # 记录错误日志文件
        way = path + '\\' + method_name + runtime.test_start_time() + '.png'
        op.screen(way, path)  # 截图
        log.error_log(path1, log_error, test_name)
        if 'AssertionError' in e:
            for i in range(0, len(l)):
                if index == l[i][0]:
                    l[i].append('fail')
                    l[i].append(log_error)
                    l[i].append(way)
        else:
            for i in range(0, len(l)):
                if index == l[i][0]:
                    l[i].append('error')
                    l[i].append(log_error)
                    l[i].append(way)
    else:
        log_error = path + '\\' + 'error.txt'  # 记录错误日志文件
        log.error_log(path1, log_error)
Example #4
0
def start(devices):
    # num = devicess["num"]
    app = {}
    mkdirInit(devices, app)
    # mc = MonkeyConfig.monkey_config(Path.scan_files(postfix='.ini'))
    # 打开想要的activity
    # ba.open_app(mc["package_name"], mc["activity"], devices) 留着备用可以统计每次打开哪个页面的启动时间等
    # monkey开始测试
    log = Path.log_path() + "\\" + str(uuid.uuid4())
    logging.debug('log路径 ' + log)
    monkey_log = log + "monkey.log"
    logging.debug('monkey日志路径: ' + monkey_log)
    package_name, monkey = Monkey_Config.monkey_config()
    # mc["cmd"] = mc['cmd'] + mc["monkey_log"]
    start_monkey("adb -s " + devices + " shell " + monkey + '>' + monkey_log,
                 log)
    # start_monkey("adb -s " + devices + " shell " + mc["cmd"], mc["log"])
    time.sleep(1)
    start_time = datetime.datetime.now()
    logging.info('测试开始时间 ' + str(start_time))
    pid = Monitor.get_pid(package_name, devices)
    cpu_kel = Monitor.get_cpu_kel(devices)
    before_battery = Monitor.get_battery(devices)
    num = 0
    while True:
        with open(monkey_log, encoding='utf-8') as monkeylog:
            time.sleep(1)  # 每1秒采集检查一次
            num = num + 1
            Monitor.cpu_rate(pid, cpu_kel, devices)
            Monitor.get_men(package_name, devices)
            Monitor.get_fps(package_name, devices)
            Monitor.get_flow(pid, devices)
            Monitor.get_battery(devices)
            if monkeylog.read().count('Monkey finished') > 0:
                end_time = datetime.datetime.now()
                logging.info(str(devices) + "测试完成咯")
                app[devices]["header"]["beforeBattery"] = before_battery
                app[devices]["header"]["afterBattery"] = Monitor.get_battery(
                    devices)
                app[devices]["header"]["monkey_log"] = monkey_log
                app[devices]["header"]["time"] = str(
                    (end_time - start_time).seconds) + "秒"
                app[devices]['num'] = num
                write_info(
                    app,
                    Path.scan_files(select_path=Path.info_path(),
                                    postfix=''.join(devices.split(':')) +
                                    '_info.pickle'))
                monkeylog.close()
                break
    monkeylog.close()
    logging.info(
        read_info(
            Path.scan_files(select_path=Path.info_path(),
                            postfix=''.join(devices.split(':')) +
                            '_info.pickle')))
    logging.info('测试结束。。。。。')
    report(
        read_info(
            Path.scan_files(select_path=Path.info_path(),
                            postfix=''.join(devices.split(':')) +
                            '_info.pickle')), devices)