def __init__(self, browser): """ 初始化测试浏览器. """ self.__driver = browser # 创建配置文件实例 self.__config = Configuration() # 获取框架主路径信息 self.__home_path = os.path.dirname(os.path.dirname(__file__))
def __init__(self): # 创建配置文件实例 config = Configuration() # 获取框架主路径信息 self.__home_path = os.path.dirname(os.path.dirname(__file__)) # 读取配置文件中的测试用例路径和测试用例执行规则 self.__test_path = config.getConfigValue("testCaseSuite", "test_path") logger.info("测试用例路径为: %s." % self.__test_path) self.__test_rule = config.getConfigValue("testCaseSuite", "test_rule") logger.info("测试用例执行规则为: %s." % self.__test_rule)
def __init__(self): # 创建配置文件实例 self.__config = Configuration() # 从配置文件获取数据库相关信息 self.__host = self.__config.getConfigValue("mysqlInfo", "host") self.__user = self.__config.getConfigValue("mysqlInfo", "user") self.__password = self.__config.getConfigValue("mysqlInfo", "password") self.__database = self.__config.getConfigValue("mysqlInfo", "database") # 连接数据库,获取操作游标 self.__db = self.__dbConnect() self.__cursor = self.__db.cursor()
def inform(best_price_info, config): text = InformText.get_text(config['informer_text_template'], best_price_info, config) subject = InformText.get_text(config['informer_subject_template'], best_price_info, config) # Check if sms text was not changed - not send new message if text == config['last_message'] or \ best_price_info['price'] == config['last_price']: return server_config = Configuration.get_category('SMTP_SERVER'); SimpleEmailSender.send({'to_email': config['informer_email'], 'to_name': config['informer_email_name'], 'server': server_config['server'], 'port': server_config['port'], 'username': server_config['username'], 'password': server_config['password'], 'from_email': server_config['email'], 'from_name': server_config['name'], 'text': text, 'subject': subject}) # Set new last information parameters in config config['last_message'] = text config['last_price'] = str(best_price_info['price'])
class App: logger = Logger() configuration = Configuration() def __init__(self): pass def run(self): pass
def __init__(self, plist_path): """ :param plist_path: plist 文件路径 """ super().__init__(plist_path) self.app_config = Configuration( self.content, conf=EnvEnum.CONFIGURATION.value) self.export_plist_path = f'{EnvEnum.SCRIPT_PATH.value}/plist/{self.app_config.method}.plist' self.icon_url = f'{EnvEnum.SCRIPT_URL.value}{self.app_config.icon_path}'
def __init__(self): """ 初始化发送邮件配置,从配置文件中,获取发邮件的基本信息:发件服务器,端口,账号,密码,收件人. """ # 创建配置文件实例 config = Configuration() # 获取框架主路径信息 self.__home_path = os.path.dirname(os.path.dirname(__file__)) # 读取发件服务器,端口,账号,密码,收件人 self.__smtpserver = config.getConfigValue("sendMailInfo", "smtp_server") self.__port = config.getConfigValue("sendMailInfo", "port") self.__sender = config.getConfigValue("sendMailInfo", "sender") self.__psw = config.getConfigValue("sendMailInfo", "password") self.__receivers = config.getConfigValue( "sendMailInfo", "receivers").split(";") # 得到收件人列表
class MySQLOperation: """ 将pymysql中的部分方法进行封装,处理对MySQL数据库的简单操作. """ def __init__(self): # 创建配置文件实例 self.__config = Configuration() # 从配置文件获取数据库相关信息 self.__host = self.__config.getConfigValue("mysqlInfo", "host") self.__user = self.__config.getConfigValue("mysqlInfo", "user") self.__password = self.__config.getConfigValue("mysqlInfo", "password") self.__database = self.__config.getConfigValue("mysqlInfo", "database") # 连接数据库,获取操作游标 self.__db = self.__dbConnect() self.__cursor = self.__db.cursor() def __dbConnect(self): """ 连接MySQL数据库. """ try: logger.info("开始连接MySQL数据库: {host}:{database}.".format( host=self.__host, database=self.__database)) db = pymysql.connect(self.__host, self.__user, self.__password, self.__database) logger.info("MySQL数据库连接成功: {host}:{database}.".format( host=self.__host, database=self.__database)) return db except Exception as msg: logger.error("连接MySQL数据库发生异常: %s." % msg) def dbClose(self): """ 关闭MySQL数据库连接. """ try: self.__db.close() logger.info("关闭数据库连接.") except Exception as msg: logger.error("关闭数据库连接发生异常: %s." % msg) def __sqlExecute(self, sql): """ 执行sql语句公共方法. :param sql: 需要处理的sql语句 """ try: self.__cursor.execute(sql) self.__db.commit() logger.info("执行SQL语句成功: %s." % sql) except Exception as msg: logger.error("执行SQL语句异常: %s." % msg) self.__db.rollback() def sqlCreate(self, sql): """ 处理sql创建语句. :param sql: 需要处理的sql创建语句 """ self.__sqlExecute(sql) def sqlSelect(self, sql): """ 处理sql查询语句. :param sql: 需要处理的sql查询语句 """ self.__sqlExecute(sql) def sqlInsert(self, sql): """ 处理sql插入语句. :param sql: 需要处理的sql插入语句 """ self.__sqlExecute(sql) def sqlUpdate(self, sql): """ 处理sql修改语句. :param sql: 需要处理的sql修改语句 """ self.__sqlExecute(sql) def sqlDelete(self, sql): """ 处理sql删除语句. :param sql: 需要处理的sql删除语句 """ self.__sqlExecute(sql) def sqlFetchOne(self, sql): """ 处理sql查询结果,获取下一个查询结果. :param sql: 需要处理的sql查询语句 :return: 下一个查询结果 """ try: self.sqlSelect(sql) if self.__cursor.fetchone(): logger.info("获取到查询结果.") return self.__cursor.fetchone() else: logger.info("没有查询到结果.") except Exception as msg: logger.error("获取查询结果出现异常: %s." % msg) def sqlFetchAll(self, sql): """ 处理sql查询结果,获取全部的查询结果. :param sql: 需要处理的sql查询语句 :return: 全部的查询结果 """ try: self.sqlSelect(sql) if self.__cursor.fetchall(): logger.info("获取到查询结果.") return self.__cursor.fetchall() else: logger.info("没有查询到结果.") except Exception as msg: logger.error("获取查询结果出现异常: %s." % msg)
__author__ = "sunxr" __version__ = "V1.2" # 集成测试用例执行和发送邮件功能 def executeCompatibilityTestcasesAndSendMail(thread_browser_type): """ 集成测试用例执行和发送邮件功能. :param thread_browser_type: 浏览器类型 """ TestCaseSuite().executeCompatibilityTestCases(thread_browser_type) SendMail().sendTestReport() # 实例化配置文件 config = Configuration() # 获取浏览器类型数量 browser_types = config.getConfigOptions(section="browserType") browser_counts = len(browser_types) # 获取浏览器具体类型 browsers = list() for browser_type in browser_types: browser = config.getConfigValue(section="browserType", option=browser_type) browsers.append(browser) # 利用多线程启动所有浏览器执行用例 threads = list()
#!/usr/bin/env python3 from libs.receiver_informer import ReceiverInformer from libs.configuration import Configuration # Get config config = Configuration.get() # Going thru receivers and process all receiver_number = 1 receiver_config_param_name = "RECEIVER_{0}" while(receiver_config_param_name.format(receiver_number) in config): # Receiver config group name receiver_config_group = receiver_config_param_name.format(receiver_number) # Notifing receiveNotifier = ReceiverInformer(config[receiver_config_group]); receiveNotifier.inform(); # Receiver number incrementing receiver_number += 1 # Save new values to the config Configuration.save()
class PagePubSelenium: """ 将Selenium中的页面公共操作进行封装,所有的页面操作类都要继承此类. """ def __init__(self, browser): """ 初始化测试浏览器. """ self.__driver = browser # 创建配置文件实例 self.__config = Configuration() # 获取框架主路径信息 self.__home_path = os.path.dirname(os.path.dirname(__file__)) def __getScreenShot(self): """ 截图操作,将图片保存在screenshots中. """ # 设置截图保存路径 screenshot_path = self.__home_path + '/screenshots/' # 通过时间戳定义图片名称 nowtime = time.strftime("%Y%m%d%H%M%S") screenshot_name = screenshot_path + nowtime + '.png' try: self.__driver.get_screenshot_as_file(screenshot_name) logger.info("截图已保存在screenshots中,名称为: %s." % screenshot_name) except Exception as msg: logger.error("截图异常: %s." % msg) def __catchExceptionAndGetScreenshot(self, msg): """ 捕获当前测试异常,并且截图保存在screenshots文件夹中. :param msg: 捕获的异常信息 """ logger.error(msg) self.__getScreenShot() def openURL(self): """ 打开待测网址. """ # 读取待测网址 url = self.__config.getConfigValue("testServerURL", "URL") self.__driver.implicitly_wait(30) logger.info("待测网址为: %s." % url) # 打开待测网址 try: self.__driver.get(url) logger.info("打开待测网址: %s." % url) except Exception as msg: self.__catchExceptionAndGetScreenshot("打开待测网址异常: %s." % msg) def openExceptedURL(self, expected_title='', timeout=30): """ 打开待测网址,并判断打开的网址title是否符合预期. :param expected_title: 待测网址期望title :param timeout: 查找元素等待时间 :return: 网址的title是否符合预期,布尔值 """ # 读取待测网址 url = self.__config.getConfigValue("testServerURL", "URL") logger.info("待测网址为: %s." % url) # 打开待测网址 try: self.__driver.get(url) logger.info("打开待测网址: %s." % url) # EC.title_is 判断当前页面的title是否完全等于预期字符串,返回布尔值 return WebDriverWait(self.__driver, timeout, 1).until(EC.title_is(expected_title)) except Exception as msg: self.__catchExceptionAndGetScreenshot("打开待测网址异常: %s." % msg) def maximizeWindow(self): """ 浏览器窗口最大化. """ try: self.__driver.set_window_size("1024", "768") self.__driver.maximize_window() logger.info("浏览器最大化.") except Exception as msg: self.__catchExceptionAndGetScreenshot("浏览器最大化异常: %s." % msg) def back(self): """ 浏览器执行返回操作. """ try: self.__driver.back() logger.info("浏览器执行返回操作.") except Exception as msg: self.__catchExceptionAndGetScreenshot("浏览器执行返回操作异常: %s." % msg) def forward(self): """ 浏览器执行前进操作. """ try: self.__driver.forward() logger.info("浏览器执行前进操作.") except Exception as msg: self.__catchExceptionAndGetScreenshot("浏览器执行前进操作异常: %s." % msg) def closeCurrentBrowser(self): """ 关闭当前浏览器. """ try: self.__driver.close() logger.info("关闭当前浏览器.") except Exception as msg: self.__catchExceptionAndGetScreenshot("关闭当前浏览器异常: %s." % msg) def quitAllBrowsers(self): """ 退出驱动并关闭所有关联的窗口. """ try: self.__driver.quit() logger.info("关闭所有浏览器进程.") except Exception as msg: self.__catchExceptionAndGetScreenshot("关闭所有浏览器进程异常: %s." % msg) @staticmethod def pageSleep(timeout=5): """ 页面等待,单位:秒. :param timeout: seconds """ time.sleep(timeout) logger.info("页面等待: %s秒." % timeout) def findElement(self, locator, timeout=3): """ 定位单个元素方法封装. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 :return: 定位到的元素element """ try: # EC.presence_of_element_located 判断某个元素是否被加到了DOM树里,并不代表该元素一定可见 element = WebDriverWait(self.__driver, timeout, 1).until( EC.presence_of_element_located(locator)) logger.info("定位到指定元素: {key} => {value}.".format(key=locator[0], value=locator[1])) return element except TimeoutException: self.__catchExceptionAndGetScreenshot( "没有找到指定元素: {key} => {value}.".format(key=locator[0], value=locator[1])) except Exception as msg: self.__catchExceptionAndGetScreenshot("定位元素其他异常: %s." % msg) def findElements(self, locator, timeout=3): """ 定位一组元素方法封装. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 :return: 定位到的一组元素elements """ try: # EC.presence_of_all_elements_located 判断是否至少有1个元素存在于DOM树 elements = WebDriverWait(self.__driver, timeout, 1).until( EC.presence_of_all_elements_located(locator)) # elements = self.__driver.find_elements(locator) logger.info("定位到指定元素: {key} => {value}.".format(key=locator[0], value=locator[1])) return elements except TimeoutException: self.__catchExceptionAndGetScreenshot( "没有找到指定元素: {key} => {value}.".format(key=locator[0], value=locator[1])) except Exception as msg: self.__catchExceptionAndGetScreenshot("定位元素其他异常: %s." % msg) def click(self, locator, timeout=3): """ 鼠标左键点击元素. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 """ try: element = self.findElement(locator=locator, timeout=timeout) element.click() logger.info("鼠标左键点击指定元素: {key} => {value}.".format( key=locator[0], value=locator[1])) except Exception as msg: self.__catchExceptionAndGetScreenshot("鼠标左键点击指定元素异常: %s." % msg) def moveMouseToElement(self, locator, timeout=3): """ 鼠标悬停在指定元素上. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 """ try: mouse = self.findElement(locator=locator, timeout=timeout) ActionChains(self.__driver).move_to_element(mouse).perform() logger.info("鼠标悬停在指定元素上: {key} => {value}.".format( key=locator[0], value=locator[1])) except Exception as msg: self.__catchExceptionAndGetScreenshot("鼠标悬停在指定元素上异常: %s." % msg) def clear(self, locator, timeout=3): """ 清空文本框内容. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 """ try: element = self.findElement(locator=locator, timeout=timeout) element.clear() logger.info("清空指定文本框元素中内容,元素为: {key} => {value}.".format( key=locator[0], value=locator[1])) except Exception as msg: self.__catchExceptionAndGetScreenshot("清空指定文本框元素中内容异常: %s." % msg) def sendText(self, locator, text, timeout=3): """ 发送文本到指定文本框. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 :param text: 输入的内容 """ try: self.clear(locator=locator, timeout=timeout) element = self.findElement(locator=locator, timeout=timeout) element.send_keys(text) logger.info("在指定文本框元素中输入文本,元素为: {key} => {value}.".format( key=locator[0], value=locator[1])) except Exception as msg: self.__catchExceptionAndGetScreenshot("在指定文本框中输入文本异常: %s." % msg) def selectByIndex(self, locator, index, timeout=3): """ 根据索引选择下拉框中内容. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param index: 下拉框索引 :param timeout: 查找元素等待时间 """ try: select = self.findElement(locator=locator, timeout=timeout) Select(select).select_by_index(index=index) logger.info("根据索引选择下拉框中指定内容,指定索引为: %s." % str(index)) except Exception as msg: self.__catchExceptionAndGetScreenshot("根据索引选择下拉框中指定内容异常: %s." % msg) def selectByValue(self, locator, value, timeout=3): """ 根据元素value值选择下拉框中内容. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param value: 下拉框索引 :param timeout: 查找元素等待时间 """ try: select = self.findElement(locator=locator, timeout=timeout) Select(select).select_by_value(value=value) logger.info("根据元素value值选择下拉框中指定内容,指定value值为: %s." % value) except Exception as msg: self.__catchExceptionAndGetScreenshot( "根据元素value值选择下拉框中指定内容异常: %s." % msg) def selectByText(self, locator, text, timeout=3): """ 根据下拉选项内容选择下拉框中内容. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param text: 下拉选项内容 :param timeout: 查找元素等待时间 """ try: select = self.findElement(locator=locator, timeout=timeout) Select(select).select_by_visible_text(text=text) logger.info("根据下拉选项内容选择下拉框中指定内容,指定内容为: %s." % text) except Exception as msg: self.__catchExceptionAndGetScreenshot("根据下拉选项内容选择下拉框中指定内容异常: %s." % msg) def getTitle(self): """ 获取网页标题. :return: 网页标题 """ logger.info("获取当前网页标题: %s." % self.__driver.title) return self.__driver.title def getElementText(self, locator, timeout=3): """ 获取元素文本. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 :return: 元素文本 """ try: element = self.findElement(locator=locator, timeout=timeout) element_text = element.text logger.info("获取到的元素文本为: %s." % element_text) return element_text except Exception as msg: self.__catchExceptionAndGetScreenshot("获取元素文本异常: %s." % msg) def getElementAttribute(self, name, locator="", element="", timeout=3): """ 获取元素指定属性的值. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param element: 已经定位到的元素 :param locator: ("id", "xxx") :param name: 属性名称 :param timeout: 查找元素等待时间 :return: 属性值 """ try: if locator == "": value = element.get_attribute(name) logger.info("获取到的元素属性值为: {name} => {value}.".format( name=name, value=value)) return value else: element = self.findElement(locator=locator, timeout=timeout) value = element.get_attribute(name) logger.info("获取到的元素属性值为: {name} => {value}.".format( name=name, value=value)) return value except Exception as msg: self.__catchExceptionAndGetScreenshot("获取元素属性值异常: %s." % msg) def switchToFrame(self, locator, timeout=3): """ 切换网页内嵌iframe框架. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 """ try: iframe = self.findElement(locator=locator, timeout=timeout) self.__driver.switch_to.frame(iframe) logger.info("切换到指定的iframe: %s." % iframe) except Exception as msg: self.__catchExceptionAndGetScreenshot("切换到指定的iframe异常: %s." % msg) def switchToDefaultContent(self): """ 释放iframe重新回到主页面上. """ self.__driver.switch_to.default_content() logger.info("释放iframe重新回到主页面上.") def getCurrentWindowHandle(self): """ 获取当前窗口的句柄. :return: 当前窗口的句柄 """ logger.info("获取当前窗口的句柄,句柄值为: %s." % str(self.__driver.current_window_handle)) return self.__driver.current_window_handle def getAllWindowHandles(self): """ 获取所有窗口的句柄. :return: 所有窗口的句柄,list """ logger.info("获取所有窗口的句柄.") return self.__driver.window_handles def switchToWindow(self, handle_value): """ 切换到指定窗口. :param handle_value: 指定窗口的句柄 """ try: self.__driver.switch_to.window(handle_value) logger.info("切换到指定窗口,窗口句柄为: %s." % str(handle_value)) except Exception as msg: logger.error("切换到指定窗口异常: %s." % msg) def switchToAlert(self): """ 切换到弹出窗体. :return: 弹窗alert """ try: alert = self.__driver.switch_to.alert if alert.text: logger.info("切换到弹窗.") return alert else: self.__catchExceptionAndGetScreenshot("没有发现弹窗.") except NoAlertPresentException: self.__catchExceptionAndGetScreenshot("没有发现弹窗.") def getAlertText(self): """ 获取弹窗中的文本信息. :return: 文本信息 """ alert = self.switchToAlert() logger.info("获取弹窗中的文本信息,内容为: %s." % alert.text) return alert.text def alertAccept(self): """ 点击弹窗中的确认. """ alert = self.switchToAlert() alert.accept() logger.info("点击弹窗中的确认按钮.") def alertCancel(self): """ 取消弹窗. """ alert = self.switchToAlert() alert.dismiss() logger.info("取消弹窗.") def alertSendkeys(self, text): """ 弹窗的文本框中输入信息. :param text: 需要输入的信息 """ try: alert = self.switchToAlert() alert.send_keys(text) logger.info("在弹窗的文本框中输入信息.") except Exception as msg: self.__catchExceptionAndGetScreenshot("在弹窗的文本框中输入信息异常: %s." % msg) def isTitle(self, expected_title, timeout=3): """ 验证网页title是否跟期望完全一样. :param expected_title: 期望title值 :param timeout: 查找元素等待时间 :return: 是否一样,布尔值 """ try: # EC.title_is 判断当前页面的title是否完全等于预期字符串,返回布尔值 result = WebDriverWait(self.__driver, timeout, 1).until(EC.title_is(expected_title)) logger.info("验证title是否为: %s." % expected_title) return result except Exception as msg: self.__catchExceptionAndGetScreenshot("验证title异常: %s." % msg) def isTitleContains(self, expected_title, timeout=3): """ 验证网页title是否包含期望值. :param expected_title: 期望包含title值 :param timeout: 查找元素等待时间 :return: 是否包含,布尔值 """ try: # EC.title_contains 判断当前页面的title是否包含预期字符串,返回布尔值 result = WebDriverWait(self.__driver, timeout, 1).until(EC.title_contains(expected_title)) logger.info("验证title是否包含: %s." % expected_title) return result except Exception as msg: self.__catchExceptionAndGetScreenshot("验证title异常: %s." % msg) def isElementSelected(self, locator, timeout=3): """ 验证元素是否被选中. locator支持: "id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector". :param locator: ("id", "xxx") :param timeout: 查找元素等待时间 :return: 是否选中,布尔值 """ try: expected_element = self.findElement(locator=locator, timeout=timeout) result = expected_element.is_selected() logger.info("验证元素是否被选中: {key} => {value}.".format( key=locator[0], value=locator[1])) return result except Exception as msg: self.__catchExceptionAndGetScreenshot("验证元素是否被选中异常: %s." % msg)
def load_configs(self): for cfg in self.config.get_site_configs(): self.db.add_site_configuration(cfg) def __init__(self, config): Runnable.__init__(self) self.db = Database() self.config = config self.load_configs() self.api = API(callback_add=self.callback_add_info_site) self.driver = WebDriver.build(config) def callback_add_info_site(self, config): self.info_sites.append(config) # todo filter if not already in list return True def _run(self): for i in self.info_sites: i.check() def stop(self): self.api.stop() if __name__ == '__main__': fetch_webdriver() c = Configuration.parse() c.set_webdriver_headless(False) Informer(c).run()