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
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
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 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 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
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))