import os from getpathinfo import get_path from openpyxl import load_workbook filepath = get_path() print(filepath) """ 读取xlsx表格 """ class ReadExcel(object): def get_xlsx(self, xlsx_name, sheet_name): excel_list = [] excelpath = os.path.join(filepath, 'testCase', xlsx_name) excel = load_workbook(excelpath) # 加载文件 # 获取sheet sheet = excel[sheet_name] # 获取表名 # 获取行数和列数 # maxrow = sheet.max_row # 获取行数 # maxcol = sheet.max_column # 获取列数 for row in sheet.rows: # 根据行数做循环 son_list = [] for cell in row: son_list.append(cell.value) # 将每一行的数据添加到son_list列表里面 if son_list[ 1] != u'case_name': # son_list的第2列不等于case_name那么我们把这行的数据添加到excel_list = [] excel_list.append(son_list) return excel_list
def __init__(self, filename): path = getpathinfo.get_path() #获取本地路径 self.filepath = os.path.join(path, 'data') + "/" + filename #拼接定位到data文件夹
import os import common.HTMLTestRunner as HTMLTestRunner import getpathinfo import unittest import readconfig from common.configemail import SendEmail from datetime import datetime from logging_method import LoggingMethod # from apscheduler.schedulers.blocking import BlockingScheduler # import pythoncom # import common.Log send_mail = SendEmail() path = getpathinfo.get_path() report_path = os.path.join(path, 'result') on_off = readconfig.ReadConfig().get_email('on_off') logger = LoggingMethod(__name__).getlogger() class AllTest: # 定义一个类AllTest def __init__(self): # 初始化一些参数和数据 global resultName now = datetime.now().strftime('%Y-%m-%d_%H_%M_%S') resultName = str(report_path + '\\' + now + '_report.html') # resultName = str(os.path.join(report_path,"report.html")) self.caseListFile = os.path.join(path, "caselist.txt") # 配置执行哪些测试文件的配置文件路径 self.caseFile = os.path.join(path, "testCase") # 真正的测试断言文件路径 self.caseList = [] logger.info("resultNmae", resultName)
import os import configparser from getpathinfo import get_path path = get_path() config_path = os.path.join(path, 'config', 'config.ini') # 获取config文件夹下的config.ini文件 config = configparser.ConfigParser() # 调用外部的读取配置文件的方法 初始化实例 config.read(config_path, encoding='utf-8') # 读取config文件 class ReadConfig(object): # 从config.ini中读取需要的数据 def __init__(self): self.server = config.get('APP', 'ServerModel') self.email_on_off = config.get def get_http(self, name=None): # 0测试服,1预发布,2正式服, if self.server == '0': self.url = config.get('TestHttp', name) elif self.server == '1': self.url = config.get('preHttp', name) elif self.server == '2': self.url = config.get('ReleaseHttp', 'baseurl') else: print("服务器选择错误,0测试服,1预发布,2正式服,目前选择", self.server) return self.url def get_mysql(self, name): # 写好,留以后备用。但是因为我们没有对数据库的操作,所以这个可以屏蔽掉 sqlvalue = config.get('Local_Database', name) return sqlvalue
2、设置下logger的日志的等级 3、创建合适的Handler(FileHandler要有路径) 4、设置下每个Handler的日志等级 5、创建下日志的格式 6、向Handler中添加上面创建的格式 7、将上面创建的Handler添加到logger中 8、打印输出logger.debug\logger.info\logger.warning\logger.error\logger.critical ''' logpath = get_path() print(logpath) class LoggingMethod(object): # 将日志输出到文件 def __init__(self, name): # 1、创建一个logger self.logger = logging.getLogger(name) # 2、设置下logger的日志的等级 self.logger.setLevel(logging.DEBUG) # 定义log文件名 now = time.strftime('%Y-%m-%d_%H_%M_%S') filename = logpath + '\\LOG\\' + now # 创建等级为DEBUG的 的日志文件,默认使用全局的Logger日志等级 all_handler = logging.handlers.TimedRotatingFileHandler(
import logging, time import os import getpathinfo path = getpathinfo.get_path() # 获取本地路径 log_path = os.path.join(path, 'logs') # log_path是存放日志的路径 # 如果不存在这个logs文件夹,就自动创建一个 if not os.path.exists(log_path): os.mkdir(log_path) class Log(): def __init__(self): # 文件的命名 self.logname = os.path.join(log_path, '%s.log' % time.strftime('%Y_%m_%d')) self.logger = logging.getLogger() self.logger.setLevel(logging.DEBUG) # 日志输出格式 self.formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') def __console(self, level, message): # 创建一个fileHander,用于写入本地 fh = logging.FileHandler(self.logname, 'a', encoding='utf-8') fh.setLevel(logging.DEBUG) fh.setFormatter(self.formatter) self.logger.addHandler(fh) # 创建一个StreamHandler,用于输入到控制台 ch = logging.StreamHandler() ch.setLevel(logging.DEBUG)
read_conf = ReadConfig() # 从配置文件中读取,smtp服务器、端口、发件人、密码、收件人、抄送人 smtp = read_conf.get_email("host") port = read_conf.get_email("port") user = read_conf.get_email("user") pwd = read_conf.get_email("pwd") addressee = read_conf.get_email("addressee") # addressee=list(addressee.split(";")) chaosong = read_conf.get_email("chaosong") # 从配置文件中读取,邮件主题 now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") subject = "hello,这是" + now + read_conf.get_email("subject") print(subject) mail_path = os.path.join(getpathinfo.get_path(), 'result', 'report.html') # 获取测试报告路径 content = "<h1>测试报告来咯!</h1>" class SendEmail(object): def aliyun(self): # 构造邮件 msg = MIMEText(content, "html", "gbk") # msg邮件对象 msg['Subject'] = subject msg['From'] = user msg['to'] = addressee msg['Accept-Language'] = 'zh-CN' msg['Accept-Charset'] = 'ISO-8859-1,utf-8' # 发送邮件
class WDriver(object): log = Log() path = getpathinfo.get_path() # 获取本地路径 chromedriver_filepath = os.path.join( path, 'lib') + '/' + 'chromedriver.exe' # 拼接定位到存储第三方chromedriver.exe msedgedriver_filepath = os.path.join( path, 'lib') + '/' + 'msedgedriver.exe' #拼接定位到存储第三方msedgedriver.exe # Firefox driver def fireFoxDriver(self): """ :return: """ try: self.driver = webdriver.Firefox() except Exception as e: self.log.info( 'FireFoxDriverServer.exe executable needs to be in PATH. Please download!' ) raise e else: self.log.info('%s:found the Firefox driver [%s] successed !' % (sys._getframe().f_code.co_name, self.driver)) return self.driver # chrome driver def chromeDriver(self): """ :return: """ try: #修改下载文件位置 # chromeOptions = webdriver.ChromeOptions() # prefs = {"download.default_directory": "F:\\TestDownloads"} #chromeOptions.add_experimental_option("prefs", prefs) #self.driver = webdriver.Chrome(chrome_options=chromeOptions) self.driver = webdriver.Chrome(self.chromedriver_filepath) except Exception as e: self.log.info( 'ChromeDriverServer.exe executable needs to be in PATH. Please download!' ) raise e else: self.log.info('%s:found the chrome driver [%s] successed !' % (sys._getframe().f_code.co_name, self.driver)) return self.driver # msedge driver def msedgeDriver(self): """ :return: """ try: self.driver = webdriver.Edge(self.msedgedriver_filepath) except Exception as e: self.log.info( 'IEDriverServer.exe executable needs to be in PATH. Please download!' ) raise e else: self.log.info('%s:found the IE driver [%s] successed !' % (sys._getframe().f_code.co_name, self.driver)) return self.driver
"%Y-%m-%d %H:%M:%S", time.localtime(time.time())) # 从config文件中读取发件人 fromaddr = read_conf.get_email('from') # 从config文件中读取收件人 addressee = read_conf.get_email('addressee') # 从config文件中读取抄送人 cc = read_conf.get_email('cc') # foxmail邮箱的SMTP服务器及端口 foxsmtpserver = 'smtp.yidoutang.com' foxport = 25 # 发件人邮箱名及密码 foxusername = '******' foxpassword = '******' # 获取测试报告路径 report_path = os.path.join(getpathinfo.get_path(), 'result', 'report.html') logger = logger class Fox_Mail(object): def fox_mail(self): # 初始化邮件对象 msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = addressee msg['Cc'] = cc msg['Subject'] = subject # 邮件正文 content = """ HI: