python游戲測試工具自動化遍歷游戲中所有關卡
場景
游戲里有很多關卡(可能有幾百個了),理論上每次發(fā)布到外網(wǎng)前都要遍歷各關卡看看會不會有異常,上次就有玩家在打某個關卡時卡住不動了,如果每個關卡要人工遍歷這樣做會非常的耗時,所以考慮用自動化的方式來實現(xiàn)。
思路
游戲的戰(zhàn)斗是有時間限制的,到了 5 分鐘打不過就會判負,勝負都會出現(xiàn)結算面板,用 GA 提供的 find_element_wait 函數(shù)查找這個結算面板,從進入戰(zhàn)斗到找到了這個面板 element 就是通關時間,如果超過 5 分鐘了就說明被卡住了,最后輸出一張” 關卡通關時間表 “排序看看哪些關卡通關時間>5 分鐘即為有問題的關卡。
實現(xiàn)細節(jié)
1.卡住的判定和處理
GAutomator find_element_wait 函數(shù)的說明
代碼中設置 5 秒查一次結算面板,超過 60 次其實已經(jīng)卡住了 。Page.panel_BattleRes 是結算面板,這里采用 PO 模式把所有的 element 都寫入到一個 Page 中。
如果卡住 了游戲會一直停在哪里,卡住后利用 adb 指令重啟游戲并繼續(xù)測試下一個關卡一直到遍歷整個關卡列表。
2.GAutomator 調(diào)用游戲內(nèi)部的 GM 指令
GAutomator 可以把游戲里的 C# 函數(shù)注冊過來然后在 python 中調(diào)用,這是 GA 說明文檔相關部分:
所以把游戲中的 GM 指令方法注冊過去再在腳本里調(diào)用,這樣我才能用指令進入各關卡而不會受到等級、入口、前置關卡等限制
unity 中:
python 中:
3.最終輸出的報告
第一列是關卡 id,第二列是通關時間,100014 這個關卡是有問題的,因為已經(jīng)超過 5 分鐘了
詳細代碼
AutoBattleTest.py 用來實現(xiàn)核心邏輯
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) #這里調(diào)用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)) # 關卡持續(xù)時間 self.excelWriter.write_excel(index, 0, level) # 第一列寫關卡名 self.excelWriter.write_excel(index, 1, duration) # 第二列寫通關時間 else: duration = str(int(time.time() - self.start_time)) # 關卡持續(xù)時間 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):#登錄一次后續(xù)直接點擊就可以登錄 #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游戲測試工具自動化遍歷游戲中所有關卡的詳細內(nèi)容,更多關于python游戲測試自動化遍歷關卡的資料請關注腳本之家其它相關文章!
相關文章
我對PyTorch dataloader里的shuffle=True的理解
這篇文章主要介紹了我對PyTorch dataloader里的shuffle=True的理解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05python使用for循環(huán)和海龜繪圖實現(xiàn)漂亮螺旋線
這篇文章主要為大家介紹了python使用for循環(huán)和海龜繪圖實現(xiàn)漂亮螺旋線實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06python之DataFrame實現(xiàn)excel合并單元格
這篇文章主要為大家詳細介紹了python之DataFrame實現(xiàn)excel合并單元格,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04