怎么使用python游戏测试工具自动化遍历游戏中所有关卡
导读:本文共3033字符,通常情况下阅读需要10分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 场景游戏里有很多关卡(可能有几百个了),理论上每次发布到外网前都要遍历各关卡看看会不会有异常,上次就有玩家在打某个关卡时卡住不动了,如果每个关卡要人工遍历这样做会非常的耗时,所以考虑用自动化的方式来实现。思路游戏的战斗是有时间限制的,到了 5 分钟打不过就会判负,胜负都会出现结算面板,用 GA 提供的 find_element_wait 函数查找这个结算面板,从... ...
目录
(为您整理了一些要点),点击可以直达。场景
游戏里有很多关卡(可能有几百个了),理论上每次发布到外网前都要遍历各关卡看看会不会有异常,上次就有玩家在打某个关卡时卡住不动了,如果每个关卡要人工遍历这样做会非常的耗时,所以考虑用自动化的方式来实现。
思路
游戏的战斗是有时间限制的,到了 5 分钟打不过就会判负,胜负都会出现结算面板,用 GA 提供的 find_element_wait 函数查找这个结算面板,从进入战斗到找到了这个面板 element 就是通关时间,如果超过 5 分钟了就说明被卡住了,最后输出一张” 关卡通关时间表 “排序看看哪些关卡通关时间>5 分钟即为有问题的关卡。
实现细节
1.卡住的判定和处理
GAutomator find_element_wait 函数的说明
代码中设置 5 秒查一次结算面板,超过 60 次其实已经卡住了 。Page.panel_BattleRes 是结算面板,这里采用 PO 模式把所有的 element 都写入到一个 Page 中。
如果卡住 了游戏会一直停在哪里,卡住后利用 adb 指令重启游戏并继续测试下一个关卡一直到遍历整个关卡列表。
2.GAutomator 调用游戏内部的 GM 指令
GAutomator 可以把游戏里的 C# 函数注册过来然后在 python 中调用,这是 GA 说明文档相关部分:
所以把游戏中的 GM 指令方法注册过去再在脚本里调用,这样我才能用指令进入各关卡而不会受到等级、入口、前置关卡等限制
unity 中:
python 中:
3.最终输出的报告
第一列是关卡 id,第二列是通关时间,100014 这个关卡是有问题的,因为已经超过 5 分钟了
详细代码
AutoBattleTest.py 用来实现核心逻辑
fromtestcase.toolsimport*fromtestcase.ExcelToolimportExcelReader,ExcelWriterfromtestcase.PageimportPageclassAutoBattle:def__init__(self,level_excel_path):self.start_time=0self.levelList=ExcelReader(level_excel_path).read_first_sheet_first_col()self.excelWriter=ExcelWriter()print(self.levelList)defstart_battle(self):self.start_time=time.time()m_btnStartGame=engine.find_element(Page.btn_BeginFight)screen_shot_click(m_btnStartGame)#autofightm_autoFight=engine.find_element(Page.btn_AutoFight)screen_shot_click(m_autoFight)deftest_each_level(self):forindex,levelinenumerate(self.levelList):print("关卡id---->"+level)engine.call_registered_handler("GoTo","n"+level)#这里调用unity里的GM指令self.start_battle()battleResult=find_element_wait(Page.panel_BattleRes,65,5)if(battleResult):screen_shot_click(battleResult)duration=str(int(time.time()-self.start_time))#关卡持续时间self.excelWriter.write_excel(index,0,level)#第一列写关卡名self.excelWriter.write_excel(index,1,duration)#第二列写通关时间else:duration=str(int(time.time()-self.start_time))#关卡持续时间self.excelWriter.write_excel(index,0,level)#第一列写关卡名self.excelWriter.write_excel(index,1,duration)#第二列写通关时间self.restart_game()self.default_login()find_element_wait(Page.btn_Battle)self.excelWriter.save_excel()defrestart_game(self):#重启游戏os.system("adbshellamforce-stop%s"%Page.package_name)time.sleep(2)os.system("adbshellamstart-n%s/.NativeUnityPlayerActivity"%Page.package_name)defdefault_login(self):#登录一次后续直接点击就可以登录#m_BtnSart2start_btn=find_element_wait(Page.btn_LogIn)screen_shot_click(start_btn)if__name__=="__main__":ab=AutoBattle("level.xls")ab.test_each_level()
ExcelTool.py 用来读写表格
importxlrdimporttimeimportxlwtclassExcelReader:def__init__(self,excel_path):self.excel_path=excel_path;defread_first_sheet_first_col(self):data=xlrd.open_workbook(self.excel_path)st=data.sheet_by_index(0)col=[str(st.cell_value(i,0)).replace(".0","")foriinrange(0,st.nrows)]returncolclassExcelWriter:def__init__(self):self.wb=xlwt.Workbook()self.sh=self.wb.add_sheet("关卡通过时间记录")self.cur_col=0defwrite_excel(self,row,col,record_str):self.sh.write(row,col,record_str)defsave_excel(self):date_string=time.strftime("%Y%m%d%H%M")excel_name="TestResult"+date_string+".xls"self.wb.save(excel_name)if__name__=="__main__":ew=ExcelWriter()ew.save_excel()
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
怎么使用python游戏测试工具自动化遍历游戏中所有关卡的详细内容,希望对您有所帮助,信息来源于网络。