class FirstOpen: ''' kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True self.testInfo = getYam(self.path)["testinfo"] self.testCase = getYam(self.path)["testcase"] ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, logTest) if not result: print("操作失败") self.isOperate = False break ''' 检查点 caseName:函数名 logTest 记录日志:一个手机记录单独记录一个日志 ''' def checkPoint(self, caseName, logTest, devices): result = False if not self.isOperate: print("操作失败,检查点失败") # return self.isOperate else: check = getYam(self.path)["check"] result = self.operateElement.findElement(check) # 检查点 countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=caseName, driver=self.driver, logTest=logTest, devices=devices) return result
class FeedbackPage: ''' 滑动删除收藏 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() # self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True self.test_msg = kwargs["test_msg"] self.testInfo = self.test_msg[1]["testinfo"] self.testCase = self.test_msg[1]["testcase"] self.testcheck = self.test_msg[1]["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.platformName = kwargs["platformName"] self.get_value = [] self.is_get = False # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 self.msg = "" ''' 操作步骤 logTest 日记记录器 ''' def get_size(self): """获取屏幕分辨率.""" rect = self.driver.get_window_size() return rect['width'], rect['height'] def swipe_by_ratio(self, start_x, start_y, direction, ratio, duration=None): """ 按照屏幕比例的滑动. :param start_x: 起始横坐标 :param start_y: 起始纵坐标 :param direction: 滑动方向,只支持'up'、'down'、'left'、'right'四种方向参数 :param ratio: 滑动距离与屏幕的比例,范围0到1 :param duration: 滑动时间,单位ms :return: """ direction_list = ['up', 'down', 'left', 'right'] if direction not in direction_list: print('滑动方向%s不支持', direction) width, height = self.get_size() def swipe_up(): """上滑.""" end_y = start_y - ratio * height if end_y < 0: print('上滑距离过大') return False else: self.driver.swipe(start_x, start_y, start_x, end_y, duration) return True def swipe_down(): """下滑.""" end_y = start_y + ratio * height if end_y > height: print('下滑距离过大') return False else: self.driver.swipe(start_x, start_y, start_x, end_y, duration) return True def swipe_left(): """左滑.""" end_x = start_x - ratio * width if end_x < 0: print('左滑距离过大') return False else: self.driver.swipe(start_x, start_y, end_x, start_y, duration) return True def swipe_right(): """右滑.""" end_x = start_x + ratio * width if end_x > width: print('右滑距离过大') return False else: self.driver.swipe(start_x, start_y, end_x, start_y, duration) return True swipe_dict = { 'up': swipe_up, 'down': swipe_down, 'left': swipe_left, 'right': swipe_right } return swipe_dict[direction]() def operate(self): m_s_g = self.msg + "\n" if self.msg != "" else "" for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] print(msg) self.testInfo[0]["msg"] = msg self.msg = m_s_g + msg self.isOperate = False return False if item.get("operate_type", "0") == be.SWIPE_UP: # 根据元素上滑动 width, height = self.get_size() start_x = width // 2 start_bottom = height - height // 8 el_up = self.driver.find_elements_by_id(item["element_info"]) while True: try: el_up.click() break except: self.swipe_by_ratio(start_x, start_bottom, 'up', 0.5, 500) if item.get("operate_type", "0") == be.GET_VALUE: self.get_value.append(result["text"]) return True def checkPoint(self, kwargs={}): result = self.check(kwargs) if self.test_msg[0] is not False: # 如果用例编写正确 if result is not True and be.RE_CONNECT: self.msg = "用例失败并重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 # self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = False self.operate() result = self.check(kwargs) self.testInfo[0]["msg"] = self.msg statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, platformName=self.platformName, testCheck=self.testcheck) ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary:相反检查点,传1表示如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比成功,说明失败 check_point: 自定义检查结果 ''' def check(self, kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 如果有重跑机制,成功后会默认把日志传进来 # if kwargs.get("check_point", "0") != "0": 自定义检查点 # return kwargs["check_point"] if self.isOperate: for item in self.testcheck: # toast检查,存在则说明成功 if kwargs.get("check", be.DEFAULT_CHECK) == be.TOAST: result = \ self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=self.logTest)[ "result"] if result is False: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) # 默认检查点,就是查找页面元素 if kwargs.get("check", be.DEFAULT_CHECK ) == be.DEFAULT_CHECK and not resp["result"]: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break # 相反检查点,表示如果检查元素存在就说明失败,如删除后,此元素依然存在 if kwargs.get( "check", be.DEFAULT_CHECK) == be.CONTRARY and resp["result"]: m = get_error({ "type": be.CONTRARY, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = self.msg result = False break # 检查点关键字contrary_getval: 相反值检查点,如果对比成功,说明失败 if kwargs.get("check", be.DEFAULT_CHECK) == be.CONTRARY_GETVAL and self.is_get and resp["result"] \ in self.get_value: m = get_error({ "type": be.CONTRARY_GETVAL, "current": item["element_info"], "history": resp["text"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break # 历史数据和实际数据对比 if kwargs.get("check", be.DEFAULT_CHECK) == be.COMPARE and self.is_get and resp["text"] \ not in self.get_value: # 历史数据和实际数据对比 result = False m = get_error({ "type": be.COMPARE, "current": item["element_info"], "history": resp["text"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: result = False return result
class CollectSwipeDelPage: ''' 滑动删除收藏 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.get_value = [] ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, logTest) if not result: print("执行过程中失败,请检查元素是否存在" + item["element_info"]) self.testInfo[0][ "msg"] = "执行过程中失败,请检查元素是否存在" + item["element_info"] self.isOperate = False return False if item.get("operate_type", "0") == be.SWIPE_LEFT: # 根据元素左滑动 web_element = self.driver.find_elements_by_id( item["element_info"])[item["index"]] start = web_element.location # 获取控件开始位置的坐标轴 startx = start["x"] starty = start["y"] # 获取控件坐标轴差 size1 = web_element.size width = size1["width"] height = size1["height"] # 计算出控件结束坐标 endX = width + startx endY = height + starty self.driver.swipe(endX - 50, endY, starty + 500, endY) if item.get("operate_type", "0") == be.GET_VALUE: re_reulst = re.findall(r'[a-zA-Z\d+\u4e00-\u9fa5]', result) # 只匹配中文,大小写,字母 self.get_value.append("".join(re_reulst)) return True ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 ''' def checkPoint(self, **kwargs): result = True if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, kwargs["logTest"]) if resp in self.get_value: # 删除后数据对比 msg = "删除数据失败,删除前数据为:" + str( self.get_value) + "当前获取的数据为:" + resp print(msg) self.testInfo[0]["msg"] = msg break else: result = False countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=kwargs["caseName"], driver=self.driver, logTest=kwargs["logTest"], devices=kwargs["devices"], testCase=self.testCase, testCheck=self.testcheck) return result
class PagesObjects: ''' page层 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, kwargs): self.driver = kwargs["driver"] #dict.get(key, default=None) 返回指定键的值,如果值不在字典中返回默认值None #这处地方报错,暂时没想明白为什么报错,先注释掉 """ if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app for i in range(0,1): self.driver.back() self.driver.launch_app() """ # self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True self.test_msg = kwargs["test_msg"] self.testInfo = self.test_msg[1]["testinfo"] self.testCase = self.test_msg[1]["testcase"] self.testcheck = self.test_msg[1]["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.is_get = False # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 self.msg = "" ''' 操作步骤 ''' def operate(self): if self.test_msg[0] is False: # 如果用例编写错误 self.isOperate = False return False for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" #调用BaseOperate.py文件里的operateElement.operate方法,传入四个参数, result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item[ "element_info"] + "," + result.get("text", " ") if not result.get("webview", True): msg = "切换到webview失败,请确定是否在webview页面" print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("--等待下---") if item.get("operate_type", "0") == be.GET_VALUE or item.get( "operate_type", "0") == be.GET_CONTENT_DESC: self.get_value.append(result["text"]) self.is_get = True # 对比数据 return True def checkPoint(self, kwargs={}): result = self.check(kwargs) #执行下面的check方法 if self.test_msg[0] is not False: # 如果用例编写正确 if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = False self.operate() result = self.check(kwargs) self.testInfo[0]["msg"] = self.msg self.operateElement.switchToNative() statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary:相反检查点,传1表示如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比成功,说明失败 check_point: 自定义检查结果 ''' def check(self, kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 如果有重跑机制,成功后会默认把日志传进来 # if kwargs.get("check_point", "0") != "0": 自定义检查点 # return kwargs["check_point"] if self.isOperate: for item in self.testcheck: #如果check的值为toast,则检查点为toast,否则为默认检查点 print("-------start checking-----------") print(item.get("check")) print("-----------end check------------") if item.get("check", be.DEFAULT_CHECK) == be.TOAST: result = \ self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=self.logTest)[ "result"] if result is False: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: #调用BaseOperate里的operateElement.operate方法,传入检查点check的信息,operate()方法会调用findElement方法 #如果findElement方法找到元素会return true,又因为检查点信息check里没有operate_type字段,operate_by方法会直接将此判定为true #不执行元素操作,所以此处的调用就是完成 查找默认检查点是否存在的操作 resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) #如果找到元素,resp["result"]为Ture,not resp["result"]就是false if item.get("check", be.DEFAULT_CHECK ) == be.DEFAULT_CHECK and not resp["result"]: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break #如果找到这个元素,就是检查失败 if item.get( "check", be.DEFAULT_CHECK) == be.CONTRARY and resp["result"]: m = get_error({ "type": be.CONTRARY, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = self.msg result = False break # 检查点关键字contrary_getval: 相反值检查点,如果对比成功,说明失败 if item.get("check", be.DEFAULT_CHECK) == be.CONTRARY_GETVAL and self.is_get and resp["result"] \ in self.get_value: m = get_error(\ {"type": be.CONTRARY_GETVAL, "current": item["element_info"], "history": resp["text"]}) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if item.get("check", be.DEFAULT_CHECK) == be.COMPARE and self.is_get and resp["text"] \ not in self.get_value: # 历史数据和实际数据对比 m = get_error({ "type": be.COMPARE, "current": item["element_info"], "history": resp["text"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break #文本检查点,如果元素的text文本和设置的msg不想等,则判定为失败 if item.get("check", be.DEFAULT_CHECK ) == be.TEXT_CHECK and resp["text"] == item["msg"]: m = get_error({ "type": be.TEXT_CHECK, "flag": item["msg"], "current": resp["text"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break else: result = False return result
class CardsSortPage: ''' 滑动删除历史记录 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.location = [] self.msg = "" ''' 操作步骤 logTest 日记记录器 ''' def operate(self): for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("operate_type", "0") == "location": app = {} web_element = self.driver.find_elements_by_id(item["element_info"])[item["index"]] start = web_element.location # 获取控件开始位置的坐标轴 app["startX"] = start["x"] app["startY"] = start["y"] # 获取控件坐标轴差 size1 = web_element.size width = size1["width"] height = size1["height"] # 计算出控件结束坐标 endX = width + app["startX"] endY = height + app["startY"] app["endX"] = endX - 20 app["endY"] = endY - 60 self.location.append(app) # self.driver.swipe(endX, endY, starty, endY) if item.get("operate_type", "0") == be.GET_VALUE: self.get_value.append(result["text"]) if item.get("is_swpie", "0") != "0": print(self.location) self.driver.swipe(self.location[0]["endX"], self.location[0]["endY"], self.location[1]["endX"], self.location[1]["endY"]+10) return True def checkPoint(self, kwargs={}): result = self.check() if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.operate() self.get_value = [] self.location = "" result = self.check() self.testInfo[0]["msg"] = self.msg statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) return result ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary 相反检查点,意思就是如果检查结果为真,检查点就是失败 ''' def check(self): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 重跑后异常日志 if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not resp["result"]: msg = "请检查元素" + item["element_info"] + "是否存在" self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg result = False if resp['text'] not in self.get_value: # 删除后数据对比 msg = "卡片排序失败" + str(self.get_value) + "当前首页第一条卡片数据为:" + resp["text"] + "排序成功的第一个卡片值为:"+".".join(self.get_value) self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg break else: result = False return result
class CollectSwipeDelPage: ''' 滑动删除收藏 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.msg = "" ''' 操作步骤 logTest 日记记录器 ''' def operate(self): m_s_g = self.msg + "\n" if self.msg != "" else "" for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] print(msg) self.testInfo[0]["msg"] = msg self.msg = m_s_g + msg self.isOperate = False return False if item.get("operate_type", "0") == be.SWIPE_LEFT: # 根据元素左滑动 web_element = self.driver.find_elements_by_id(item["element_info"])[item["index"]] start = web_element.location # 获取控件开始位置的坐标轴 startx = start["x"] starty = start["y"] # 获取控件坐标轴差 size1 = web_element.size width = size1["width"] height = size1["height"] # 计算出控件结束坐标 endX = width + startx endY = height + starty self.driver.swipe(endX-50, endY, starty+500, endY) if item.get("operate_type", "0") == be.GET_VALUE: self.get_value.append(result["text"]) return True def checkPoint(self): result = self.check() if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 # self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.operate() result = self.check() self.testInfo[0]["msg"] = self.msg self.operateElement.switchToNative() statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) return result ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 ''' def check(self, kwargs={}): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not resp["result"]: # 表示操作出现异常情况检查点为成功 print("操作失败,简单点为成功") result = True break if resp["text"] in self.get_value: # 删除后数据对比 msg = "删除数据失败,删除前数据为:" + ".".join(self.get_value) + "当前获取的数据为:" + resp["text"] self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg break else: result = False return result
class PagesObjects: ''' page层 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.get_value = [] self.is_get = False # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 self.msg = "" ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, logTest) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] if not result.get("webview", True): msg = "切换到webview失败,请确定是否在webview页面" print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("--等待下---") if item.get("operate_type", "0") == be.GET_VALUE or item.get("operate_type", "0") == be.GET_CONTENT_DESC : self.get_value.append(result["text"]) self.is_get = True # 对比数据 return True def checkPoint(self, **kwargs): result = self.check(**kwargs) if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] kwargs["logTest"].buildStartLine(kwargs["caseName"]+"_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = False self.operate(kwargs["logTest"]) result = self.check(**kwargs) self.testInfo[0]["msg"] = self.msg self.operateElement.switchToNative() countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=kwargs["caseName"], driver=self.driver, logTest=kwargs["logTest"], devices=kwargs["devices"], testCase=self.testCase, testCheck=self.testcheck) return result ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary:相反检查点,传1表示如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比成功,说明失败 check: 自定义检查结果 excepts: 如果为1,表示操作出现异常情况检查点为成功 ''' def check(self, **kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 如果有重跑机制,成功后会默认把日志传进来 # if kwargs.get("check", "0") != "0": # return kwargs["check"] if self.isOperate: for item in self.testcheck: if kwargs.get("toast", "0") != "0": resp = self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=kwargs["logTest"]) else: resp = self.operateElement.operate(item, self.testInfo, kwargs["logTest"]) if kwargs.get("excepts", "0") != "0" and not resp["result"]: print("操作失败,简单点为成功") result = True break if kwargs.get("contrary", "0") != "0" and resp["result"]: m = "请检查%s" % item["info"] + "是否成功" self.msg = m_s_g + m print(self.msg) self.testInfo[0]["msg"] = m result = False break if kwargs.get("contrary", "0") == "0" and not resp["result"]: m = "请检查元素" + item["element_info"] + "是否存在" self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if kwargs.get("contrary_getval", "0") != "0" and self.is_get and resp["result"] in self.get_value: result = False m = "对比数据失败,当前取到到数据为:%s,历史取到数据为:%s" % resp["text"] % self.get_value self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break if kwargs.get("contrary_getval", "0") == "0" and self.is_get and resp["text"] not in self.get_value: # 历史数据和实际数据对比 result = False m = "对比数据失败,获取历史数据为:" + ".".join(self.get_value) + ",当前获取的数据为:" + resp["text"] self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: result = False return result
class p_settings_search02: ''' 滑动删除历史记录 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, kwargs): self.driver = kwargs["driver"] #Driver if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] #用例参数yaml路径 self.operateElement = OperateElement(self.driver) #元素操作 self.isOperate = True #元素是否可执行,默认为可以。 #从yaml文件解析操作步骤和执行结果 test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] #每一条用例由 testinfo,testcase,check3部分组成,其中testcase由多个元素组成 self.device = kwargs["device"] self.logTest = kwargs["logTest"] #myLog().getLog(cls.devicesName) self.caseName = kwargs["caseName"] #测试函数名 self.get_value = get_text(self.testcheck) #self.get_value = ['Add a language'] self.location = [] self.msg = "" ''' 操作步骤 logTest 日记记录器 ''' def operate(self): for item in self.testCase: #解析出测试步骤(测试步骤包括多个元素) print(item) print('IIIIIIIIIIIIIIIIIIIIIIIIIII') m_s_g = self.msg + "\n" if self.msg != "" else "" #如果msg非空,加\n,否则保持空 result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) #result:True or False #执行测试用例,并获取结果 #如果result["result"] 为:None,False,'' ,则: if not result["result"]: msg = "执行失败,请检查元素是否存在:" + item["element_info"] self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False #如果该用例的操作类型为 location,获取该控件的坐标self.location if item.get("operate_type", "0") == "location": app = {} web_element = self.driver.find_elements_by_id( item["element_info"])[item["index"]] start = web_element.location # 获取控件开始位置的坐标轴 app["startX"] = start["x"] app["startY"] = start["y"] # 获取控件坐标轴差 size1 = web_element.size width = size1["width"] height = size1["height"] # 计算出控件结束坐标 endX = width + app["startX"] endY = height + app["startY"] app["endX"] = endX - 20 app["endY"] = endY - 60 print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^') print(self.location) self.location.append(app) # self.driver.swipe(endX, endY, starty, endY) print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^') print(self.location) #如果该用例的操作类型为 :get_value (获取text) if item.get("operate_type", "0") == be.GET_VALUE: #将操作获得的text值,注入到 self.get_value中 self.get_value.append(result["text"]) #如果该用例中 is_swpie 不是0 的时候:(待确认) if item.get("is_swpie", "0") != "0": self.driver.swipe(self.location[0]["endX"], self.location[0]["endY"], self.location[1]["endX"], self.location[1]["endY"] + 10) return True #(待确认) def checkPoint(self, kwargs={}): result = self.check() if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.operate() self.get_value = [True] self.location = "" result = self.check() self.testInfo[0]["msg"] = self.msg statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) return result ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary 相反检查点,意思就是如果检查结果为真,检查点就是失败 ''' def check(self): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 重跑后异常日志 if self.isOperate: #默认为True for item in self.testcheck: #迭代yaml中check元素 resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) #resp : maybe = {'result': True, 'text': 'ERASEEVERYTHING'} print(resp) print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') if resp["result"] == True: result = True if not resp["result"]: msg = "请检查元素" + item["element_info"] + "是否存在" self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg result = False if resp['text'] not in self.get_value: #resp : {'result': True, 'text': 'ERASEEVERYTHING'} msg = "期望的text为:" + str( self.get_value) + "。 实际的text为:" + resp["text"] self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg break else: result = False return result
class PagesObjects: def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": self.driver.launch_app() self.operateElement = OperateElement(self.driver) self.isOperate = True self.test_msg = kwargs["test_msg"] self.testInfo = self.test_msg[1]["testinfo"] self.testCase = self.test_msg[1]["testcase"] self.testcheck = self.test_msg[1]["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.is_get = False self.msg = "" def operate(self): if self.test_msg[0] is False: self.isOperate = False return False for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "Falló durante la ejecución, verifique si el elemento existe" + item[ "element_info"] + "," + result.get("text", " ") if not result.get("webview", True): msg = "No se pudo cambiar a la vista web, confirme si está en la página de vista web" print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) print("--Espere---") if item.get("operate_type", "0") == be.GET_VALUE or item.get( "operate_type", "0") == be.GET_CONTENT_DESC: self.get_value.append(result["text"]) self.is_get = True return True def checkPoint(self, kwargs={}): result = self.check(kwargs) if self.test_msg[0] is not False: if result is not True and be.RE_CONNECT: self.msg = "El caso de uso falló y se volvió a conectar una vez, el motivo del fallo:" + \ self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_No se pudo volver a conectar") self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = False self.operate() result = self.check(kwargs) self.testInfo[0]["msg"] = self.msg self.operateElement.switchToNative() statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) def check(self, kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" if self.isOperate: for item in self.testcheck: if kwargs.get("check", be.DEFAULT_CHECK) == be.TOAST: result = \ self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=self.logTest)[ "result"] if result is False: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if kwargs.get("check", be.DEFAULT_CHECK ) == be.DEFAULT_CHECK and not resp["result"]: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break else: result = False return result
class CardsSortPage: def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.location = [] self.msg = "" def operate(self): for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "Falló durante la ejecución, verifique si el elemento existe" + item["element_info"] print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("operate_type", "0") == "location": app = {} web_element = self.driver.find_elements_by_id(item["element_info"])[item["index"]] start = web_element.location app["startX"] = start["x"] app["startY"] = start["y"] size1 = web_element.size width = size1["width"] height = size1["height"] endX = width + app["startX"] endY = height + app["startY"] app["endX"] = endX - 20 app["endY"] = endY - 60 self.location.append(app) if item.get("operate_type", "0") == be.GET_VALUE: self.get_value.append(result["text"]) if item.get("is_swpie", "0") != "0": print(self.location) self.driver.swipe(self.location[0]["endX"], self.location[0]["endY"], self.location[1]["endX"], self.location[1]["endY"] + 10) return True def checkPoint(self, kwargs={}): result = self.check() if result is not True and be.RE_CONNECT: self.msg = "El caso de uso falló y se volvió a conectar una vez, el motivo del fallo:" + self.testInfo[0][ "msg"] self.logTest.buildStartLine(self.caseName + "_No se pudo volver a conectar") self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.operate() self.get_value = [] self.location = "" result = self.check() self.testInfo[0]["msg"] = self.msg statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) return result def check(self): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not resp["result"]: msg = "Por favor marque el elemento" + item["element_info"] + "existe" self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg result = False if resp['text'] not in self.get_value: msg = "Falló la clasificación de tarjetas" + str( self.get_value) + "Los primeros datos de la tarjeta en la página de inicio actual son:" + resp[ "text"] + "El primer valor de la tarjeta que se clasifica correctamente es: " + ".".join( self.get_value) self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg break else: result = False return result
class PagesObjects: ''' page层 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch", "0") == "0": # 若为空, 刷新页面 self.driver.get(self.driver.current_url) self.operateElement = "" self.isOperate = True self.test_msg = kwargs["test_msg"] self.testInfo = self.test_msg[1]["testinfo"] self.testCase = self.test_msg[1]["testcase"] self.test_check = self.test_msg[1]["check"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.is_get = False # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 self.msg = "" ''' 操作步骤 ''' def operate(self): if self.test_msg[0] is False: self.isOperate = False return False self.operateElement = OperateElement(self.driver) for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, self.logTest) if not result["result"]: msg = get_error({ "type": be.DEFAULT_ERROR, "element_info": item["element_info"] }) print(msg) self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("==等待%s秒==" % item["is_time"]) if item.get("operate_type", "0") == be.GET_VALUE or item.get( "operate_type", "0") == be.GET_TEXT: self.get_value.append(result["text"]) self.is_get = True # 对比数据 return True def checkPoint(self, kwargs={}): result = self.check(kwargs) statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, testCase=self.testCase, testCheck=self.test_check) ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 ''' def check(self, kwargs): result = True # if kwargs.get("check_point", "0") != "0": # return kwargs["check_point"] if self.isOperate: for item in self.test_check: resp = self.operateElement.operate(item, self.testInfo, self.logTest) # 默认检查点,就是查找页面元素 if kwargs.get("check", be.DEFAULT_CHECK ) == be.DEFAULT_CHECK and not resp["result"]: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) print(m) self.testInfo[0]["msg"] = m result = False break # 历史数据和实际数据对比 if kwargs.get("check", be.DEFAULT_CHECK) == be.COMPARE and self.is_get and resp["text"]\ not in self.get_value: # 历史数据和实际数据对比 result = False m = get_error({ "type": be.COMPARE, "current": item["element_info"], "history": resp["text"] }) print(m) self.testInfo[0]["msg"] = m break # 相反检查点,表示如果检查元素存在就说明失败,如删除后,此元素依然存在 if kwargs.get( "check", be.DEFAULT_CHECK) == be.CONTRARY and resp["result"]: m = get_error({ "type": be.CONTRARY, "element_info": item["element_info"], "info": item["info"] }) print(m) self.testInfo[0]["msg"] = m result = False break # 检查点关键字contrary_getval: 相反值检查点,如果对比成功,说明失败 if kwargs.get("check", be.DEFAULT_CHECK) == be.CONTRARY_GETVAL and self.is_get and resp["result"] \ in self.get_value: m = get_error({ "type": be.CONTRARY_GETVAL, "current": item["element_info"], "history": resp["text"] }) print(m) self.testInfo[0]["msg"] = m result = False break else: result = False return result
class HistorySwipeDelPage: ''' 滑动删除历史记录 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.msg = "" ''' 操作步骤 logTest 日记记录器 ''' def operate(self): for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] m_s_g = self.msg + "\n" if self.msg != "" else "" self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("operate_type", "0") == be.SWIPE_LEFT: # 根据元素左滑动 web_element = self.driver.find_elements_by_id(item["element_info"])[item["index"]] start = web_element.location # 获取控件开始位置的坐标轴 startx = start["x"] starty = start["y"] # 获取控件坐标轴差 size1 = web_element.size width = size1["width"] height = size1["height"] # 计算出控件结束坐标 endX = width + startx endY = height + starty self.driver.swipe(endX, endY, starty, endY) if item.get("operate_type", "0") == be.GET_VALUE: self.get_value.append(result['text']) return True def checkPoint(self, kwargs={}): result = self.check() if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.operate() result = self.check() self.testInfo[0]["msg"] = self.msg statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) return result ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 ''' def check(self, kwargs={}): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 重跑后异常日志 if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not resp["result"]: msg = "请检查元素" + item["element_info"] + "是否存在" self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg result = False if resp["text"] in self.get_value: # 删除后数据对比 msg = "删除数据失败,删除前数据为:" + ".".join(self.get_value) + "当前获取的数据为:" + resp["text"] self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg break else: result = False return result
class PagesObjects: ''' page层 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.get_value = [] self.is_get = False # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, logTest) if not result: print("执行过程中失败,请检查元素是否存在" + item["element_info"]) self.testInfo[0][ "msg"] = "执行过程中失败,请检查元素是否存在" + item["element_info"] self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("--等待下---") if item.get("operate_type", "0") == be.GET_VALUE: re_reulst = re.findall(r'[a-zA-Z\d+\u4e00-\u9fa5]', result) # 只匹配中文,大小写,字母 self.get_value.append("".join(re_reulst)) self.is_get = True # 对比数据 return True ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 msg: 自定义错误日志 contrary:相反检查点,如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比存说明失败 ''' def checkPoint(self, func=None, **kwargs): result = True if self.isOperate: for item in self.testcheck: if kwargs.get("toast", "0") != "0": resp = self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=kwargs["logTest"]) else: resp = self.operateElement.operate(item, self.testInfo, kwargs["logTest"]) if kwargs.get("contrary", "0") != "0" and resp: m = "请检查%s操作是否成功" % item["info"] msg = kwargs["msg"] if kwargs.get("msg", "0") != "0" else m print(msg) self.isOperate = False self.testInfo[0]["msg"] = msg result = False break if kwargs.get("contrary", "0") == "0" and not resp: msg = "请检查元素" + item["element_info"] + "是否存在" print(msg) self.isOperate = False self.testInfo[0]["msg"] = msg result = False break if kwargs.get( "contrary_getval", "0") != "0" and self.is_get and resp in self.get_value: result = False m = "对比数据失败,当前取到到数据为:%s,历史取到数据为:%s" % resp % self.get_value msg = kwargs["msg"] if kwargs.get("msg", "0") != "0" else m print(msg) self.testInfo[0]["msg"] = msg break if kwargs.get( "contrary_getval", "0" ) == "0" and self.is_get and resp not in self.get_value: # 历史数据和实际数据对比 result = False msg = "对比数据失败,获取历史数据为:" + ".".join( self.get_value) + "当前获取的数据为:" + resp print(msg) self.testInfo[0]["msg"] = msg break else: result = False self.operateElement.switchToNative() countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=kwargs["caseName"], driver=self.driver, logTest=kwargs["logTest"], devices=kwargs["devices"], testCase=self.testCase, testCheck=self.testcheck) return result
class PagesObjects: ''' page层 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.is_get = False # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 self.msg = "" ''' 操作步骤 ''' def operate(self): for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] + "," + result.get("text", " ") if not result.get("webview", True): msg = "切换到webview失败,请确定是否在webview页面" print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("--等待下---") if item.get("operate_type", "0") == be.GET_VALUE or item.get("operate_type", "0") == be.GET_CONTENT_DESC: self.get_value.append(result["text"]) self.is_get = True # 对比数据 return True def checkPoint(self, kwargs={}): result = self.check(kwargs) # print(self.driver.page_source) if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = False self.operate() result = self.check(kwargs) self.testInfo[0]["msg"] = self.msg self.operateElement.switchToNative() statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary:相反检查点,传1表示如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比成功,说明失败 check_point: 自定义检查结果 ''' def check(self, kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 如果有重跑机制,成功后会默认把日志传进来 # if kwargs.get("check_point", "0") != "0": # return kwargs["check_point"] if self.isOperate: for item in self.testcheck: if kwargs.get("toast", "0") != "0": resp = self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=self.logTest) else: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if kwargs.get("check", "0") == "0" and not resp["result"]: m = "请检查元素" + item["element_info"] + "是否存在," + item["info"] + " 操作是否成功" self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if kwargs.get("check", "0") == "contrary" and resp["result"]: m = "请检查%s" % item["info"] + "是否成功" self.msg = m_s_g + m print(self.msg) self.testInfo[0]["msg"] = m result = False break if kwargs.get("check", "0") == "contrary_getval" and self.is_get and resp["result"] in self.get_value: m = "对比数据失败,当前取到到数据为:%s,历史取到数据为:%s" % resp["text"] % self.get_value self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if kwargs.get("check", "0") == "0" and self.is_get and resp["text"] not in self.get_value: # 历史数据和实际数据对比 result = False m = "对比数据失败,获取历史数据为:" + ".".join(self.get_value) + ",当前获取的数据为:" + resp["text"] self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: result = False return result
class PagesObjects: ''' page层 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() # self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True self.test_msg = kwargs["test_msg"] self.testInfo = self.test_msg[1]["testinfo"] self.testCase = self.test_msg[1]["testcase"] self.testcheck = self.test_msg[1]["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.is_get = False # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 self.msg = "" ''' 操作步骤 ''' def operate(self): if self.test_msg[0] is False: # 如果用例编写错误 self.isOperate = False return False for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] + "," + result.get("text", " ") if not result.get("webview", True): msg = "切换到webview失败,请确定是否在webview页面" print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("--等待下---") if item.get("operate_type", "0") == be.GET_VALUE or item.get("operate_type", "0") == be.GET_CONTENT_DESC: self.get_value.append(result["text"]) self.is_get = True # 对比数据 return True def checkPoint(self, kwargs={}): result = self.check(kwargs) if self.test_msg[0] is not False: # 如果用例编写正确 if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = False self.operate() result = self.check(kwargs) self.testInfo[0]["msg"] = self.msg self.operateElement.switchToNative() statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary:相反检查点,传1表示如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比成功,说明失败 check_point: 自定义检查结果 ''' def check(self, kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 如果有重跑机制,成功后会默认把日志传进来 # if kwargs.get("check_point", "0") != "0": 自定义检查点 # return kwargs["check_point"] if self.isOperate: for item in self.testcheck: if kwargs.get("check", be.DEFAULT_CHECK) == be.TOAST: result = \ self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=self.logTest)[ "result"] if result is False: m = get_error( {"type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"]}) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if kwargs.get("check", be.DEFAULT_CHECK) == be.DEFAULT_CHECK and not resp["result"]: m = get_error( {"type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"]}) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if kwargs.get("check", be.DEFAULT_CHECK) == be.CONTRARY and resp["result"]: m = get_error({"type": be.CONTRARY, "element_info": item["element_info"], "info": item["info"]}) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = self.msg result = False break # 检查点关键字contrary_getval: 相反值检查点,如果对比成功,说明失败 if kwargs.get("check", be.DEFAULT_CHECK) == be.CONTRARY_GETVAL and self.is_get and resp["result"] \ in self.get_value: m = get_error( {"type": be.CONTRARY_GETVAL, "current": item["element_info"], "history": resp["text"]}) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if kwargs.get("check", be.DEFAULT_CHECK) == be.COMPARE and self.is_get and resp["text"] \ not in self.get_value: # 历史数据和实际数据对比 result = False m = get_error({"type": be.COMPARE, "current": item["element_info"], "history": resp["text"]}) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: result = False return result
class TechZoneDetail: ''' 知识-技术专区详情页 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.getValue = "" ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): import time time.sleep(3) #防止页面元素没有加载完,就滚动页面 self.swipeToUp() for item in self.testCase: if item.get("get_value", "0") == 1 : # 点击之前,取列表中某个元素到文本 if self.operateElement.findElement(item): # 查找获取到元素是否存在 self.getValue = self.driver.find_element_by_xpath(item["element_info"]).get_attribute("text") print("-getValue---") print(self.getValue) self.isOperate = True else: self.isOperate = False self.testInfo[0]["msg"] = "获取列表元素到文本值失败" break result = self.operateElement.operate(item, self.testInfo, logTest) # 执行操作 if not result: print("操作失败") self.testInfo[0]["msg"] = "执行过程中失败" self.isOperate = False break ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 ''' def checkPoint(self, caseName, logTest, devices): result = False if self.isOperate: time.sleep(2) # 进入详情后,页面即使没有加载完成默认也会现实次标签,默认值为博客详情,等待2秒,尽量让页面加载完成后,加载后台数据 # result = self.operateElement.findElement(self.testcheck) # 检查点 self.operateElement.switchToWebview() # 切换到webview temp = self.operateElement.operate(self.testcheck, self.testInfo, logTest) if not temp: print("查找元素" + self.testcheck["element_info"] + "失败") self.isOperate = False self.testInfo[0]["msg"] = "请检查元素" + self.testcheck["element_info"] + "是否存在" result = False elif temp == self.getValue: # 对比列表中到一条数据和详情页数据是否相同 result = True else: result = False self.testInfo[0]["msg"] = "详情页值为="+temp+";列表获取到值为:"+self.getValue + "。两者值不相等" self.driver.switch_to.context("NATIVE_APP") # 切换到native,还原 self.swipeToUp() countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=caseName, driver=self.driver, logTest=logTest, devices=devices, testCase=self.testCase, testCheck=self.testcheck) return result def swipeToUp(self): width = self.driver.get_window_size()["width"] height = self.driver.get_window_size()["height"] # self.driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, 600) self.driver.swipe(0, 1327, 500, 900, 1000) print("--swipeToDown--")
class TechZoneList: ''' 知识-技术专区列表页面 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.getValue = [] ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, logTest) if not result: print("操作失败") self.isOperate = False self.testInfo[0]["msg"] = "执行过程中失败,请检查元素是否存在" break elif item.get("operate_type", "0") == Element.GET_VALUE: self.getValue.append(result) ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 ''' def checkPoint(self, caseName, logTest, devices): time.sleep(2) # 从详情页返回到列表后,列表数据需要时间更新 self.swipeToUp() result = False if self.isOperate: for item in self.testcheck: temp = self.operateElement.operate(item, self.testInfo, logTest) if not temp: print("查找元素" + item["element_info"] + "失败") self.isOperate = False self.testInfo[0][ "msg"] = "请检查元素" + item["element_info"] + "是否存在" result = False break elif temp in self.getValue: result = True # if self.driver.find_element_by_xpath(item["element_info"]).get_attribute("text") in self.getValue: # 对比列表中的三条数据是否等于详情的三条数据 else: result = False # 只要有一条数据不匹配,用例就失败 self.testInfo[0][ "msg"] = "首页到知识专区到列表三条数据和详情页到三条数据对不上。详情页三条数据为:" + str( self.getValue) + "。首页当前匹配数据为:" + temp break countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=caseName, driver=self.driver, logTest=logTest, devices=devices, testCase=self.testCase, testCheck=self.testcheck) return result # swipe start_x: 200, start_y: 200, end_x: 200, end_y: 400, duration: 2000 从200滑动到400 def swipeToUp(self): # time.sleep(1) width = self.driver.get_window_size()["width"] height = self.driver.get_window_size()["height"] # self.driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, 600) self.driver.swipe(0, 1327, 500, 800, 1000) print("--swipeToDown--")
class CardsSortPage: ''' 滑动删除历史记录 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.location = [] self.msg = "" ''' 操作步骤 logTest 日记记录器 ''' def operate(self): for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item["element_info"] print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("operate_type", "0") == "location": app = {} web_element = self.driver.find_elements_by_id( item["element_info"])[item["index"]] start = web_element.location # 获取控件开始位置的坐标轴 app["startX"] = start["x"] app["startY"] = start["y"] # 获取控件坐标轴差 size1 = web_element.size width = size1["width"] height = size1["height"] # 计算出控件结束坐标 endX = width + app["startX"] endY = height + app["startY"] app["endX"] = endX - 20 app["endY"] = endY - 60 self.location.append(app) # self.driver.swipe(endX, endY, starty, endY) if item.get("operate_type", "0") == be.GET_VALUE: self.get_value.append(result["text"]) if item.get("is_swpie", "0") != "0": print(self.location) self.driver.swipe(self.location[0]["endX"], self.location[0]["endY"], self.location[1]["endX"], self.location[1]["endY"] + 10) return True def checkPoint(self, kwargs={}): result = self.check() if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.operate() self.get_value = [] self.location = "" result = self.check() self.testInfo[0]["msg"] = self.msg statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) return result ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary 相反检查点,意思就是如果检查结果为真,检查点就是失败 ''' def check(self): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 重跑后异常日志 if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not resp["result"]: msg = "请检查元素" + item["element_info"] + "是否存在" self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg result = False if resp['text'] not in self.get_value: # 删除后数据对比 msg = "卡片排序失败" + str( self.get_value) + "当前首页第一条卡片数据为:" + resp[ "text"] + "排序成功的第一个卡片值为:" + ".".join( self.get_value) self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg break else: result = False return result
class CardsSortPage: ''' 滑动删除历史记录 isOperate: 操作失败,检查点就失败,kwargs: WebDriver driver, String path(yaml配置参数) ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": # 若为空,重新打开app self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.get_value = [] self.location = [] ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, logTest) if not result: print("执行过程中失败,请检查元素是否存在" + item["element_info"]) self.testInfo[0]["msg"] = "执行过程中失败,请检查元素是否存在" + item["element_info"] self.isOperate = False return False if item.get("operate_type", "0") == "location": app = {} web_element = self.driver.find_elements_by_id(item["element_info"])[item["index"]] start = web_element.location # 获取控件开始位置的坐标轴 app["startX"] = start["x"] app["startY"] = start["y"] # 获取控件坐标轴差 size1 = web_element.size width = size1["width"] height = size1["height"] # 计算出控件结束坐标 endX = width + app["startX"] endY = height + app["startY"] app["endX"] = endX - 20 app["endY"] = endY - 60 self.location.append(app) # self.driver.swipe(endX, endY, starty, endY) if item.get("operate_type", "0") == be.GET_VALUE: re_reulst = re.findall(r'[a-zA-Z\d+\u4e00-\u9fa5]', result) # 只匹配中文,大小写,字母 self.get_value.append("".join(re_reulst)) if item.get("is_swpie", "0") != "0": print(self.location) self.driver.swipe(self.location[0]["endX"], self.location[0]["endY"], self.location[1]["endX"], self.location[1]["endY"]+10) return True ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary 相反检查点,意思就是如果检查结果为真,检查点就是失败 ''' def checkPoint(self, **kwargs): result = True if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, kwargs["logTest"]) if not resp: msg = "请检查元素" + item["element_info"] + "是否存在" print(msg) self.isOperate = False self.testInfo[0]["msg"] = msg result = False if resp not in self.get_value: # 删除后数据对比 msg = "卡片排序失败" + str(self.get_value) + "当前首页第一条卡片数据为:" + resp + "排序成功的第一个卡片值为:"+".".join(self.get_value) print(msg) self.testInfo[0]["msg"] = msg break else: result = False countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=kwargs["caseName"], driver=self.driver, logTest=kwargs["logTest"], devices=kwargs["devices"], testCase=self.testCase, testCheck=self.testcheck) return result
class HistorySwipeDelPage: def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") == "0": self.driver.launch_app() self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True test_msg = getYam(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = [] self.msg = "" def operate(self): for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "Falló durante la ejecución, verifique si el elemento existe" + item["element_info"] m_s_g = self.msg + "\n" if self.msg != "" else "" self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("operate_type", "0") == be.SWIPE_LEFT: web_element = self.driver.find_elements_by_id(item["element_info"])[item["index"]] start = web_element.location startx = start["x"] starty = start["y"] size1 = web_element.size width = size1["width"] height = size1["height"] endX = width + startx endY = height + starty self.driver.swipe(endX, endY, starty, endY) if item.get("operate_type", "0") == be.GET_VALUE: self.get_value.append(result['text']) return True def checkPoint(self, kwargs={}): result = self.check() if result is not True and be.RE_CONNECT: self.msg = "El caso de uso falló y se volvió a conectar una vez, el motivo del fallo:" + self.testInfo[0][ "msg"] self.logTest.buildStartLine(self.caseName + "_No se pudo volver a conectar") self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.operate() result = self.check() self.testInfo[0]["msg"] = self.msg statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) return result def check(self, kwargs={}): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" if self.isOperate: for item in self.testcheck: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not resp["result"]: msg = "Por favor marque el elemento" + item["element_info"] + "existe" self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg result = False if resp["text"] in self.get_value: msg = "Error en la eliminación de datos, los datos antes de la eliminación son: " + ".".join( self.get_value) + "Los datos adquiridos actualmente son:" + resp["text"] self.msg = m_s_g + msg print(msg) self.testInfo[0]["msg"] = msg break else: result = False return result
class Write: ''' kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 logTest: 日记记录 testInfo:用例介绍 testCase:操作步骤 prefix: 前置条件 ''' def __init__(self, **kwargs): self.driver = kwargs["driver"] self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True _getYam = getYam(self.path) self.testInfo = _getYam["testinfo"] self.testCase = _getYam["testcase"] self._prefix = _getYam["prefix"] ''' 前置条件 logTest 日记记录器 ''' def prefix(self, logTest): for item in self._prefix: if item.get("isWebView", "1") == True: self.operateElement.switchToContext(item.get["isWebView"]) result = self.operateElement.operate(item, self.testInfo, logTest) if not result: print("前置条件失败") self.isOperate = False break ''' 操作步骤 logTest 日记记录器 ''' def operate(self, logTest): self.prefix(logTest) if not self.isOperate: print("前置条件失败") # 如果前置条件失败,操作步骤肯定也失败 else: for item in self.testCase: result = self.operateElement.operate(item, self.testInfo, logTest) if not result: print("操作失败") self.isOperate = False break ''' 检查点 ''' def checkPoint(self, caseName, logTest, devices): result = False if not self.isOperate: print("操作失败,检查点失败") else: check = getYam(self.path)["check"] result = self.operateElement.findElement(check) # 检查点 countSum(result) countInfo(result=result, testInfo=self.testInfo, caseName=caseName, driver=self.driver, logTest=logTest, devices=devices) return result
class PagesObjects: ''' page层 kwargs: WebDriver driver, String path(yaml配置参数) isOperate: 操作失败,检查点就失败 testInfo: testCase: ''' def __init__(self, kwargs): self.driver = kwargs["driver"] if kwargs.get("launch_app", "0") != "0": # 若为空,重新打开app self.driver.launch_app() # self.path = kwargs["path"] self.operateElement = OperateElement(self.driver) self.isOperate = True self.test_msg = kwargs["test_msg"] self.testInfo = self.test_msg[1]["testinfo"] self.testCase = self.test_msg[1]["testcase"] self.testcheck = self.test_msg[1]["check"] self.device = kwargs["device"] self.logTest = kwargs["logTest"] self.caseName = kwargs["caseName"] self.get_value = self.test_msg[1]["check"] self.is_get = True # 检查点特殊标志,结合get_value使用。若为真,说明检查点要对比历史数据和实际数据 self.msg = "" ''' 操作步骤 ''' def operate(self): if self.test_msg[0] is False: # 如果用例编写错误 self.isOperate = False return False for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item[ "element_info"] + "," + result.get("text", " ") if not result.get("webview", True): msg = "切换到webview失败,请确定是否在webview页面" print(msg) self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("------->>等待:" + str(item["is_time"]) + "秒...") if item.get("operate_type", "0") == be.GET_VALUE or item.get( "operate_type", "0") == be.GET_CONTENT_DESC: self.get_value.append(result["text"]) self.is_get = True # 对比数据 return True def checkPoint(self, kwargs={}): result = self.check(kwargs) if self.test_msg[0] is not False: # 如果用例编写正确 if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.logTest.buildStartLine(self.caseName + "_失败重连") # 记录日志 self.operateElement.switchToNative() self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = True self.operate() result = self.check(kwargs) self.testInfo[0]["msg"] = self.msg self.operateElement.switchToNative() statistics_result(result=result, testInfo=self.testInfo, caseName=self.caseName, driver=self.driver, logTest=self.logTest, devices=self.device, testCase=self.testCase, testCheck=self.testcheck) ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary:相反检查点,传1表示如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比成功,说明失败 check_point: 自定义检查结果 ''' def check(self, kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 如果有重跑机制,成功后会默认把日志传进来 # if kwargs.get("check_point", "0") != "0": 自定义检查点 # return kwargs["check_point"] if self.isOperate: for item in self.testcheck: if item.get("check", be.DEFAULT_CHECK) == be.TOAST: result = \ self.operateElement.toast(item["element_info"], testInfo=self.testInfo, logTest=self.logTest)[ "result"] if result is False: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: resp = self.operateElement.operate(item, self.testInfo, self.logTest, self.device) if item.get("check", be.DEFAULT_CHECK ) == be.DEFAULT_CHECK and not resp["result"]: m = get_error({ "type": be.DEFAULT_CHECK, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if item.get( "check", be.DEFAULT_CHECK) == be.CONTRARY and resp["result"]: m = get_error({ "type": be.CONTRARY, "element_info": item["element_info"], "info": item["info"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = self.msg result = False break # 检查点关键字contrary_getval: 相反值检查点,如果对比成功,说明失败 if item.get("check", be.DEFAULT_CHECK) == be.CONTRARY_GETVAL and self.is_get and resp["result"] \ in self.get_value: m = get_error({ "type": be.CONTRARY_GETVAL, "current": item["element_info"], "history": resp["text"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break print(resp) if item.get("check", be.DEFAULT_CHECK ) == be.COMPARE and self.is_get and resp.get( "text", "NoSuchElementError") != str( item["msg"]): # 历史数据和实际数据对比 result = False m = get_error({ "type": be.COMPARE, "current": resp.get("text", "NoSuchElementError"), "expectValue": item["msg"] }) self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: result = False return result
class PageObjects: def __init__(self, kwargs): self.driver = kwargs['driver'] self.path = kwargs['path'] self.isOperate = True self.operateElement = OperateElement(self.driver) test_msg = readyaml(self.path) self.testInfo = test_msg["testinfo"] self.testCase = test_msg["testcase"] self.testcheck = test_msg["check"] self.is_get = False self.msg = "" def operate(self): print(self.testCase) for item in self.testCase: m_s_g = self.msg + "\n" if self.msg != "" else "" result = self.operateElement.operate(item, self.testInfo) if not result["result"]: msg = "执行过程中失败,请检查元素是否存在" + item[ "element_info"] + "," + result.get("text", " ") if not result.get("webview", True): msg = "切换到webview失败,请确定是否在webview页面" self.msg = m_s_g + msg self.testInfo[0]["msg"] = msg self.isOperate = False return False if item.get("is_time", "0") != "0": time.sleep(item["is_time"]) # 等待时间 print("--等待下---") if item.get("operate_type", "0") == be.GET_VALUE or item.get( "operate_type", "0") == be.GET_CONTENT_DESC: self.get_value.append(result["text"]) self.is_get = True # 对比数据 return True def checkPoint(self, kwargs={}): result = self.check(kwargs) # print(self.driver.page_source) if result is not True and be.RE_CONNECT: self.msg = "用例失败重连过一次,失败原因:" + self.testInfo[0]["msg"] self.driver.launch_app() self.isOperate = True self.get_value = [] self.is_get = False self.operate() result = self.check(kwargs) self.testInfo[0]["msg"] = self.msg ''' 检查点 caseName:测试用例函数名 用作统计 logTest: 日志记录 devices 设备名 contrary:相反检查点,传1表示如果检查元素存在就说明失败 toast: 表示提示框检查点 contrary_getval: 相反值检查点,如果对比成功,说明失败 check_point: 自定义检查结果 ''' def check(self, kwargs): result = True m_s_g = self.msg + "\n" if self.msg != "" else "" # 如果有重跑机制,成功后会默认把日志传进来 # if kwargs.get("check_point", "0") != "0": # return kwargs["check_point"] if self.isOperate: for item in self.testcheck: if kwargs.get("toast", "0") != "0": resp = self.operateElement.toast(item["element_info"]) else: resp = self.operateElement.operate(item, self.testInfo) if kwargs.get("check", "0") == "0" and not resp["result"]: m = "请检查元素" + item["element_info"] + "是否存在," + item[ "info"] + " 操作是否成功" self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if kwargs.get("check", "0") == "contrary" and resp["result"]: m = "请检查%s" % item["info"] + "是否成功" self.msg = m_s_g + m print(self.msg) self.testInfo[0]["msg"] = m result = False break if kwargs.get( "check", "0") == "contrary_getval" and self.is_get and resp[ "result"] in self.get_value: m = "对比数据失败,当前取到到数据为:%s,历史取到数据为:%s" % resp[ "text"] % self.get_value self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m result = False break if kwargs.get("check", "0") == "0" and self.is_get and resp[ "text"] not in self.get_value: # 历史数据和实际数据对比 result = False m = "对比数据失败,获取历史数据为:" + ".".join( self.get_value) + ",当前获取的数据为:" + resp["text"] self.msg = m_s_g + m print(m) self.testInfo[0]["msg"] = m break else: result = False return result