场景
游戏里有很多关卡(可能有几百个了),理论上每次发布到外网前都要遍历各关卡看看会不会有异常,上次就有玩家在打某个关卡时卡住不动了,如果每个关卡要人工遍历这样做会非常的耗时,所以考虑用自动化的方式来实现。
思路
游戏的战斗是有时间限制的,到了 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 用来实现核心逻辑
- 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 用来读写表格
- 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()
后记
这套脚本可以排查出一进入或者中途被卡住或者不结算被卡住的问题,但是如果是某个怪物的某个技能必定能导致关卡卡住,而这个怪物在放技能之前就被杀了,这种情况这套脚本有概率排查不到。
以上就是python游戏测试工具自动化遍历游戏中所有关卡的详细内容,更多关于python游戏测试自动化遍历关卡的资料请关注w3xue其它相关文章!