def __init__(self, encoding='utf-8'): """初始化""" formatter = logging.Formatter() now_time = time.strftime('%Y-%m-%d') dir_log = '{}.log'.format(now_time) log_dir = read_file('log', dir_log) level = MyConfig('level').config logs_day = MyConfig('logs_save').config file_handler = logging.FileHandler(log_dir, encoding=encoding) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler(sys.stdout) console_handler.formatter = formatter self.Logging = logging.getLogger(__name__) self.Logging.addHandler(file_handler) self.Logging.setLevel(level) self.current_time = standard_time() if isinstance(logs_day, int): for i in range(logs_day): dir_log = '{}.log'.format( (datetime.today() - timedelta(days=i + logs_day)).strftime('%Y-%m-%d')) log_dir = read_file('log', dir_log) exists = os.path.exists(log_dir) if exists: os.remove(log_dir) else: TypeError('日志最长保存天数格式错误,它需要是一个int型')
def __init__(self): self.Mail = smtplib.SMTP() self.contents = MIMEMultipart() self.sender = MyConfig('send_account').send_email self.sender_password = MyConfig('send_password').send_email self.server = MyConfig('server').send_email self.receiver = MyConfig('receiver').send_email self.title_name = MyConfig('project_name').excel_parameter self.title = MyConfig('science').excel_parameter self.img_path = read_file('img', 'html.png') self.excel_path = read_file('report', 'ExcelReport.xlsx')
def setUp(self): """用例初始化""" self.module = self.__class__.__module__ self.class_name = self.__class__.__name__ self.case_name = self._testMethodName self.current_path = os.path.dirname(__file__) _data_initialization = Get(module=self.MODULE.split('\\')[-2], class_name=self.class_name, case_name=self.case_name) _return_data = _data_initialization.re() self.level = _return_data.get('level') self.author = _return_data.get('author') self.url = _return_data.get('url') self.second = _return_data.get('asserts') self.case_remark = _return_data.get('scene') if self.case_remark: self.data = _data_initialization.param_extract(self.case_remark) else: msg = "{}.{}.{}".format(self.module, self.class_name, self.case_name) warnings.warn(msg + "common中scene参数为空,此参数不能为空,请增加") self.driver.set_page_load_timeout(self.wait) self.driver.set_script_timeout(self.wait) self.current_time = standard_time() self.screenshots_path = read_file('img', '{}.png'.format(self.case_name)) self.start_time = time.time()
def current_file_path(): """获取当前路径下所有的文件名,并删除以test_开头的png""" path = os.path.dirname(__file__) paths = os.listdir(path) for i in paths: if 'test_' in i: path = read_file('img', i) os.remove(path)
def _save_as_report(self, case_name): """ 此方法主要用于,html存储 :return: 返回对应的路径 """ path = read_file('report', '{}.html'.format(self.html_name)) html = self._html_handle(case_name) with open(path, 'wt', encoding=self.encoding) as f: f.writelines(html) return path
def _module(self, modules: classmethod): """模块是否存在""" try: path = read_file(self.paths, modules) if not os.path.exists(path): os.mkdir(path) return modules else: return modules except Exception as exc: self._EXCEPTIONS(FUN_NAME(self.path), self.time, exc)
def _browser_get_html(self, case_name): """ 将请求的html用浏览器请求 :return: ... """ path = self._save_as_report(case_name) img_path = read_file('img', 'html.png') driver = browser(switch=self.switch_browser) driver.get(path) import time time.sleep(2) driver.save_screenshot(img_path) driver.quit()
def get_log(): """获取项目logo""" url = MyConfig('url').base_url + MyConfig('logo_url').config r = requests.get(url, stream=True) if r.content: log_path = read_file('img', 'logo.png') with open(log_path, 'wb') as f: f.write(r.content) if os.path.exists(log_path): img = Image.open(log_path) x, y = img.size p = Image.new('RGBA', img.size, (255, 255, 255)) p.paste(img, (0, 0, x, y)) p.save(log_path)
def browser(switch=False): """打开浏览器""" global driver driver_path = read_file('package', 'ChromeDriver.exe') if switch: warnings.filterwarnings('ignore') options = webdriver.ChromeOptions() options.add_argument('--headless') driver = webdriver.Chrome(driver_path, chrome_options=options) driver.set_window_size(1900, 980) else: options = None driver = webdriver.Chrome(driver_path, chrome_options=options) driver.maximize_window() return driver
def __init__(self, first, second, id, level, name, remark, status, reason, url, time, driver, module, screenshots_path, author, myself, error_path, log=None, encoding='utf8'): """初始化""" self.first = first self.second = second self.id = id self.level = level self.name = name self.remark = remark self.status = status self.reason = reason self.url = url self.time = time self.driver = driver self.screenshots_path = screenshots_path self.author = author self.myself = myself self.log = log self.error_path = error_path self.module = module self.encoding = encoding self.img_path = None self.thread = MyConfig('thread').config self.sql_type = MyConfig('execute_type').sql self.project = MyConfig('project_name').excel_parameter self.case_path = read_file(self.project, 'case.txt') if 'my_sql' == self.sql_type: self.sql = Mysql() else: self.sql = MyDB()
def __init__(self, encoding='utf8'): """初始化""" self.current_path = os.path.dirname(__file__) self.re = MyConfig('re').config sql_type = MyConfig('execute_type').sql if 'my_sql' == sql_type: self.sql = Mysql() else: self.sql = MyDB() project = MyConfig('project_name').excel_parameter path = read_file(project, 'case.txt') with open(path, 'wt', encoding=encoding): pass self.mail = Email() self.start_time = standard_time() self.wait = MyConfig('while_sleep').config self.case = MyConfig('while_case').config self.thread = MyConfig('thread').config self.excel = ExcelTitle self._clear_sql()
def _connect_sql(self): """连接数据库""" db_path = read_file('package', 'DB.db') conn = sqlite3.connect(db_path) return conn
def _read_project_data(self): """读取项目下common.yaml全部数据""" path = read_file(self.pro_name, 'common.yaml') with open(path, 'rt', encoding=self.encoding) as f: return yaml.safe_load(f)
def _read_config_data(self): """读取config.yaml中的全部数据""" path = read_file('config', 'config.yaml') with open(path, 'rt', encoding=self.encoding) as f: return yaml.safe_load(f)