def after_test(self, methodName, isSucceed): ends = formated_time("%Y-%m-%d %H:%M:%S:%f") captureName = "" if (isSucceed): self.log4py.info("案例 【" + str(self.className) + "." + methodName + "】 运行通过!") else: dateTime = formated_time("-%Y%m%d-%H%M%S%f") captureName = self.seProperties.capturePath + str(self.className)+"."+methodName+str(dateTime)+".png" self.capture_screenshot(captureName) self.log4py.error("案例 【" + str(self.className) + "." + methodName+ "】 运行失败,请查看截图快照:" + captureName) self.log4py.info("======" + ends + ":案例【" + str(self.className) + "." + methodName+ "】结束======") afterTestStops = time.time() self.log4py.info("======本次案例运行消耗时间 " + str(afterTestStops - self.__beforeTestStarts) + " 秒!======") return captureName
def after_suite(self): ends = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__afterSuiteStops = time.time() self.log4py.info("======" + ends + ":测试集结束======") self.log4py.info("======本次测试集运行消耗时间 " + str(self.__afterSuiteStops - self.__beforeSuiteStarts) + " 秒!======")
class WebDriverDoBeforeTest(object): def __init__(self, clzss): """ :param clzss: 获取脚本的文件名和class名 """ self.driver = None self.className = clzss.__class__.__module__ + "." + clzss.__class__.__name__ self.seProperties = WebConfingGetter().properties self.log4py = LoggingPorter() self.__beforeSuiteStarts = 0 self.__beforeClassStarts = 0 self.__beforeTestStarts = 0 self.init = None def get_api_driver(self): self.init = InitWebDriver(self.seProperties) try: resp = requests.get(self.seProperties.baseURL) if resp.status_code != 200: self.log4py.error("浏览器实例化driver失败,请检查你的被测试服务是否启动或baseURL是否设置正确: {}".format(self.seProperties.baseURL)) return None except exceptions.ConnectionError as e: self.log4py.error("浏览器实例化driver失败,请检查你的被测试服务是否启动或baseURL是否设置正确: {}".format(self.seProperties.baseURL)) return None self.driver = self.init.run_browser() if self.driver is None: self.log4py.error("浏览器实例化driver失败,请重新检查驱动及启动参数") return None return SeleniumBaseApi(self.driver, self.seProperties) def before_suite(self): begins = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__beforeSuiteStarts = time.time() self.log4py.info("======" + begins + ":测试集开始======") def after_suite(self): ends = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__afterSuiteStops = time.time() self.log4py.info("======" + ends + ":测试集结束======") self.log4py.info("======本次测试集运行消耗时间 " + str(self.__afterSuiteStops - self.__beforeSuiteStarts) + " 秒!======") def before_class(self): begins = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__beforeClassStarts = time.time() self.log4py.info("======" + str(begins) + ":测试【" + str(self.className) + "】开始======") def after_class(self): # 如果执行了case,必然已经启动了webdriver,在这里做一次关闭操作 try: self.init.stop_web_driver() except Exception, e: self.log4py.error("after class with stoping web driver happend error") ends = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__afterClassStops = time.time() self.log4py.info("======" + str(ends) + ":测试【" + str(self.className) + "】结束======") self.log4py.info("======本次测试运行消耗时间 " + str(self.__afterClassStops - self.__beforeClassStarts) + " 秒!======")
class MobileDriverBeforeTest(object): def __init__(self): self.driver = None self.className = None self.mcg = MobileConfigGetter() self.log4py = LoggingPorter() self.__beforeSuiteStarts = 0 self.__beforeClassStarts = 0 self.__beforeTestStarts = 0 self.init = None def get_api_driver(self, sno): """ 通过sno获取对应的手机的驱动及其实例化的api对象 :param sno: :return: """ self.init = InitAppiumDriver(self.mcg) self.driver = self.init.get_android_driver(sno) if self.driver is None: self.log4py.error("appium实例化driver失败,请重新检查驱动及启动参数") return None return AppiumBaseApi(self.driver, self.mcg.properties) def before_suite(self): begins = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__beforeSuiteStarts = time.time() self.log4py.info("======" + begins + ":测试集开始======") def after_suite(self): ends = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__afterSuiteStops = time.time() self.log4py.info("======" + ends + ":测试集结束======") self.log4py.info("======本次测试集运行消耗时间 " + str(self.__afterSuiteStops - self.__beforeSuiteStarts) + " 秒!======") def before_class(self): begins = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__beforeClassStarts = time.time() self.log4py.info("======" + str(begins) + ":测试【" + str(self.className) + "】开始======") def after_class(self): # 如果执行了case,必然已经启动了webdriver,在这里做一次关闭操作 try: self.driver.quit() except Exception, e: self.log4py.error( "after class with stoping web driver happend error") ends = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__afterClassStops = time.time() self.log4py.info("======" + str(ends) + ":测试【" + str(self.className) + "】结束======") self.log4py.info("======本次测试运行消耗时间 " + str(self.__afterClassStops - self.__beforeClassStarts) + " 秒!======")
def capture(self, name): ''' * 截取屏幕截图并保存到指定路径 * @param name:保存屏幕截图名称 * @return 无 ''' time.sleep(3) dateTime = formated_time("-%Y%m%d-%H%M%S-%f") captureName = self.seProperties.capturePath + name + dateTime+".png" self.capture_screenshot(captureName) self.log4py.debug("请查看截图快照:" + captureName)
def operation_check(self, method_name, is_succeed): ''' * public method for handle assertions and screenshot. * @param isSucceed:if your operation success ''' if is_succeed: self.log4py.info("method 【" + method_name + "】 运行通过!") else: date_time = formated_time("-%Y%m%d-%H%M%S%f") capture_name = self.capturePath + method_name + date_time + ".png" self.capture_screenshot(capture_name) self.log4py.error("method 【" + method_name + "】 运行失败,请查看截图快照:" + capture_name)
def operation_check(self, method_name, is_succeed): """ 就是校验方法时候执行成功,如果没有就进行截图操作 :param method_name: 执行的方法 :param is_succeed: 判断条件 :return: """ if is_succeed: self.log4py.info("method 【" + method_name + "】 运行通过!") else: dateTime = formated_time("-%Y%m%d-%H%M%S%f") captureName = self.capturePath + method_name + dateTime + ".png" self.capture_screenshot(captureName) self.log4py.error("method 【" + method_name + "】 运行失败,请查看截图快照:" + captureName)
def html_reporter(): logger = LoggingPorter() fc = FileInspector() pro_path = fc.get_project_path() boolean = fc.is_has_file("owl-framework.ini") if boolean: inipath = fc.get_file_abspath() cf = ConfigReader(inipath) htmlrp_path = cf.get_value("ResultPath", "htmlreportPath") htmreportl_abs_path = os.path.join(pro_path, htmlrp_path) timecurrent = formated_time("%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))
def befor_test(self, methodName): begins = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__beforeTestStarts = time.time() self.log4py.info("======" + begins + ":案例【" + str(self.className) + "." + methodName+ "】开始======")
def before_class(self): begins = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__beforeClassStarts = time.time() self.log4py.info("======" + str(begins) + ":测试【" + str(self.className) + "】开始======")
def before_suite(self): begins = formated_time("%Y-%m-%d %H:%M:%S:%f") self.__beforeSuiteStarts = time.time() self.log4py.info("======" + begins + ":测试集开始======")