怎么使用python游戏测试工具自动化遍历游戏中所有关卡

发布时间:2022-06-21 13:47:00 作者:iii
来源:亿速云 阅读:267

这篇文章主要介绍了怎么使用python游戏测试工具自动化遍历游戏中所有关卡的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用python游戏测试工具自动化遍历游戏中所有关卡文章都会有所收获,下面我们一起来看看吧。

场景

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

思路

游戏的战斗是有时间限制的,到了 5 分钟打不过就会判负,胜负都会出现结算面板,用 GA 提供的 find_element_wait 函数查找这个结算面板,从进入战斗到找到了这个面板 element 就是通关时间,如果超过 5 分钟了就说明被卡住了,最后输出一张” 关卡通关时间表 “排序看看哪些关卡通关时间>5 分钟即为有问题的关卡。

实现细节

1.卡住的判定和处理

GAutomator find_element_wait 函数的说明

怎么使用python游戏测试工具自动化遍历游戏中所有关卡

代码中设置 5 秒查一次结算面板,超过 60 次其实已经卡住了 。Page.panel_BattleRes 是结算面板,这里采用 PO 模式把所有的 element 都写入到一个 Page 中。

怎么使用python游戏测试工具自动化遍历游戏中所有关卡

如果卡住 了游戏会一直停在哪里,卡住后利用 adb 指令重启游戏并继续测试下一个关卡一直到遍历整个关卡列表。

怎么使用python游戏测试工具自动化遍历游戏中所有关卡

2.GAutomator 调用游戏内部的 GM 指令

GAutomator 可以把游戏里的 C# 函数注册过来然后在 python 中调用,这是 GA 说明文档相关部分:

怎么使用python游戏测试工具自动化遍历游戏中所有关卡

所以把游戏中的 GM 指令方法注册过去再在脚本里调用,这样我才能用指令进入各关卡而不会受到等级、入口、前置关卡等限制

unity 中:

怎么使用python游戏测试工具自动化遍历游戏中所有关卡

python 中:

怎么使用python游戏测试工具自动化遍历游戏中所有关卡

3.最终输出的报告

第一列是关卡 id,第二列是通关时间,100014 这个关卡是有问题的,因为已经超过 5 分钟了

怎么使用python游戏测试工具自动化遍历游戏中所有关卡

详细代码

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游戏测试工具自动化遍历游戏中所有关卡”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 简单的没有关卡的关灯游戏
  2. python如何制作游戏

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:python怎么利用scatter绘画散点图

下一篇:基于Pytorch如何实现的声音分类

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》