一.PO项目架构
此次PO项目架构分为Base层、Common层、Data层、Logs层、PageObject层、Report层、TestCase层、config.ini
二.Common层
Common层主要包含处理Excel文件的方法、获取项目路径、测试系统URL信息和框架执行相关日志功能的实现方法
三.代码实现
- 获取项目路径、测试系统URL方法,文件夹名为function.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time : 2019/12/24 11:51
# @file : function.py
# @Software : PyCharm
# @author : MAYH
# @contact : mayh@chnsys.com.cn
# @Version :V1.1.0
"""
文件说明:获取配置文件参数
"""
import os, configparser
# 获取项目路径
def project_path():
return os.path.split( os.path.realpath(__file__))[0].split( 'C' )[0]
# 返回config.ini文件中testUrl
def config_url():
config = configparser.ConfigParser()
config.read( project_path() + "config.ini" )
# print(config.read(project_path() + " config.ini" ))
return config.get( 'testUrl', "url" )
if __name__ == "__main__":
print( "项目路径为:" + project_path() )
print( "被检测系统的URL为:" + config_url() )
- 框架执行相关日志功能的实现方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time : 2019/12/26 15:58
# @file : log.py
# @Software : PyCharm
# @author : MAYH
# @contact : mayh@chnsys.com.cn
# @Version :V1.1.0
"""
文件说明:
"""
import logging
import time
from Common.function import project_path
class FrameLog():
def __init__(self, logger=None):
# def logger_config(log_path, logging_name):
'''
logger是日志对象,handler是流处理器,console是控制台输出(没有console也可以,将不会在控制台输出,会在日志文件中输出)
'''
# 获取logger对象,取名
self.logger = logging.getLogger( logger )
# 输出DEBUG及以上级别的信息,针对所有输出的第一层过滤
self.logger.setLevel( level=logging.DEBUG )
self.log_time = time.strftime( "%Y_%m_%d_" )
self.log_path = project_path() + '/Logs/'
self.log_name = self.log_path + self.log_time + 'log.log'
# 获取文件日志句柄并设置日志级别,第二层过滤
handler = logging.FileHandler( self.log_name, encoding='UTF-8' )
handler.setLevel( logging.INFO )
# 生成并设置文件日志格式
# formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
formatter = logging.Formatter(
'[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]%(message)s' )
handler.setFormatter( formatter )
# console相当于控制台输出,handler文件输出。获取流句柄并设置日志级别,第二层过滤
console = logging.StreamHandler()
console.setLevel( logging.DEBUG )
# 为logger对象添加句柄
self.logger.addHandler( handler )
self.logger.addHandler( console )
return logger
def log(self):
return self.logger
if __name__ == "__main__":
lo = FrameLog()
log = lo.log()
log.info( "info" )
log.error( "error" )
log.debug( "debug" )
log.warning( "warning" )
- 处理测试数据文件(Excel)方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time : 2019/12/26 16:30
# @file : excel_data.py
# @Software : PyCharm
# @author : MAYH
# @contact : mayh@chnsys.com.cn
# @Version :V1.1.0
"""
文件说明:读取excel操作,所有数据放在字典中
"""
import xlrd, os
def read_excel(filename, index):
'''
:param filename: 文件名
:param index: Excel sheet工作簿索引
:return:
'''
xls = xlrd.open_workbook( filename )
sheet = xls.sheet_by_index( index )
# print( sheet.nrows )
# print( sheet.ncols )
dic = {}
for j in range( sheet.ncols ):
data = []
for i in range( sheet.nrows ):
data.append( sheet.row_values( i )[j] )
dic[j] = data
return dic
if __name__=='__main__':
# 读取Excel操作,返回字典
excel_path=os.path.split( os.path.realpath( __file__ ) )[0].split( "C" )[0] + "Data\\testdata.xlsx"
print(excel_path)
data = read_excel( excel_path, 0 )
print( data )
print( data.get( 1 ) )