def __init__(self): """配置日志数据""" self.common = Common() try: log_path = self.common.get_result_path("result.log") # 生成日志文件 self.logger = logging.getLogger() # 定义logger self.logger.setLevel(logging.DEBUG) # 定义输出等级 sh = logging.StreamHandler() # 日志输出到屏幕控制台 sh.setLevel(logging.DEBUG) # 设置日志等级 fh = logging.FileHandler(log_path, encoding="utf-8") # 输出日志信息到log_path fh.setLevel(logging.DEBUG) # 设置日志等级 # 设置handler的格式 formatter = logging.Formatter( '%(asctime)s %(filename)s %(module)s [line:%(lineno)d] %(levelname)s %(message)s' ) # 设置handler的格式对象 sh.setFormatter(formatter) fh.setFormatter(formatter) # 将handler增加到logger中 self.logger.addHandler(sh) self.logger.addHandler(fh) except Exception as e: raise Exception("Log.__init__异常 %s" % e)
def __init__(self): try: self.config = ReadConfig() self.log = MyLog().get_log().logger self.common = Common() except Exception as e: self.log.error(e) raise Exception("出现异常!")
def __init__(self): try: self.log = MyLog.get_log().logger self.common = Common() self.api_cases_path, self.api_cases_dict = self.common.get_api_cases( ) except Exception as e: self.log.error(e) raise Exception("出现异常!")
def __init__(self): self.common = Common() self.my_log = MyLog() self.send_email = SendEmail() self.cases = ProduceCases() self.cases.produce_case() # 自动生成接口测试用例 self.path = self.common.get_result_path() # 获取报告存储路径 self.log = self.my_log.get_log().logger # log日志 self.suit = unittest.TestSuite() # 测试套件(定义执行顺序)
class Run(object): def __init__(self): self.common = Common() self.my_log = MyLog() self.send_email = SendEmail() self.cases = ProduceCases() self.cases.produce_case() # 自动生成接口测试用例 self.path = self.common.get_result_path() # 获取报告存储路径 self.log = self.my_log.get_log().logger # log日志 self.suit = unittest.TestSuite() # 测试套件(定义执行顺序) # 方式一: def add_api_test(self): """添加api测试用例""" from NT.cases.api.test_cases import APITestCases # 生成所有接口测试用例后导入TestCases类 for origin, sheet_dict in self.common.api_cases_dict.items(): for case_name, case_params in sheet_dict.items(): self.suit.addTest(APITestCases("test_%s" % case_name)) # 方式一: def add_ui_test(self): """添加ui测试用例""" self.suit.addTest(WebLogin("test_web_login")) # web登录 self.suit.addTest(AppLogin("test_app_login")) # app登录 self.suit.addTest(AppOperation("test_app_operation")) # 技术操作考核 self.suit.addTest(AppExams("test_app_exams")) # 考试 self.suit.addTest(AppPractice("test_app_practice")) # 练习 self.suit.addTest(AppSurvey("test_app_survey")) # 问卷调查 self.suit.addTest(AppCurriculum("test_app_curriculum")) # 课程学习 self.suit.addTest(AppCourseware("test_app_courseware")) # 课件学习 # 方式二: def add_cases(self): """添加所有测试用例""" cases_path = self.common.get_path("cases") # 用例路径 cases_file = "test*.py" # 用例文件或用例模式 discover = unittest.defaultTestLoader.discover(cases_path, pattern=cases_file, top_level_dir=None) return discover # 方式二: def run_cases(self, case): """执行用例并生成报告""" result = BeautifulReport(case) result.report(log_path=self.path, filename="NT_测试报告.html", description='NT自动化测试')
def __init__(self): try: self.common = Common() # 实例化一个common调用公用函数 self.log = MyLog().get_log().logger # 实例化一个log打印日志 self.config = ReadConfig() # 实例化一个read_config读取email的配置信息 self.msg = email.mime.multipart.MIMEMultipart( 'alternative') # 实例化一个email发送email self.log_dir = self.common.get_result_path() # 获取存储日志的时间目录 self.principal_name_list = [] # 错误用例负责人list self.zip_path = self.log_dir + ".zip" # 设置存放zip的路径 self.result = False # 发送结果标志 self.num = 0 # 发送失败后重试次数 except Exception as e: self.log.error(e) raise Exception("SendEmail.__init__异常!")
def setUp(self): try: self.common = Common() self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!")
class Log(object): _instance_lock = threading.Lock() # 设置单例锁 def __new__(cls, *args, **kwargs): """单例模式(支持多线程)""" if not hasattr(cls, "_instance"): with cls._instance_lock: if not hasattr(cls, "_instance"): cls._instance = object.__new__(cls) return cls._instance def __init__(self): """配置日志数据""" self.common = Common() try: log_path = self.common.get_result_path("result.log") # 生成日志文件 self.logger = logging.getLogger() # 定义logger self.logger.setLevel(logging.DEBUG) # 定义输出等级 sh = logging.StreamHandler() # 日志输出到屏幕控制台 sh.setLevel(logging.DEBUG) # 设置日志等级 fh = logging.FileHandler(log_path, encoding="utf-8") # 输出日志信息到log_path fh.setLevel(logging.DEBUG) # 设置日志等级 # 设置handler的格式 formatter = logging.Formatter( '%(asctime)s %(filename)s %(module)s [line:%(lineno)d] %(levelname)s %(message)s' ) # 设置handler的格式对象 sh.setFormatter(formatter) fh.setFormatter(formatter) # 将handler增加到logger中 self.logger.addHandler(sh) self.logger.addHandler(fh) except Exception as e: raise Exception("Log.__init__异常 %s" % e)
def __init__(self): try: self.config_path = Common.get_path("data", "config.ini") # 拼接配置文件路径 self.cf = configparser.ConfigParser() # 读取配置文件的对象 # 判断文件是否存在 if not os.path.isfile(self.config_path): raise Exception("文件%s不存在!" % self.config_path) # 判断是否有BOM头 bom = b'\xef\xbb\xbf' # BOM头多出的内容 judge_bom = lambda s: True if s == bom else False # 定义一个匿名函数 with open(self.config_path, 'rb') as fr: if judge_bom(fr.read(3)): # 读取头3个字节进行判断 data = fr.read() with open(self.config_path, 'wb') as fw: fw.write(data) # 利用二进制重新写入后BOM头就消失了 # 读取文件 self.cf.read(self.config_path, encoding="utf-8") except Exception as e: raise Exception("ReadConfig.__init__异常 %s" % e)
class BasePage(object): """页面元素基本操作,page module""" web_case_num = 0 # web用例编号 web_driver = None # 用于标记web用例 app_driver = None # 用于标记APP用例 current_driver = "" # 用于标记当前是web端还是APP端的用例 _instance_lock = threading.Lock() # 设置单例锁 def __new__(cls, *args, **kwargs): """单例模式(支持多线程)""" if not hasattr(cls, "_instance"): with cls._instance_lock: if not hasattr(cls, "_instance"): cls._instance = object.__new__(cls) return cls._instance def __init__(self): try: self.config = ReadConfig() self.log = MyLog().get_log().logger self.common = Common() except Exception as e: self.log.error(e) raise Exception("出现异常!") def open_app(self): """打开app""" try: # 读取app参数 system = self.config.get_app_param("system") # 系统类型 udid = self.config.get_app_param("udid") # 手机udid version = self.config.get_app_param("version") # 手机系统版本 app_package = self.config.get_app_param("app_package") # 待测app包名 app_activity = self.config.get_app_param( "app_activity") # 待测app的activity名 # app_address = self.config.get_app_param("app_address") # app安装包路径 # android_process = self.config.get_app_param("androidProcess") # 小程序线程名 desired_caps = { 'platformName': system, 'platformVersion': version, 'automationName': 'appium', 'deviceName': 'udid', 'udid': udid, 'newCommandTimeout': 60, 'appActivity': app_activity, 'appPackage': app_package, 'unicodeKeyboard': True, 'resetKeyboard': True, 'setWebContentsDebuggingEnabled': True, 'recreateChromeDriverSessions': True, 'noReset': True, # 'app': app_address, # 'chromeOptions': {'androidProcess': android_process} } # 操作APP端元素的webdriver实例 self.app_driver = appium.webdriver.Remote( 'http://127.0.0.1:4723/wd/hub', desired_caps) self.current_driver = "app_driver" # 标记为APP端用例 time.sleep(10) self.switch_context() # H5时需要切换context except Exception as e: self.log.error(e) raise Exception("打开app时异常!") def open_browser(self, browser="chrome"): """打开浏览器""" try: if browser == "chrome" or browser == "Chrome": driver = selenium.webdriver.Chrome() elif browser == "firefox" or browser == "Firefox" or browser == "FireFox" or browser == "ff": driver = selenium.webdriver.Firefox() elif browser == "ie" or browser == "IE" or browser == "internet explorer": driver = selenium.webdriver.Ie() else: raise Exception( self.log.error("没有找到浏览器 %s, 你可以输入'Chrome,Firefox or Ie'" % browser)) self.web_driver = driver # 操作web端元素的webdriver实例 self.current_driver = "web_driver" # 标记为web端用例 self.web_driver.maximize_window() except Exception as e: self.log.error(e) raise Exception("打开%s浏览器时异常!" % browser) def get(self, url, element): """打开web端URL, element用于等待页面加载完成""" try: if url != "" or url != " ": self.web_driver.get(url) self.wait_elem(element) # 等待元素出现 else: raise Exception("URL地址错误!") except Exception as e: self.log.error(e) raise Exception("打开网址时异常!") def refresh(self): """刷新页面""" if self.current_driver == "web_driver": self.web_driver.refresh() else: self.swipe_down() def back(self): """返回上一页""" try: if self.current_driver == "web_driver": self.web_driver.back() else: self.app_driver.keyevent(4) time.sleep(0.5) except Exception as e: self.log.error(e) raise Exception("返回时异常!") def quit(self): """退出程序""" try: if self.current_driver == "web_driver": self.web_driver.quit() else: # H5时用 self.app_driver.switch_to.context("NATIVE_APP") # input_name = self.app_driver.active_ime_engine # self.log.debug("当前输入法:%s" % input_name) input_list = self.app_driver.available_ime_engines # self.log.debug("现有输入法:%s" % input_list) self.app_driver.activate_ime_engine(input_list[0]) # input_name = self.app_driver.active_ime_engine # self.log.debug("更改当前输入法为:%s" % input_name) self.app_driver.quit() except Exception as e: self.log.error(e) raise Exception("退出程序时异常!") def click_elem_tag(self, elements, tag=0, roll=False, t=1): """根据元素下标点击元素""" for i in range(1, 6, +1): # 操作失败后重试 try: if roll: # 是否需要滚动页面 self.location(elements, tag) # app端页面上下微调 elem = self.find_elements_tag(elements, tag) elem.click() time.sleep(t) break except Exception as e: time.sleep(1) self.log.debug("等待 %s s %s" % (i, e)) else: self.log.error("%s元素未出现!" % str(elements)) raise Exception("点击元素时异常!") def input_tag(self, elements, text, tag=0, roll=False): """输入文本""" for i in range(1, 6, +1): # 操作失败后重试 try: if roll: # 是否需要滚动页面 self.location(elements, tag) # app端页面上下微调 elem = self.find_elements_tag(elements, tag) elem.clear() elem.send_keys(text) break except Exception as e: time.sleep(1) self.log.debug("等待 %s s %s" % (i, e)) else: self.log.error("%s元素未出现!" % str(elements)) raise Exception("输入文本时异常!") def get_text(self, *args, text="", tag=0): """获取文本内容,可多条,text为获取内容的标题/属性""" value = "" # 文本内容 for param in args: for i in range(1, 6, +1): # 操作失败后重试 try: elem = self.find_elements_tag(param, tag) value = elem.text # web端获取文本内容 if value == "": value = elem.get_attribute("name") # app获取文本内容 if value != "": self.log.debug("%s%s" % (text, value)) break except Exception as e: time.sleep(1) self.log.debug("等待 %s s %s" % (i, e)) else: self.log.error("%s元素未出现!" % str(param)) raise Exception("获取元素文本时异常!") return value def switch_context(self, tag=1): """切换环境,tag=0时为android原生context""" try: contexts = self.app_driver.contexts # 获取当前所有context self.log.debug("contexts:%s" % contexts) if len(contexts) != 1: # 需要切换context self.app_driver.switch_to.context(contexts[tag]) # 切换context self.log.debug("切换context") context = self.app_driver.current_context # 获取当前context self.log.debug("current_context: %s" % context) except Exception as e: self.log.error(e) raise Exception("切换context时异常!") def switch_handle(self, element): """切换句柄""" try: handles = self.app_driver.window_handles if len(handles) != 1: # 需要切换句柄 self.log.debug("handles:%s" % handles) self.app_driver.switch_to.window(handles[-1]) if self.displayed(element): # 判断该句柄下是否有该元素 self.log.debug("切换handle") return self.displayed(element) except Exception as e: self.log.error(e) raise Exception("切换handle时异常!") def home_page_to(self, module): """首页待办事项进入功能模块""" module_elem = ("xpath", "//span[contains(text(), '%s')]" % module ) # 待办事项中的模块标签 result = False try: if self.displayed(module_elem): self.log.debug("从首页的待办事项进入%s" % module) self.screen_shot() self.click_elem_tag(module_elem) self.screen_shot() result = True return result except Exception as e: self.log.error(e) raise Exception("从首页的待办事项进入%s时异常!" % module) def back_to(self, *args): """返回(首页)或指定元素页面(须该页独有元素)""" try: home_menu = ("css_selector", "span.tab-title.ng-binding" ) # 首页底部menu username_input = ("css_selector", "input[type='text']" ) # 用户名输入框定位信息 menu_elem = () if args != (): menu_elem = args[0] self.log.debug("返回") i = 1 while i <= 5: # 最多返回5级页面 if self.displayed(username_input): # 判断是否处于登录页 raise Exception("登录失败!") self.back() self.screen_shot() if args == (): # 返回首页 if self.switch_handle(home_menu): self.click_elem_tag(home_menu) break elif args != (): # 返回指定元素页面 if self.switch_handle(menu_elem): break self.log.debug("返回:%s" % i) i += 1 else: raise Exception("返回时异常!") except Exception as e: self.log.error(e) raise Exception("返回时异常!") def popup(self): """获取弹框信息,点击按钮""" popup_title = ("css_selector", "h3.popup-title.ng-binding") # 提示框title popup_info = ("css_selector", "div.popup-body") # 提示信息 popup_ok_button = ("css_selector", "button.button.ng-binding.button-calm") # 确定按钮 try: n = len(self.find_elements(popup_title)) # self.log.debug("弹框数量:%s" % n) self.get_text(popup_title, popup_info, tag=n - 1) self.screen_shot() self.click_elem_tag(popup_ok_button, tag=n - 1) self.screen_shot() except Exception as e: self.log.error(e) raise Exception("操作弹框时异常!") def roll(self, elements): """web端页面下滑""" elem = self.find_elements_tag(elements) selenium.webdriver.ActionChains( self.web_driver).move_to_element(elem).perform() time.sleep(1) self.log.debug("滚动页面!") def screen_shot(self): """截图""" try: current_time = str(self.common.get_now_time()) # 获取当前时间 func_name = sys._getframe().f_back.f_code.co_name # 获取调用函数名 line_number = sys._getframe().f_back.f_lineno # 获取调用行号 path = self.common.get_result_path( case_name, "%s %s %s.png" % (current_time, func_name, line_number)) if self.current_driver == "web_driver": # web端直接截图 self.web_driver.get_screenshot_as_file(path) else: # 移动端截图 contexts = self.app_driver.contexts # 获取所有的context current_context = self.app_driver.current_context # 获取当前的context if current_context == contexts[0]: # 如果是android原生环境直接截图 self.app_driver.get_screenshot_as_file(path) else: # 如果是H5页面先切换到android原生环境再截图 self.app_driver.switch_to.context(contexts[0]) self.app_driver.get_screenshot_as_file(path) self.app_driver.switch_to.context( contexts[1]) # 截完图后回到原来的context except Exception as e: self.log.error(e) raise Exception("截图保存时异常!") def case_start(self, principal, api_case_name="", api_case_num=0): """用例开始,参数为负责人姓名,api测试名,api测试编号""" try: global case_name # 获取调用函数名作为截图文件夹名 if api_case_name == "" and api_case_num == 0: case_name = sys._getframe().f_back.f_code.co_name self.web_case_num += 1 self.log.debug("web用例%s:%s,负责人:%s" % (self.web_case_num, case_name, principal)) else: case_name = api_case_name # 将全局变量case_name重新赋值 self.log.debug("api用例%s:%s,负责人:%s" % (api_case_num, api_case_name, principal)) except Exception as e: self.log.error(e) raise Exception("用例开始时异常!") def case_end(self): """用例结束""" self.log.debug("*" * 100 + "\n") # "*"号不可改,用于提取用例失败的日志 def case_pass(self): """用例通过""" self.log.debug("=" * 10 + "%s: pass!" % case_name + "=" * 10) def case_failed(self): """用例失败""" self.log.debug("=" * 10 + "%s: failed!" % case_name + "=" * 10) # "failed!"不可改,用于标记用例失败的日志 def find_elements_tag(self, elements, tag=0): """查找元素(一个具体的元素点击和输入时定位)""" try: key = elements[0] # 定位方式 value = elements[1] # 值 if self.current_driver == "web_driver": # web定位 if key == "css_selector": elem = self.web_driver.find_elements_by_css_selector( value)[tag] elif key == "xpath": elem = self.web_driver.find_elements_by_xpath(value)[tag] elif key == "id": elem = self.web_driver.find_elements_by_id(value)[tag] elif key == "name": elem = self.web_driver.find_elements_by_name(value)[tag] elif key == "class": elem = self.web_driver.find_elements_by_class_name( value)[tag] elif key == "link_text": elem = self.web_driver.find_elements_by_link_text(value) elif key == "partial_link_text": elem = self.web_driver.find_elements_by_partial_link_text( value) elif key == "tag_name": elem = self.web_driver.find_elements_by_tag_name( value)[tag] else: self.log.error("定位类型书写错误:%s" % str(elements)) raise Exception return elem else: # app定位 if key == "css_selector": elem = self.app_driver.find_elements_by_css_selector( value)[tag] elif key == "xpath": elem = self.app_driver.find_elements_by_xpath(value)[tag] elif key == "accessibility_id": elem = self.app_driver.find_elements_by_accessibility_id( value)[tag] elif key == "id": elem = self.app_driver.find_elements_by_id(value)[tag] elif key == "name": elem = self.app_driver.find_elements_by_name(value)[tag] elif key == "class": elem = self.app_driver.find_elements_by_class_name( value)[tag] elif key == "link_text": elem = self.app_driver.find_elements_by_link_text(value) elif key == "partial_link_text": elem = self.app_driver.find_elements_by_partial_link_text( value) elif key == "tag_name": elem = self.app_driver.find_elements_by_tag_name( value)[tag] else: self.log.error("定位类型书写错误:%s" % str(elements)) raise Exception return elem except Exception as e: # self.log.debug("元素不存在:%s,%s" % (str(elements), e)) raise Exception def find_elements(self, elements): """查找元素集合""" try: key = elements[0] value = elements[1] if self.current_driver == "web_driver": # web查找元素 if key == "css_selector": elem = self.web_driver.find_elements_by_css_selector(value) elif key == "xpath": elem = self.web_driver.find_elements_by_xpath(value) elif key == "id": elem = self.web_driver.find_elements_by_id(value) elif key == "name": elem = self.web_driver.find_elements_by_name(value) elif key == "class_name": elem = self.web_driver.find_elements_by_class_name(value) elif key == "link_text": elem = self.web_driver.find_elements_by_link_text(value) elif key == "partial_link_text": elem = self.web_driver.find_elements_by_partial_link_text( value) elif key == "tag_name": elem = self.web_driver.find_element_by_tag_name(value) else: self.log.error("函数类型书写错误:%s" % str(elements)) raise Exception return elem else: # APP查找元素 if key == "css_selector": elem = self.app_driver.find_elements_by_css_selector(value) elif key == "xpath": elem = self.app_driver.find_elements_by_xpath(value) elif key == "accessibility_id": elem = self.app_driver.find_elements_by_accessibility_id( value) elif key == "id": elem = self.app_driver.find_elements_by_id(value) elif key == "name": elem = self.app_driver.find_elements_by_name(value) elif key == "class": elem = self.app_driver.find_elements_by_class_name(value) elif key == "link_text": elem = self.app_driver.find_elements_by_link_text(value) elif key == "partial_link_text": elem = self.app_driver.find_elements_by_partial_link_text( value) elif key == "tag_name": elem = self.app_driver.find_elements_by_tag_name(value) else: self.log.error("函数类型书写错误:%s" % str(elements)) raise Exception return elem except Exception as e: # self.log.debug("元素不存在:%s,%s" % (str(elements), e)) raise Exception def wait_elem(self, element): """等待元素出现""" key = element[0] value = element[1] locator = None try: if key == "css_selector": locator = (By.CSS_SELECTOR, value) elif key == "xpath": locator = (By.XPATH, value) elif key == "id": locator = (By.ID, value) elif key == "name": locator = (By.NAME, value) elif key == "class": locator = (By.CLASS_NAME, value) elif key == "link_text": locator = (By.LINK_TEXT, value) elif key == "partial_link_text": locator = (By.PARTIAL_LINK_TEXT, value) elif key == "tag_name": locator = (By.TAG_NAME, value) if self.current_driver == "web_driver": WebDriverWait(self.web_driver, 20, 0.5).until( ec.presence_of_element_located(locator), "%s元素未出现!" % str(element)) else: WebDriverWait(self.app_driver, 20, 0.5).until( ec.presence_of_element_located(locator), "%s元素未出现!" % str(element)) except Exception as e: self.log.error(e) raise Exception("等待元素出现时异常!") # def judgment(self, elements, tag=0): # """判断元素是否存在""" # for i in range(1, 6, +1): # time.sleep(1) # try: # self.find_elements_tag(elements, tag) # return True # except Exception as e: # return False # 方式二(速度较慢): # key = elements[0] # value = elements[1] # locator = None # # if key == "css_selector": # locator = (By.CSS_SELECTOR, value) # elif key == "xpath": # locator = (By.XPATH, value) # elif key == "id": # locator = (By.ID, value) # elif key == "name": # locator = (By.NAME, value) # elif key == "class": # locator = (By.CLASS_NAME, value) # elif key == "link_text": # locator = (By.LINK_TEXT, value) # elif key == "partial_link_text": # locator = (By.PARTIAL_LINK_TEXT, value) # elif key == "tag_name": # locator = (By.TAG_NAME, value) # # if current_driver == "web_driver": # try: # WebDriverWait(self.web_driver, 20, 0.5).until(lambda x: x.find_element(*locator)) # return True # except: # return False # else: # try: # WebDriverWait(self.app_driver, 20, 0.5).until(lambda x: x.find_element(*locator)) # return True # except: # return False def displayed(self, elements, tag=0): """判断元素是否可见""" try: elem = self.find_elements_tag(elements, tag) return elem.is_displayed() # 元素可见为True,隐藏为False except Exception as e: return False # 没有找到元素 def swipe_up(self, x=0.5, y1=0.85, y2=0.15, t=500): """屏幕向上滑动""" try: self.swipe(x, y1, y2, t) self.log.debug("上滑") except Exception as e: self.log.error(e) raise Exception("屏幕向上滑动时异常!") def swipe_down(self, x=0.5, y1=0.15, y2=0.85, t=500): """屏幕向下滑动""" try: self.swipe(x, y1, y2, t) self.log.debug("下滑") except Exception as e: self.log.error(e) raise Exception("屏幕向下滑动时异常!") def swipe(self, x, y1, y2, t): """上下滑动""" try: coordinate_x = self.app_driver.get_window_size()['width'] # 获取屏幕宽度 coordinate_y = self.app_driver.get_window_size()[ 'height'] # 获取屏幕高度 x1 = int(coordinate_x * x) # x坐标 y1 = int(coordinate_y * y1) # 起始y坐标 y2 = int(coordinate_y * y2) # 终点y坐标 self.app_driver.swipe(x1, y1, x1, y2, t) time.sleep(1) except Exception as e: raise Exception(e) def location(self, element, tag=0): """屏幕内容上下微调""" current_context = "" try: elem = self.find_elements_tag( element, tag) # css_selector不能在android环境下定位,所以定位完成后再切换环境 y1 = elem.location["y"] # 获取元素y坐标 # self.log.debug(y1) contexts = self.app_driver.contexts # 获取所有的context current_context = self.app_driver.current_context # 获取当前的context if current_context != contexts[ 0]: # 当前为非android环境时需要切换为APP_context才能进行滑动操作 self.app_driver.switch_to.context(contexts[0]) y2 = self.app_driver.get_window_size()['height'] # 获取屏幕高度 # self.log.debug(y2) while y1 + 200 > y2 or y1 < 100: # 判断是否需要滑动 if y1 + 200 > y2: self.swipe(x=0.02, y1=0.85, y2=0.45, t=500) # 向上滑 self.screen_shot() n = y1 if current_context == contexts[ 1]: # 当前为非H5环境时需要切换为H5环境才能获取元素坐标 self.app_driver.switch_to.context(contexts[1]) y1 = elem.location["y"] # self.log.debug(y1) if current_context != contexts[ 0]: # 当前为非android环境时需要切换为APP_context才能进行滑动操作 self.app_driver.switch_to.context(contexts[0]) if y1 < 100: self.swipe(x=0.02, y1=0.60, y2=0.75, t=500) # 向下滑 self.screen_shot() if n == y1: break except Exception as e: self.log.error(e) raise Exception("位置调整时异常!") finally: self.app_driver.switch_to.context(current_context) # 微调完成后切换为原来的环境
class SendEmail(object): """发送测试报告""" _instance_lock = threading.Lock() # 设置单例锁 def __new__(cls, *args, **kwargs): """单例模式(支持多线程)""" if not hasattr(cls, "_instance"): with cls._instance_lock: if not hasattr(cls, "_instance"): cls._instance = object.__new__(cls) return cls._instance def __init__(self): try: self.common = Common() # 实例化一个common调用公用函数 self.log = MyLog().get_log().logger # 实例化一个log打印日志 self.config = ReadConfig() # 实例化一个read_config读取email的配置信息 self.msg = email.mime.multipart.MIMEMultipart( 'alternative') # 实例化一个email发送email self.log_dir = self.common.get_result_path() # 获取存储日志的时间目录 self.principal_name_list = [] # 错误用例负责人list self.zip_path = self.log_dir + ".zip" # 设置存放zip的路径 self.result = False # 发送结果标志 self.num = 0 # 发送失败后重试次数 except Exception as e: self.log.error(e) raise Exception("SendEmail.__init__异常!") def with_zip(self): """附件以zip格式发送邮件""" while self.result is False and self.num < 3: # 发送失败后重试3次 try: # 提取错误用例负责人姓名 file_list = os.listdir(self.log_dir) # 获取时间目录下的文件列表 for file in file_list: file_name = os.path.splitext(file)[0] # 文件名 # 用正则表达式查找文件名为汉字的文件(负责人对应的错误日志文件),正则表达式为:非汉字的字符用""替换掉 if file_name == re.sub("[^\u4e00-\u9fa5]+", "", file_name): self.principal_name_list.append( file_name) # 添加负责人姓名到principal_name_list中 # 创建一个写入的zip对象 with zipfile.ZipFile( self.zip_path, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_obj: for path, folders, files in os.walk(self.log_dir): for file in files: zip_obj.write(os.path.join(path, file)) # 将内容写入zip # 添加附件 part = MIMEApplication(open(self.zip_path, 'rb').read()) # 读取内容 part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', "result.zip")) # 设置附件名 self.msg.attach(part) self.send() # 发送邮件 self.result = True except Exception as e: self.log.error("发送失败 %s" % e) self.num += 1 finally: os.remove(self.zip_path) # 删除zip文件 self.remove_result() # 删除之前的结果文件夹 def with_file(self): """附件以单个文件形式发送邮件""" while self.result is False and self.num < 3: # 发送失败后重试3次 try: file_list = os.listdir(self.log_dir) # 获取时间目录下的文件列表 for file in file_list: file_name = os.path.splitext(file)[0] # 文件名 file_type = os.path.splitext(file)[1] # 文件类型 # 用正则表达式查找文件名为汉字的文件(负责人对应的错误日志文件),正则表达式为:非汉字的字符用""替换掉 if file_name == re.sub("[^\u4e00-\u9fa5]+", "", file_name): self.principal_name_list.append( file_name) # 添加负责人姓名到错误负责人list中 current_file = os.path.join(self.log_dir, file) # 拼接当前的日志路径 part = MIMEApplication( open(current_file, 'rb').read()) # 读取当前的日志 part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', file)) # 设置附件名 self.msg.attach(part) elif file_type == ".html": # 查找html文件 current_file = os.path.join(self.log_dir, file) # 拼接当前的日志路径 part = MIMEApplication( open(current_file, 'rb').read()) # 读取当前的日志 part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', file)) # 设置附件名 self.msg.attach(part) elif "error" in file_name: # 查找错误日志文件 current_file = os.path.join(self.log_dir, file) # 拼接当前的日志路径 part = MIMEApplication( open(current_file, 'rb').read()) # 读取当前的日志 part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', file)) # 设置附件名 self.msg.attach(part) self.send() # 发送邮件 self.result = True except Exception as e: self.log.error("发送失败 %s" % e) self.num += 1 finally: self.remove_result() # 删除之前的结果文件夹 def send(self): """发送邮件""" try: # 从配置文件中读取发件人信息 sender_name = "" # 发件人 sender_email = "" # 发件箱 sender_dict = json.loads(self.config.get_email("sender")) for key, value in sender_dict.items(): sender_name = key # 发件人 sender_email = value # 发件箱 # 从配置文件中读取收件人信息 # receivers内容为字典时使用(receivers = {"蓝梦":"*****@*****.**", "孟冰":"*****@*****.**") receivers_dict = json.loads(self.config.get_email("receivers")) name_list = [] # 收件人list receivers = [] # 收件箱list for key, value in receivers_dict.items(): if key in self.principal_name_list: name_list.append(key) receivers.append(value) # 邮件信息 name_list_str = ",".join(name_list) # 收件人姓名,将list转换为str mail_host = self.config.get_email("email_host") # 设置邮箱服务器域名 mail_port = self.config.get_email("email_port") # 设置邮箱服务器接口 mail_user = self.config.get_email("email_user") # 发件人用户名 mail_pass = self.config.get_email("email_pass") # 发件人口令 subject = self.config.get_email("subject") # 主题 content = self.config.get_email("content") # 正文 if len(name_list_str) == 0: self.log.debug("所有用例都正常通过!") else: self.log.debug("发件人:%s" % sender_name) self.log.debug("收件人:%s" % name_list_str) txt = email.mime.text.MIMEText(content, 'plain', 'utf-8') self.msg.attach(txt) self.msg['Subject'] = Header(subject, 'utf-8') self.msg['From'] = Header(sender_name, 'utf-8') self.msg['To'] = Header("%s" % name_list_str, 'utf-8') # 调用邮箱服务器 smt_obj = smtplib.SMTP_SSL(mail_host, mail_port) # 登录邮箱 smt_obj.login(mail_user, mail_pass) # 发送邮件 smt_obj.sendmail(sender_email, receivers, self.msg.as_string()) # 关闭邮箱 smt_obj.quit() self.log.debug("发送成功!") except Exception as e: self.log.error(e) raise Exception("发送email时异常!") def remove_result(self): """发送报告后删除其他的文件夹""" try: result_path = os.path.dirname(self.log_dir) # 获取result目录路径 result_list = os.listdir(result_path) # 获取result下的文夹列表 i = len(result_list) # 统计文件夹数量 for file in result_list: path = os.path.join(result_path, file) # 拼接每个文件夹的路径 if i > 1: # 保留最新的文件夹 shutil.rmtree(path) i -= 1 except Exception as e: self.log.error(e) raise Exception("删除result下文件夹时异常!")
def __init__(self): self.common = Common()
class MyLog: log = None mutex = threading.Lock() _instance_lock = threading.Lock() # 设置单例锁 def __new__(cls, *args, **kwargs): """单例模式(支持多线程)""" if not hasattr(cls, "_instance"): with cls._instance_lock: if not hasattr(cls, "_instance"): cls._instance = object.__new__(cls) return cls._instance def __init__(self): self.common = Common() @staticmethod def get_log(): """日志单例模式""" try: if MyLog.log is None: MyLog.mutex.acquire() MyLog.log = Log() MyLog.mutex.release() return MyLog.log except Exception as e: raise Exception("MyLog.get_log异常 %s" % e) def extraction_error_log(self): """按负责人提取错误日志""" try: log_path = self.common.get_result_path("result.log") # 生成原始日志文件路径 error_log_path = self.common.get_result_path( "error.log") # 生成错误日志文件路径 # 从邮件的收件人信息中读取所有的负责人姓名,实现错误日志按人分类 config = ReadConfig() receivers = config.get_email("receivers") # 收件人列表str类型(姓名,邮箱) receivers_dict = json.loads(receivers) # 收件人列表dict类型(姓名,邮箱) principal_list = [] # 收件人姓名列表 for key, value in receivers_dict.items(): principal_list.append(key) data = "" # 单个错误日志临时存储器 error = False # 错误日志标识 all_error_num = 0 # 错误用例总的编号 # principal_error_num = 0 # 负责人的错误编号 i = 1 # 原始日志中的文本行数 j = 1 # 单个用例内的行数 with open(log_path, "r", encoding="utf-8") as log: # 以read方式打开原始日志 with open(error_log_path, "w", encoding="utf-8") as error_log: # 以write方式打开错误日志 lines = log.readlines() # 读取原始日志 principal = "" # 用例负责人 for line in lines: # 匹配负责人姓名,以便是错误日志时创建负责人对应的错误日志文件名,负责人姓名在单个用例的第一行 if j == 1: for name in principal_list: if name in line: principal = name data = data + line # 临时存储一个用例的日志 if "failed!" in line: # line中包含"ERROR"的标记为错误日志 error = True # "*"不可改,出现"*"表示一个用例结束,一个用例结束并被标记为错误日志的内容被写入error_log和principal_log文件 if "*" * 100 in line and error is True: all_error_num += 1 error_log.write("\n错误用例%s:" % str(all_error_num)) error_log.write(data) # 所有错误日志写入一个文件中 # 将错误日志提取到对应负责人的日志文件中 principal_log_name = "%s.log" % principal # 负责人对应的错误日志名 principal_log_path = self.common.get_result_path( principal_log_name) # 生成负责人对应的错误日志路径 # 按负责人分类写入对应的文件中 if os.path.exists( principal_log_path ): # 判断principal_log_path是否存在,存在时添加,不存在时创建 with open(principal_log_path, "a+", encoding="utf-8") as principal_log: principal_log.write(data) else: with open(principal_log_path, "w+", encoding="utf-8") as principal_log: principal_log.write(data) # 有错误日志时恢复初始化 data = "" error = False j = 1 # 没有错误日志时恢复初始化 elif "*" * 100 in line: # "*"不可改 data = "" error = False j = 1 i += 1 if os.path.exists(error_log_path) and os.path.getsize( error_log_path) == 0: # 如果没有错误日志,就删除空的error_log文件 os.remove(error_log_path) except Exception as e: raise Exception("MyLog.extraction_error_log异常 %s" % e)
class ProduceCases: """利用base_case.py自动生成所有api测试用例的请求函数类test_cases.py""" _instance_lock = threading.Lock() # 设置单例锁 def __new__(cls, *args, **kwargs): """单例模式(支持多线程)""" if not hasattr(cls, "_instance"): with cls._instance_lock: if not hasattr(cls, "_instance"): cls._instance = object.__new__(cls) return cls._instance def __init__(self): try: self.log = MyLog.get_log().logger self.common = Common() self.api_cases_path, self.api_cases_dict = self.common.get_api_cases( ) except Exception as e: self.log.error(e) raise Exception("出现异常!") def produce_case(self): """生成所有api用例""" try: self.log.debug("api用例路径:%s" % self.api_cases_path) # self.log.debug(self.common.api_cases_dict) base_case_path = Common.get_path( "cases", "api", "base_case.py") # 拼接用例解析函数模板(base_case.py)路径 test_cases_path = Common.get_path( "cases", "api", "test_cases.py") # 拼接存放生成的所有测试用例的文件路径 with open(base_case_path, "r", encoding="utf-8") as file_old: # 从file_old中读取 with open(test_cases_path, "w", encoding="utf-8") as file_new: # 写入file_new中 lines = file_old.readlines() # 按行读取file_old中的所有数据 # 不需要改变的部分 i = 1 # 不需要改变的行号 for line in lines: if "# 定位标记" in line: break file_new.write(line) i += 1 # 需要改变的部分 n = 0 # 用例编号 global case_name # 用例名 for origin, sheet_dict in self.api_cases_dict.items(): # key:origin(项目地址原点),value:sheet_dict(单个sheet中的用例集合) for case_name, case_params in sheet_dict.items( ): # key:case_name(用例名),value:case_params(一条用例) n += 1 j = i # 需要改变的行号 while j < len(lines): # 动态生成一个用例 if "def test_case(self):" in lines[j]: line = lines[j].replace( "test_case", "test_%s" % case_name) elif "用例描述" in lines[j]: line = lines[j].replace( "用例描述", str(case_params["remark"])) elif "case_params = {}" in lines[j]: line = lines[j].replace( "{}", str(case_params)) elif "origin = 'null'" in lines[j]: line = lines[j].replace("null", origin) elif "case_name = 'null'" in lines[j]: line = lines[j].replace("null", case_name) elif "case_num = 0" in lines[j]: line = lines[j].replace("0", str(n)) elif "self.execute_case" in lines[j]: line = lines[j] + "\n" else: line = lines[j] # 不在上边的其他行(空行) file_new.write(line) j += 1 self.log.debug("api用例数量:%s" % n) self.log.debug("*" * 100 + "\n") except Exception as e: self.log.error(e) raise Exception("请检测用例%s格式是否正确!" % case_name)
def produce_case(self): """生成所有api用例""" try: self.log.debug("api用例路径:%s" % self.api_cases_path) # self.log.debug(self.common.api_cases_dict) base_case_path = Common.get_path( "cases", "api", "base_case.py") # 拼接用例解析函数模板(base_case.py)路径 test_cases_path = Common.get_path( "cases", "api", "test_cases.py") # 拼接存放生成的所有测试用例的文件路径 with open(base_case_path, "r", encoding="utf-8") as file_old: # 从file_old中读取 with open(test_cases_path, "w", encoding="utf-8") as file_new: # 写入file_new中 lines = file_old.readlines() # 按行读取file_old中的所有数据 # 不需要改变的部分 i = 1 # 不需要改变的行号 for line in lines: if "# 定位标记" in line: break file_new.write(line) i += 1 # 需要改变的部分 n = 0 # 用例编号 global case_name # 用例名 for origin, sheet_dict in self.api_cases_dict.items(): # key:origin(项目地址原点),value:sheet_dict(单个sheet中的用例集合) for case_name, case_params in sheet_dict.items( ): # key:case_name(用例名),value:case_params(一条用例) n += 1 j = i # 需要改变的行号 while j < len(lines): # 动态生成一个用例 if "def test_case(self):" in lines[j]: line = lines[j].replace( "test_case", "test_%s" % case_name) elif "用例描述" in lines[j]: line = lines[j].replace( "用例描述", str(case_params["remark"])) elif "case_params = {}" in lines[j]: line = lines[j].replace( "{}", str(case_params)) elif "origin = 'null'" in lines[j]: line = lines[j].replace("null", origin) elif "case_name = 'null'" in lines[j]: line = lines[j].replace("null", case_name) elif "case_num = 0" in lines[j]: line = lines[j].replace("0", str(n)) elif "self.execute_case" in lines[j]: line = lines[j] + "\n" else: line = lines[j] # 不在上边的其他行(空行) file_new.write(line) j += 1 self.log.debug("api用例数量:%s" % n) self.log.debug("*" * 100 + "\n") except Exception as e: self.log.error(e) raise Exception("请检测用例%s格式是否正确!" % case_name)
pattern=cases_file, top_level_dir=None) return discover # 方式二: def run_cases(self, case): """执行用例并生成报告""" result = BeautifulReport(case) result.report(log_path=self.path, filename="NT_测试报告.html", description='NT自动化测试') if __name__ == "__main__": run = Run() common = Common() start_time = common.get_now_time() # 方式一: run.add_api_test() # run.add_ui_test() BeautifulReport(run.suit).report(log_path=run.path, filename="NT_测试报告.html", description='NT自动化测试') # 方式二: # cases = run.add_cases() # run.run_cases(cases) run.my_log.extraction_error_log() # 提取错误日志 run.send_email.with_zip() # 发送email end_time = common.get_now_time()