def __init__(self, excelfilename): self.excelfilename = excelfilename fc = FileChecKController() boolean = fc.is_has_file(excelfilename) if boolean: self.excel_path = fc.get_fileabspath() self.log4py = LoggingController()
class InitDriverOption(object): def __init__(self): self.log4py = LoggingController() self.run_cfg = InitConfiger() self.android = AdbCmder() self.run_data = None def get_desired_capabilities(self, sno): device_info = {"udid": sno} try: result = subprocess.Popen( "adb -s %s shell getprop" % sno, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines() for res in result: if re.search(r"ro\.build\.version\.release", res): device_info["platformVersion"] = ( res.split(': ')[-1].strip())[1:-1] elif re.search(r"ro\.product\.model", res): device_info["deviceName"] = ( res.split(': ')[-1].strip())[1:-1] if "platformVersion" in device_info.keys( ) and "deviceName" in device_info.keys(): break except Exception, e: self.log4py.error("获取手机信息时出错 :" + str(e)) return None desired_caps_conf = self.run_cfg.get_desired_caps_conf() desired_caps = device_info.copy() desired_caps.update(desired_caps_conf) return desired_caps
def __init__(self): self.fkctl = FileChecKController() if self.fkctl.is_has_file("allpath.ini"): fp = self.fkctl.get_fileabspath() self.cfgctl = ConfigController(fp) self.log4py = LoggingController() self.pro_path = self.fkctl.get_project_path()
def __init__(self): self.log4py = LoggingController() path_obj = GetAllPathController() self.appium_log_path = path_obj.get_appium_logs_path() self.appium_port_list = [] self.bootstrap_port_list = [] self.device_list = [] self.cfg = CreateConfigFile() self.tmp = {}
def __init__(self, driver): self.android = AdbCmder() self.log4py = LoggingController() self.driver = driver self.taction = TouchAction(self.driver) self.path_get = GetAllPathController() self.actions = [] self.xml_file_path = self.path_get.get_dumpxml_path() self.pattern = re.compile(r"\d+") self.capturePath = self.path_get.get_capture_path()
def __init__(self): self.fc = FileChecKController() bools = self.fc.is_has_file("email.ini") if bools: fp = self.fc.get_fileabspath() conf = ConfigController(fp) self.smtp_host = conf.get("emails", "smtp_host") self.pop3_host = conf.get("emails", "pop3_host") self.receiver = conf.get("emails", "receiver").split(",") self.receiver_pa = conf.get("emails", "receiver_pa") self.sender = conf.get("emails", "sender") self.sender_pa = conf.get("emails", "sender_pa") self.log4py = LoggingController()
class JsonParser(object): def __init__(self): self.json_obj = None self.fc = FileChecKController() if self.fc.is_has_file("android_devices_info.json"): self.json_file_path = self.fc.get_fileabspath() self.log4py = LoggingController() def load_json(self, json_file_path): fin = open(json_file_path, "r") try: json_obj = json.load(fin) self.log4py.info("加载了%s文件" % json_file_path) except ValueError, e: json_obj = {} fin.close() return json_obj
def __init__(self): self.logger = LoggingController() self.fc = FileChecKController() boolean = self.fc.is_has_file("db.ini") if boolean: self.inipath = self.fc.get_fileabspath() self.conf = ConfigController(self.inipath) self.host = str(self.conf.get("dbset", "host")) self.port = int(self.conf.get("dbset", "port")) self.user = str(self.conf.get("dbset", "user")) self.passwd = str(self.conf.get("dbset", "passwd")) self.db = str(self.conf.get("dbset", "db")) self.charset = str(self.conf.get("dbset", "charset")) self.conn = mysqldb.Connect(self.host, self.user, self.passwd, self.db, self.port, self.charset) self.logger.debug("数据库初始化完成" + self.host + str(self.port) + self.db + self.charset)
class MySQLController(object): def __init__(self): self.logger = LoggingController() self.fc = FileChecKController() boolean = self.fc.is_has_file("db.ini") if boolean: self.inipath = self.fc.get_fileabspath() self.conf = ConfigController(self.inipath) self.host = str(self.conf.get("dbset", "host")) self.port = int(self.conf.get("dbset", "port")) self.user = str(self.conf.get("dbset", "user")) self.passwd = str(self.conf.get("dbset", "passwd")) self.db = str(self.conf.get("dbset", "db")) self.charset = str(self.conf.get("dbset", "charset")) self.conn = mysqldb.Connect(self.host, self.user, self.passwd, self.db, self.port, self.charset) self.logger.debug("数据库初始化完成" + self.host + str(self.port) + self.db + self.charset) def execute_select(self, sql): cursor = self.conn.cursor() try: cursor.execute(sql) self.logger.debug("check_acct_available :" + sql) res = cursor.fetchall() if len(res) < 1: self.logger.error("%s执行查询内容不存在" % sql) return res finally: cursor.close() return None def execute_add_one(self, sql): cursor = self.conn.cursor() #操作数据库的游标 try: cursor.execute(sql) #执行sql语句 if cursor.rowcount == 1: self.logger.debug("%s添加成功" % sql) self.conn.commit() #执行成功后向数据库进行提交 return True except Exception, e: self.conn.rollback() self.logger.error("插入数据出现错误" + str(e)) finally:
class AppiumDriver(object): def __init__(self, driver): self.android = AdbCmder() self.log4py = LoggingController() self.driver = driver self.taction = TouchAction(self.driver) self.path_get = GetAllPathController() self.actions = [] self.xml_file_path = self.path_get.get_dumpxml_path() self.pattern = re.compile(r"\d+") self.capturePath = self.path_get.get_capture_path() def is_displayed(self, by, value): is_displayed = False try: is_displayed = self.driver.find_element(by, value).is_displayed() self.log4py.debug("element [ " + str(value) + " ] displayed? " + str(is_displayed)) except Exception, e: self.log4py.error("element元素没有点位到"+str(e)) return is_displayed
class DeviceController(): def __init__(self): self.android = AdbCmder() self.log4py = LoggingController() ''' 获取连接上电脑的手机设备,返回一个设备名的list ''' def get_devices(self): sno_list = self.android.get_device_list() return sno_list ''' 根据不同的需求,设计了返回dict和list格式的两个function。 ''' def get_infos_as_dict(self): try: info = {} lists = self.get_devices() if not lists or len(lists) <= 0: self.log4py.info("NO Device connected") return None for sno in lists: sno, phone_brand, phone_model, os_version, ram, dpi, image_resolution, ip = self.get_info( sno) info[sno] = { "phone_brand": phone_brand, "phone_model": phone_model, "ram": ram, "os_version": os_version, "dpi": dpi, "image_resolution": image_resolution, "ip": ip } return info except TypeError, e: self.log4py.error(e) return None
class ExcelManager(object): def __init__(self, excelfilename): self.excelfilename = excelfilename fc = FileChecKController() boolean = fc.is_has_file(excelfilename) if boolean: self.excel_path = fc.get_fileabspath() self.log4py = LoggingController() def read_excel(self, excel_sheet_name): """打开目标excel文件 r--读,w--写(覆盖),a--追加写""" xls_data = xlrd.open_workbook(self.excel_path, "rb") table = xls_data.sheet_by_name(excel_sheet_name) #打开sheet页 self.log4py.debug("打开的%s文件中的sheet页" % self.excelfilename) return table # 将指定的sheet页对象返回给调用者 def writ_excel(self, row, column, value): x_data = xlrd.open_workbook(self.excel_path, "rb") #只能是xls文件 copy_sheet = copy(x_data) #copy,并对附件进行操作 write_xls = copy_sheet.get_sheet(0) #得到附件中的sheet页 write_xls.write(row, column, value) #将测试的结果追加到附件中sheet页中每一行的后面 copy_sheet.save(self.excel_path) #覆盖保存(注意编码错误)
class FileChecKController(): def __init__(self): self.__fileabspath = None #不可访问的 self.__logger = LoggingController() ''' 是否存在指定的文件,路径默认为当前项目的目录 ''' def is_has_file(self, filename): propath = self.get_project_path() boolean = self.is_path_has_file(propath, filename) return boolean ''' 指定目录下是否存在指定的文件 ''' def is_path_has_file(self, path, filename): boolean = self.check_has_file(path, filename) return boolean ''' 扫描指定目录下的所有文件,找到所要找的文件,return True or False ''' def check_has_file(self, path, filename): try: for filep, dirs, filelist in os.walk(path): for fl in filelist: if cmp(fl, filename) == 0: #这个字符串的比较存在风险,python3不支持,待修改 self.__fileabspath = os.path.join(filep, fl) self.__logger.info("查找的%s文件存在" % filename) return True return False except Exception, e: self.__logger.error("check_has_file()方法出现异常" + str(e))
class EmailController(object): def __init__(self): self.fc = FileChecKController() bools = self.fc.is_has_file("email.ini") if bools: fp = self.fc.get_fileabspath() conf = ConfigController(fp) self.smtp_host = conf.get("emails", "smtp_host") self.pop3_host = conf.get("emails", "pop3_host") self.receiver = conf.get("emails", "receiver").split(",") self.receiver_pa = conf.get("emails", "receiver_pa") self.sender = conf.get("emails", "sender") self.sender_pa = conf.get("emails", "sender_pa") self.log4py = LoggingController() def send_email_is_html(self): latestfpath, fname, currentfolder = self.fc.get_LatestFile() msgRoot = MIMEMultipart('related') ff = open(latestfpath, 'rb') message = MIMEText(ff.read(), 'html', 'utf-8') ff.close() message['From'] = self.sender #message['To'] = self.receiver subject = '实验室数字化平台-自动化测试报告' message['Subject'] = Header(subject, 'utf-8') msgRoot.attach(message) try: smtpObj = smtplib.SMTP() smtpObj.connect(self.smtp_host) smtpObj.login(self.sender, self.sender_pa) smtpObj.sendmail(self.sender, self.receiver, msgRoot.as_string()) self.log4py.debug("SendEmail_withFile邮件发送成功") smtpObj.close() except Exception, e: print e self.log4py.error("Error: 无法发送邮件::" + str(e))
def html_reporter(): logger = LoggingController() fc = FileChecKController() pro_path = fc.getProjectPath() boolean = fc.is_has_file("framework.ini") if boolean: inipath = fc.get_fileabspath() fw_conf = ConfigController(inipath) htmlrp_path = fw_conf.get("htmlreportPath", "htmlreportPath") htmreportl_abs_path = os.path.join(pro_path,htmlrp_path) timecurrent = DateTimeManager().formatedTime("%Y-%m-%d-%H-%M-%S") logger.debug("=====创建了一个html文件报告,路径是::"+htmreportl_abs_path) file_path = str(htmreportl_abs_path)+timecurrent+"-LDP-TestingRreporter.html" try: if os.path.exists(file_path): html_obj = open(file_path, "a") #打开文件 追加 return html_obj else: html_obj = file(file_path, "wb+") return html_obj except Exception, e: logger.error("创建html_reporter出现错误"+str(e))
class ServicePort(object): def __init__(self): self.log4py = LoggingController() path_obj = GetAllPathController() self.appium_log_path = path_obj.get_appium_logs_path() self.appium_port_list = [] self.bootstrap_port_list = [] self.device_list = [] self.cfg = CreateConfigFile() self.tmp = {} def is_port_used(self, port_num): """ 检查端口是否被占用 netstat -aon | findstr port 能够获得到内容证明端口被占用 """ flag = False try: port_res = subprocess.Popen( 'netstat -ano | findstr %s | findstr LISTENING' % port_num, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines() reg = re.compile(str(port_num)) for i in range(len(port_res)): ip_port = port_res[i].strip().split(" ") if re.search(reg, ip_port[1]): flag = True self.log4py.info( str(port_num) + " 端口已经在使用,对应的进程是:" + str(ip_port[-1])) self.tmp[port_num] = ip_port[-1] if not flag: self.log4py.info(str(port_num) + " 端口没有被占用.") except Exception, e: self.log4py.error( str(port_num) + " port get occupied status failure: " + str(e)) return flag
class WindowCmder(object): def __init__(self): self.log4py = LoggingController() def is_port_used(self, port_num): """ 检查端口是否被占用 netstat -aon | findstr port 能够获得到内容证明端口被占用 :param port_num: :return: """ flog = True try: port_res = subprocess.Popen('netstat -ano | findstr %s' % port_num, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines() if len(port_res) <= 0: self.log4py.info(str(port_num) + " port unoccupied.") flog = False else: self.log4py.error(str(port_num) + " port has been occupied.") except Exception, e: self.log4py.error(str(port_num) + " port get occupied status failure: " + str(e)) return flog
def __init__(self): self.__fileabspath = None #不可访问的 self.__logger = LoggingController()
def __init__(self): self.log4py = LoggingController()
def __init__(self): self.log4py = LoggingController() self.run_cfg = InitConfiger() self.android = AdbCmder() self.run_data = None
def __init__(self): self.json_obj = None self.fc = FileChecKController() if self.fc.is_has_file("android_devices_info.json"): self.json_file_path = self.fc.get_fileabspath() self.log4py = LoggingController()
class GetAllPathController(object): def __init__(self): self.fkctl = FileChecKController() if self.fkctl.is_has_file("allpath.ini"): fp = self.fkctl.get_fileabspath() self.cfgctl = ConfigController(fp) self.log4py = LoggingController() self.pro_path = self.fkctl.get_project_path() def get_dumpxml_path(self): self.log4py.info("executive -get_dumpxml_path- function ") path = os.path.join(self.pro_path, self.cfgctl.get("dumpxmlPath", "dumpxmlPath")) if PATH(path): self.log4py.info("获取 %s"%path) return path return None def get_htmlreport_path(self): self.log4py.info("executive -get_htmlreport_path- function ") path = os.path.join(self.pro_path, self.cfgctl.get("htmlreportPath", "htmlreportPath")) if PATH(path): self.log4py.info("获取 %s" % path) return path return None def get_logs_path(self): self.log4py.info("executive -get_logs_path- function ") path = os.path.join(self.pro_path, self.cfgctl.get("logsPath", "logsPath")) if PATH(path): if not os.path.exists(path): os.makedirs(path) self.log4py.info("获取 %s" % path) return path return None def get_capture_path(self): self.log4py.info("executive get_logs_path function ") path = os.path.join(self.pro_path, self.cfgctl.get("capturePath", "capturePath")) if PATH(path): self.log4py.info("获取 %s" % path) return path return None def get_appium_logs_path(self): self.log4py.info("executive get_logs_path function ") path = os.path.join(self.pro_path, self.cfgctl.get("appiumlogPath", "appiumlogPath")) if PATH(path): if not os.path.exists(path): os.makedirs(path) self.log4py.info("获取到appium服务的日志路径 %s" % path) return path.replace("\\", "/") return None
class CreateConfigFile(object): def __init__(self): self.cfg = ConfigParser.ConfigParser() self.log4py = LoggingController() self.path = (os.getcwd()).split('src')[0] + "\\testconfig" def set_appium_uuids_ports(self, device_list, port_list): """ 遍历list,按照下表进行对应映射 :param device_lsit: 手机uuid :param port_list: pc启动的appium服务端口 """ f_path = self.create_config_file(self.path) if len(device_list) > 0 and len(port_list) > 0: self.cfg.read(f_path) for i in range(len(device_list)): filed = device_list[i] key = filed value = port_list[i] # 因为是覆盖写入,没有section,需要先添加再设置, 初始化的服务都加一个run的标识 self.cfg.add_section(filed) self.cfg.set(filed, key, value) self.cfg.set(filed, "run", "0") self.cfg.write(open(f_path, 'wb')) self.log4py.debug( "设备sno与appium服务端口映射已写入appiumService.ini配置文件:{}--{}".format( key, value)) def set_appium_uuid_port(self, device, port, bp): """ 如果这样一个一个的写入到配置文件中,是追加还是覆盖?如果是覆盖的,服务启动完成后就剩一个配置,所以不行 如果是追加,需要判断配置文件中是否已经有了相同的section,有就更新,没有就添加 :param device: 手机uuid :param port pc启动的appium服务端口 """ f_path = os.path.join(self.path, 'appiumService.ini') if not os.path.exists(f_path): os.makedirs(f_path) if device is not None and port is not None: self.cfg.read(f_path) sec = device key = sec value = port if sec in self.cfg.sections(): self.cfg.set(sec, key, value) self.cfg.set(sec, "bp", bp) self.cfg.set(sec, "run", "0") else: self.cfg.add_section(sec) self.cfg.set(sec, key, value) self.cfg.set(sec, "bp", bp) self.cfg.set(sec, "run", "0") self.cfg.write(open(f_path, 'wb')) self.log4py.debug( "设备sno与appium服务端口映射已写入appiumService.ini配置文件:{}--{}".format( key, value)) def create_config_file(self, path): """ 如果path这个文件不存在,就创建这个文件;存在就清空文件 :param path: :return: """ if not os.path.exists(path): os.makedirs(path) f_path = os.path.join(path, 'appiumService.ini') f = open(f_path, "wb") f.close() return f_path def get_all_appium_server_port(self): f_path = os.path.join(self.path, 'appiumService.ini') port_list = [] if os.path.exists(f_path): self.cfg.read(f_path) section_list = self.cfg.sections() for sl in section_list: port_list.append(self.cfg.get(sl, sl)) port_list.append(self.cfg.get(sl, "bp")) return port_list
def __init__(self): self.cfg = ConfigParser.ConfigParser() self.log4py = LoggingController() self.path = (os.getcwd()).split('src')[0] + "\\testconfig"
def __init__(self): self.android = AdbCmder() self.log4py = LoggingController()