def buy_diamond_step(self, nString=""): """购买钻石步骤 商品ID""" MyData.getUsercurrency() increment = MyData.UserData_dir["diamond"] self.click_buy_diamond(nString) self.mysleep(3) self.dealwith_error() self.mysleep(3) self.click_keytobuy() if self.android_tryfind("android.widget.RadioButton", description="购买身份验证", waitTime=2): androidpoco = self.androidpoco("android.widget.RadioButton")[0] self.findClick_childobject(androidpoco, description="一律启用", waitTime=1, sleeptime=1) self.mysleep(3) self.click_affirm() self.mysleep(3) self.click_claim_shop() # 购买完后展示用户信息 MyData.getUsercurrency() mylog.info("用户信息【{}】".format(MyData.UserData_dir)) print("钻石", MyData.UserData_dir["diamond"]) MyData.UserData_dir[ "increment"] = MyData.UserData_dir["diamond"] - increment self.Shopmodule_info["diamond"] = MyData.UserData_dir["diamond"] self.Shopmodule_info["increment"] = MyData.UserData_dir["increment"] return self.Shopmodule_info
def findClick_childobject(self, ClickPoco: poco, description="", waitTime=1, tryTime=1, sleeptime=0.1, clickPos=None): """用于关联父级才能点击到的元素""" waitTime = waitTime + float(MyData.EnvData_dir["sleepLevel"]) if ClickPoco.wait(waitTime).exists(): print("发现{0}".format(description)) mylog.info("查找点击元素-【{}】--成功".format(description)) if clickPos is None: ClickPoco.click() else: ClickPoco.click(clickPos) sleep(sleeptime) mylog.info("点击元素-【{}】--成功".format(description)) return True else: mylog.error("查找-【{}】-元素失败".format(description)) log(PocoNoSuchNodeException("点击-【{}】-元素失败".format(description)), desc="点击元素失败", snapshot=True) raise PocoNoSuchNodeException("点击-【{}】-元素失败".format(description))
def chat_typeconf(self, chat_id, select_id): """选项判断""" if chat_id in MyData.chat_type_dir: description = MyData.chat_type_dir[chat_id][0] print("description", description) mylog.info("description") for val in range(1, len(MyData.chat_type_dir[chat_id])): clickname = MyData.chat_type_dir[chat_id][val] if type(clickname) == int: sleep(clickname) else: self.findClick_try(clickname, clickname, description=description, waitTime=2, sleeptime=2) if select_id == 0: touch(self._POS) sleep(0.2) else: if self.poco("UIChapterSelectList").child( "Item").exists(): # "UISelectList")老版本 Item0 = self.poco("UIChapterSelectList").child("Item")[0] self.findClick_childobject(Item0, description="点击选项") if self.find_try("UIQuickPayFrame", description="快捷购买", waitTime=0.2): self.myShop.quick_purchase() self.touchtime = self.touchtime + 1
def __init__(self): if G.DEVICE == None: if not cli_setup(): conf = MyData.EnvData_dir[ "device"] + "://" + MyData.EnvData_dir[ "ADBip"] + "/" + MyData.EnvData_dir["ADBdevice"] method = MyData.EnvData_dir["method"] if "127" in MyData.EnvData_dir["ADBdevice"]: method = MyData.EnvData_dir["simulator"] auto_setup(__file__, logdir=path_LOG_DIR, devices=[ conf + method, ], project_root=path_BASE_DIR) if MyData.DeviceData_dir["androidpoco"] == None: MyData.DeviceData_dir[ "androidpoco"] = AndroidUiautomationPoco() mylog.info("完成android原生元素定位方法初始化【{}】".format( MyData.DeviceData_dir["androidpoco"])) print("完成android原生元素定位方法初始化【{}】".format( MyData.DeviceData_dir["androidpoco"])) # ADBdevice = MyData.EnvData_dir["ADBdevice"] # print(MyData.mobileconf_dir["Notch_Fit"]) # if ADBdevice in MyData.mobileconf_dir["Notch_Fit"]: # print("报错把刚开始了解") # MyData.DeviceData_dir["androidpoco"].use_render_resolution(True, MyData.mobileconf_dir["Notch_Fit"][ # ADBdevice]) # mylog.info("androidpoco完成【{}】刘海屏特殊渲染处理".format(ADBdevice)) # print("androidpoco完成【{}】刘海屏特殊渲染处理".format(ADBdevice)) print("DEVIEC:", G.DEVICE)
def stopGame(self): stop_app(MyData.EnvData_dir["packageName"]) MyData.DeviceData_dir["poco"] = None # MyData.DeviceData_dir["androidpoco"] = None print("停止游戏") mylog.info("停止游戏") return True
def notchfit_childobject(self, ClickPoco: poco, description="", waitTime=0.5, tryTime=1, sleeptime=0, log=True): """用于关联父级才能点击到的元素""" waitTime = waitTime + float(MyData.EnvData_dir["sleepLevel"]) # ADBdevice=MyData.EnvData_dir["ADBdevice"] # if ADBdevice in MyData.mobileconf_dir["Notch_Fit"]: # self.poco.use_render_resolution(True, MyData.mobileconf_dir["Notch_Fit"][ADBdevice]) if ClickPoco.wait(waitTime).exists(): print("发现{0}".format(description)) mylog.info("查找点击元素-【{}】--成功".format(description)) ClickPoco.click() sleep(sleeptime) mylog.info("点击元素-【{}】--成功".format(description)) # self.poco.use_render_resolution(False, MyData.mobileconf_dir["Notch_Fit"][ADBdevice]) return True else: mylog.error("查找-【{}】-元素失败".format(description)) log(PocoNoSuchNodeException("点击-【{}】-元素失败".format(description)), desc="点击元素失败", snapshot=True) raise PocoNoSuchNodeException("点击-【{}】-元素失败".format(description))
def findClick_object(self, findName, ClickName, description="", waitTime=1, tryTime=1, sleeptime=0): """用寻找目标,后并点击""" waitTime = waitTime + float(MyData.EnvData_dir["sleepLevel"]) print("正在寻找{0}".format(description)) if self.poco(ClickName).wait(waitTime).exists(): print("发现{0}元素,并点击".format(description)) mylog.info("查找点击-【{}】--元素成功".format(description)) self.poco(ClickName).click() mylog.info("点击元素-【{}】--成功".format(description)) print("点击元素-【{}】--成功".format(description)) sleep(sleeptime) return True else: print("查找{0}失败".format(description)) mylog.error("查找点击元素-【{}】--失败".format(findName)) log(PocoNoSuchNodeException("点击-【{}】-元素失败".format(description)), desc="点击元素失败", snapshot=True) raise PocoNoSuchNodeException("点击-【{}】-元素失败".format(description))
def starGame(self): # 启动游戏 wake() print("尝试启动游戏") mylog.info("启动游戏") start_app(MyData.EnvData_dir["packageName"]) print("等待加载") self.isStarGame = True
def resetEnv(self, k): print(self.Runlist_dir[k]) if "登陆" in self.Case_info[k]["casename"]: pass else: mylog.info("----------正在进行异常重启------") test_startgame(0) test_discoverPopup() mylog.info("--------完成异常重启------")
def shop_buy_diamondcard(self): """购买钻卡""" MyData.getUsercurrency() mylog.info("用户信息【{}】".format(MyData.UserData_dir)) self.click_buy_diamondcard() time.sleep(10) self.click_keytobuy() self.mysleep(5) self.click_claim_shop() #购买完后的用户数据 MyData.getUsercurrency() mylog.info("用户信息【{}】".format(MyData.UserData_dir))
def partReport(self, htmlname, k, __title__): """分步报告""" mylog.info( "-------------------------【{0}】执行完毕-----------------".format( __title__)) print("-----执行完毕-----") outputpath = os.path.join(path_REPORT_DIR, htmlname) self.writelogs() simple_report(__file__, logpath=path_LOG_DIR, output=outputpath) self.Case_info[k]["repeattime"] = self.Case_info[k]["repeattime"] - 1 auto_setup(logdir=path_LOG_DIR) mylog.info("完成html测试报告,等待生产录制文件需要一定时间")
def shop_buy_packages1(self): """购买礼包""" MyData.getUsercurrency() mylog.info("用户信息【{}】".format(MyData.UserData_dir)) self.click_buy_packges1() time.sleep(10) self.click_keytobuy() time.sleep(20) self.click_claim_shop() #购买完后的用户数据 MyData.getUsercurrency() mylog.info("用户信息【{}】".format(MyData.UserData_dir))
def gameloading(self, login=1): # 游戏是否加载完成判断 while self.poco("Slider").wait(1).exists(): self.Popo_Errorinfo() self.Popup_login(login=1) if float(COM_utilities.clock("stop")) > 360: print("游戏加载失败。。。") log(Exception("游戏加载失败。。。"), snapshot=True) raise Exception if self.GameLoaded_info["loadtime"] is None: self.GameLoaded_info["loadtime"] = float( COM_utilities.clock("stop")) - 2 mylog.info("完成游戏加载,加载时间为{0}秒".format(self.GameLoaded_info["loadtime"])) return True
def uninstallGame(self, package=MyData.EnvData_dir["packageName"], **kwargs): # 卸载应用 packageName = MyData.EnvData_dir["packageName"] mypath = MyData.EnvData_dir["packagepath"] my_file = Path(mypath) if my_file.is_file(): list = G.DEVICE.list_app(third_only=True) for i in list: if i == packageName: uninstall(package, **kwargs) mylog.info("已经卸载【{}】--apk包".format(packageName)) return True else: mylog.info("未找到【{}】--apk包".format(packageName))
def runing(self): for k, v in self.Runlist_dir.items(): repeattime = self.Case_info[k]["repeattime"] while (self.Case_info[k]["repeattime"] > 0): __author__ = self.Case_info[k]["caseauthor"] __title__ = self.Case_info[k]["casename"] + str( repeattime - (self.Case_info[k]["repeattime"] - 1)) __desc__ = self.Case_info[k]["casedec"] recordfile = __title__ + ".mp4" htmlname = self.Case_info[k]["reportname"] + str(repeattime - ( self.Case_info[k]["repeattime"] - 1)) + ".html" # logname = self.Case_info[k]["reportname"] + str(repeattime - (self.Case_info[k]["repeattime"] - 1)) + "log.txt" try: start_record() mylog.info("【{0}】启动录制成功".format(__title__)) except: mylog.info("【{0}】启动录制失败".format(__title__)) try: self.runcase(self.Runlist_dir[k]) self.Case_info[k]["repeattime"] = 0 except Exception as e: sleep(1) mylog.error("------第出现异常", e) log(e, "------出现异常----------") self.resetEnv(k) finally: self.partReport(htmlname=htmlname, __title__=__title__, k=k) try: stop_record(recordfile) mylog.info("【{0}】生成录制文件成功".format(__title__)) except: mylog.info("【{0}】生成录制文件失败".format(__title__))
def Popo_Errorinfo(self): if self.android_tryfind("android:id/button1", description="Google提示"): self.android_findClick("android:id/button1", "android:id/button1", description="Google框架提示处理") mylog.error("检测到未安装谷歌框架,无法执行相关操作") if self.find_try("Context", description="加载中..."): TXT = self.poco("Context").get_TMPtext() self.GameLoaded_info["ErrorTxt"].append(TXT) self.click_object("CenterBtn", description="Try again", waitTime=5) mylog.info("异常弹框,{0}".format(TXT)) # GameLoaded1=GameLoaded() # GameLoaded1.Popo_Errorinfo()
def __init__(self): CommonDevices.__init__(self) if MyData.DeviceData_dir["poco"] == None: MyData.DeviceData_dir["poco"] = UnityPoco() ADBdevice = MyData.EnvData_dir["ADBdevice"] if ADBdevice in MyData.mobileconf_dir["Notch_Fit"]: MyData.DeviceData_dir["poco"].use_render_resolution( True, MyData.mobileconf_dir["Notch_Fit"][ADBdevice]) mylog.info("完成【{}】刘海屏特殊渲染处理".format(ADBdevice)) print("完成【{}】刘海屏特殊渲染处理".format(ADBdevice)) mylog.info("完成Unity元素定位方法初始化【{}】".format( MyData.DeviceData_dir["poco"])) print("完成Unity元素定位方法初始化【{}】".format(MyData.DeviceData_dir["poco"])) self.poco = MyData.DeviceData_dir["poco"] self.androidpoco = MyData.DeviceData_dir["androidpoco"]
def click_object(self, clickName, waitTime=1, description="", sleeptime=0): """直接点击,不存在会报错""" waitTime = waitTime + float(MyData.EnvData_dir["sleepLevel"]) try: self.poco(clickName).wait(waitTime).click() mylog.info("点击元素-【{}】--成功".format(description)) print("点击元素-【{}】--成功".format(description)) sleep(sleeptime) return True except Exception as e: mylog.error("点击【{0}】出现未知错误,{1}".format(description, e)) return False log(PocoNoSuchNodeException("点击-【{}】-元素失败".format(description)), desc="点击元素失败", snapshot=True) raise PocoNoSuchNodeException("点击-【{}】-元素失败".format(description))
def UIAlterPoP(self): if self.find_try("AlterView", description="文本弹框"): txt = self.poco("UIAlter").child("AlterView").child( "Title").get_TMPtext() print("弹框类型:", txt) mylog.info("发现-【{}】-类型弹框".format(txt)) Btn = self.AlterTxt.get(txt) self.Popuplist.append(txt) print("准备点击按钮", Btn) try: self.poco(Btn).click() print("点击按钮", Btn) sleep(2) except: print("未成功点击按钮") return False
def clear(self): """清空之前的报告和文件""" fileNamelist = [path_LOG_DIR, path_REPORT_DIR, path_RES_DIR] for fileName in fileNamelist: filelist = os.listdir(fileName) for f in filelist: filepath = os.path.join(fileName, f) if os.path.isfile(filepath): os.remove(filepath) # elif os.path.isdir(filepath): # shutil.rmtree(filepath, True) path = os.path.join(path_LOG_MY, "logging.log") with open(path, 'w') as f1: f1.seek(0) f1.truncate() mylog.info("完成文件清空")
def dialogueEndPOP(self): """章节尾弹框""" # self.poco.wait_for_any() if self.find_try("UIChapterContinue", description="章节尾弹框", waitTime=1): stoptime = str(clock("stop")) + "秒" self.ReadBook_info["阅读时长"] = stoptime while (self.find_try("UIChapterContinue", description="章节尾弹框", waitTime=1)): if self.poco("UIRewardPopup").wait(5).exists(): print("检测到有弹框,结束阅读") if self.poco("UIRewardPopup").wait(2).exists(): print("弹框类型为奖励弹框") self.findClick_try("RoleCard", "BtnGet", description="角色卡奖励", waitTime=0.5) self.findClick_try("AdGroup", "BtnGet", description="付费用户章节尾奖励", waitTime=0.5) else: print("未检测到任何弹框") self.findClick_try("UIChapterStar", "CloseBtn", description="阅读分享", waitTime=1) self.findClick_try("UIChapterContinue", "BtnGet", description="章节尾奖励", waitTime=0.5, sleeptime=2) self.findClick_try("UIChapterContinue", "ContinueBtn", description="章节尾弹框", waitTime=0.5, sleeptime=2) self.isstopRead = True self.ReadBook_info["点击次数"] = self.touchtime mylog.info("结束阅读") print("结束阅读") sleep(3) self.ReadBook_info["章节尾弹框详情"] = self.Popuplist self.ReadBook_info["阅读情况"] = "完成阅读" return True
def find_childobject(self, findPoco: poco, description="", waitTime=1, tryTime=3, sleeptime=0): """用于关联父级才能找到的元素""" waitTime = waitTime + float(MyData.EnvData_dir["sleepLevel"]) if findPoco.wait(waitTime).exists(): print("发现{0}".format(description)) mylog.info("查找点击元素-【{}】--成功".format(description)) return True else: mylog.error("查找-【{}】-元素失败".format(description)) log(PocoNoSuchNodeException("点击-【{}】-元素失败".format(description)), desc="点击元素失败", snapshot=True) raise PocoNoSuchNodeException("点击-【{}】-元素失败".format(description))
def find_object(self, findName, description="", waitTime=1, tryTime=1, sleeptime=0): """寻找目标""" waitTime = waitTime + float(MyData.EnvData_dir["sleepLevel"]) print("正在寻找{0}".format(description)) if self.poco(findName).wait(waitTime).exists(): print("发现{0}".format(description)) sleep(sleeptime) mylog.info("等待元素-【{}】--加载成功".format(description)) return True log(PocoNoSuchNodeException("等待-【{}】-元素超时".format(description)), desc="等待元素超时", snapshot=True) raise PocoNoSuchNodeException("等待-【{}】-元素超时".format(description))
def findClick_Image(self, filename, record_pos, description="图片", resolution=(1600, 2560), tryTime=1, waitTime=5): """点击图片""" width = G.DEVICE.display_info['width'] height = G.DEVICE.display_info['height'] file_path = os.path.join(path_RESOURCE_IMAGE, filename) # 1080, 1920 # record_pos = (0.432, 0.068), try: pos = wait(Template((file_path), resolution=resolution), timeout=3) touch(pos) mylog.info("点击-【{}】-元素成功".format(description)) print("点击-【{}】-元素成功".format(description)) return True except: return False
def writelogs(self): """转存log到logs""" try: log_file = open(self.logpath, "r") logs_file = open(self.logspath, "a") lines = log_file.readlines() for val in range(len(lines)): # alllog_file = open(alllogspath, "a") # alllog_file.write(lines[val]) # if "assert_equal" in lines[val] or "traceback" in lines[val]: logs_file.write(lines[val]) except Exception as e: print("转存log到logs失败", e) mylog.error("转存log到logs失败", e) else: print("转存log到logs成功") mylog.info("转存log到logs成功") finally: log_file.close() logs_file.close()
def android_tryfind(self, findName, description="", waitTime=0.2, tryTime=1, sleeptime=0): """尝试寻找原生,不一定存在""" while (tryTime > 0): tryTime = tryTime - 1 try: print("尝试寻找{0}".format(description)) mylog.info("尝试寻找-【{}】--".format(description)) if self.androidpoco(findName).wait(waitTime).exists(): print("发现{0}".format(description)) sleep(sleeptime) self.Popuplist.append(description) mylog.info("尝试寻找-【{}】-元素成功".format(description)) return True except: return False
def notchfit__Click_try(self, findName, ClickName, description="", waitTime=0.5, tryTime=1, sleeptime=0, log=True, POCOtype=None): """解决上部分黑屏问题,更改渲染""" if POCOtype == "Androidpoco": poco = self.androidpoco else: poco = self.poco # ADBdevice=MyData.EnvData_dir["ADBdevice"] # if ADBdevice in MyData.mobileconf_dir["Notch_Fit"]: # poco.use_render_resolution(True, MyData.mobileconf_dir["Notch_Fit"][ADBdevice]) try: print("尝试寻找{0}".format(description)) # gameobject = self.poco(findName) if poco(findName).wait(waitTime).exists(): print("发现{0}".format(description)) # if ADBdevice in MyData.mobileconf_dir["Notch_Fit"]: # poco.use_render_resolution(True, MyData.mobileconf_dir["Notch_Fit"][ADBdevice]) if poco(ClickName).wait(waitTime + 1).exists(): print("发现{0}按钮,并点击".format(ClickName)) poco(ClickName).click() sleep(sleeptime) self.Popuplist.append(description) mylog.info("尝试点击-【{}】-元素成功并加入弹框列表".format(description)) # poco.use_render_resolution(False, MyData.mobileconf_dir["Notch_Fit"][ADBdevice]) return True else: mylog.info("尝试点击-【{}】-元素失败".format(description)) print("未触发点击") except: log(Exception("点击-【{}】-元素失败".format(description)), desc="点击元素失败", snapshot=True) mylog.error("尝试点击-【{}】-元素失败".format(description)) return False
def installGame(self): # 安装应用 packageName = MyData.EnvData_dir["packageName"] print(MyData.EnvData_dir["packagepath"]) mypath = MyData.EnvData_dir["packagepath"] my_file = Path(mypath) if my_file.is_file(): list = G.DEVICE.list_app(third_only=True) for i in list: if i == packageName: print("{0}包已经安装".format(packageName)) self.GameStart_info["安装apk"] = True return True print("正在安装apk这时间可能有点长", packageName) # 需要解决安装很慢。。。。。。。。。。。。。。 mylog.info("正在安装【{}】--apk包".format(packageName)) G.DEVICE.install_app(mypath) mylog.info("已经安装【{}】--apk包".format(packageName)) self.GameStart_info["安装apk"] = True return True else: print("未找到对应的安装包") return False
def findchildobject_try(self, findPoco: poco, description="", waitTime=0.2, tryTime=1, sleeptime=0): """尝试寻找,不一定存在""" while (tryTime > 0): tryTime = tryTime - 1 try: gameobject = findPoco print("尝试寻找{0}".format(description)) mylog.info("尝试寻找-【{}】--".format(description)) if gameobject.wait(waitTime).exists(): print("发现{0}".format(description)) sleep(sleeptime) mylog.info("尝试寻找-【{}】-元素成功".format(description)) return True except: return False
def android_findClick(self, findName, ClickName, description="", waitTime=1, tryTime=1, sleeptime=0): """用寻找目标,后并点击""" waitTime = waitTime + float(MyData.EnvData_dir["sleepLevel"]) while (tryTime > 0): tryTime = tryTime - 1 try: print("正在寻找{0}".format(description)) gameobject = self.androidpoco(findName) if gameobject.wait(waitTime).exists(): mylog.info("查找元素-【{}】--成功".format(findName)) if self.androidpoco(ClickName).exists(): print("发现{0}点击元素,并点击".format(description)) mylog.info("查找点击-【{}】--元素成功".format(description)) self.androidpoco(ClickName).click() mylog.info("点击元素-【{}】--成功".format(description)) sleep(sleeptime) return True else: print("查找{0}失败".format(description)) mylog.error("查找点击元素-【{}】--失败".format(findName)) else: mylog.error("查找元素-【{}】--失败".format(findName)) except Exception as e: mylog.error("查找【{0}】出现未知错误,{1}".format(description, e)) return False log(PocoNoSuchNodeException("点击-【{}】-元素失败".format(description)), desc="点击元素失败", snapshot=True) raise PocoNoSuchNodeException("点击-【{}】-元素失败".format(description))