def __init__(self, filename=Config.LOGGER): # 获取logger实例,如果参数为空则返回root logger self.logger = logging.getLogger(filename) # 指定logger输出格式 formatter = logging.Formatter( '%(asctime)s %(levelname)-8s: %(message)s') # 文件日志 Utils.make_dir(Config.LOG_DIR) file_handler = logging.FileHandler("{}.log".format( os.path.join(Config.LOG_DIR, filename)), encoding="utf-8") file_handler.setFormatter(formatter) # 可以通过setFormatter指定输出格式 # 控制台日志 console_handler = logging.StreamHandler(sys.stdout) console_handler.formatter = formatter # 也可以直接给formatter赋值 # 为logger添加的日志处理器 if file_handler.baseFilename not in \ [x.baseFilename for x in self.logger.handlers if getattr(x, "baseFilename", False)]: self.logger.addHandler(file_handler) self.logger.addHandler(console_handler) # 指定日志的最低输出级别,默认为WARN级别 self.logger.setLevel(logging.INFO)
def write_report(html): file = "report{}.html".format( datetime.strftime(datetime.now(), "%Y-%m-%d %H-%M-%S")) Utils.make_dir(Config.report_path) filename = os.path.join(Config.report_path, file) with open(filename, "w", encoding="utf-8") as f: f.write(html) with open(os.path.join(Config.report_path, "report.html"), "w", encoding="utf-8") as file: file.write(html)
def check_driver(cls, version): status, filename = False, None Utils.make_dir(Config.driver_dir) # check driver_dir for root, dirs, files in os.walk(Config.driver_dir): for file in files: if version not in file: try: os.remove(os.path.join(root, file)) except: pass else: status, filename = True, file return status, filename
def get_pic(self, func_name, case_id=None): """ 截图方法 :param case_id: :return: """ pic_dir = Config.pic_dir Utils.make_dir(pic_dir) pic = os.path.join(pic_dir, case_id) if case_id else pic_dir Utils.make_dir(pic) filename = os.path.join( pic, "{} {}.png".format( func_name, datetime.strftime(datetime.now(), "%Y-%m-%d %H-%M-%S"))) self.driver.get_screenshot_as_file(filename) return filename