场景

游戏里有很多关卡(可能有几百个了),理论上每次发布到外网前都要遍历各关卡看看会不会有异常,上次就有玩家在打某个关卡时卡住不动了,如果每个关卡要人工遍历这样做会非常的耗时,所以考虑用自动化的方式来实现。

思路

游戏的战斗是有时间限制的,到了 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 用来实现核心逻辑

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 from testcase.tools import * from testcase.ExcelTool import ExcelReader,ExcelWriter from testcase.Page import Page class AutoBattle:     def __init__(self,level_excel_path):         self.start_time=0         self.levelList=ExcelReader(level_excel_path).read_first_sheet_first_col()         self.excelWriter =  ExcelWriter()         print(self.levelList)     def start_battle(self):         self.start_time=time.time()         m_btnStartGame = engine.find_element(Page.btn_BeginFight)         screen_shot_click(m_btnStartGame)         #auto fight         m_autoFight = engine.find_element(Page.btn_AutoFight)         screen_shot_click(m_autoFight)     def test_each_level(self):         for index,level in enumerate(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()     def restart_game(self): #重启游戏         os.system("adb shell am force-stop  %s"%Page.package_name)         time.sleep(2)         os.system("adb shell am start -n %s/.NativeUnityPlayerActivity"%Page.package_name)     def default_login(self):#登录一次后续直接点击就可以登录         #m_BtnSart2         start_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 用来读写表格

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import xlrd import time import xlwt class ExcelReader:     def __init__(self,excel_path):         self.excel_path = excel_path;     def read_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","") for i in range(0, st.nrows)]         return col class ExcelWriter:     def __init__(self):         self.wb = xlwt.Workbook()         self.sh = self.wb.add_sheet("关卡通过时间记录")         self.cur_col  =0     def write_excel(self,row ,col,record_str):         self.sh.write(row, col, record_str)     def save_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()

后记

这套脚本可以排查出一进入或者中途被卡住或者不结算被卡住的问题,但是如果是某个怪物的某个技能必定能导致关卡卡住,而这个怪物在放技能之前就被杀了,这种情况这套脚本有概率排查不到。

​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。

如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受

可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛

分享他们的经验,还会分享很多直播讲座和技术沙龙

可以免费学习!划重点!开源的!!!

qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: