这两天在写游戏自动化的时候遇到 poco 初始化的问题,搞了几天才找到问题所在。
报错信息如下

# 主入口main.py
from airtest.core.api import *
from pages.loginPage import loginPage
from pages.mainPage import *
from pages.setPage import setPage
from pocoMgr import *
from testcase.test_rename import test_rename
auto_setup(__file__, logdir=True, devices=["android://127.0.0.1:5037/959775083200472"])
# 设备号VIVOS7: android://127.0.0.1:5037/959775083200472
start_app("xxxxxxxxxx")
sleep(20)
if get_poco()("spCompeting").exists():
    mainPage.autoCloseFrame()
# mainPage.py 类文件
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from poco.drivers.std import StdPoco
poco = StdPoco()
class MainPage:
    def __init__(self):
        self.loginreward = "text='登陆有礼'"
        self.mainpageLv = "textnumLevel"
        self.setPageBtn = "spbtnMore"
        # 弹窗关闭按钮
        self.spbtnClose = "spbtnClose"
        self.imgbtnOK = "imgbtnOK"
        # self.imgbtnClose = "imgbtnClose"
        self.imgbtnClose = "textSure"
        # 竞技匹配按钮就
        self.spCompeting = "spCompeting"
    def autoCloseFrame(self):
        notallspringframeisclose = True
        while notallspringframeisclose:
            notallspringframeisclose = False
            if get_poco()(self.imgbtnClose).exists():
                get_poco()(self.imgbtnClose).click()
                notallspringframeisclose = True
                continue
            if get_poco()(self.spbtnClose).exists():
                get_poco()(self.spbtnClose).click()
                notallspringframeisclose = True
                continue
            if get_poco()(self.imgbtnOK).exists():
                get_poco()(self.imgbtnOK).click()
                notallspringframeisclose = True
                continue
mainPage = MainPage()
原因:主入口 main 文件里面最上方是有 import 其它类的,而这些类文件最上方又有 poco 初始化代码,导致初始化代码跑在了 startapp 之前。
解决办法:单独写一个 pocoMgr.py,包含初始化 poco 和 getpoco 两个方法,把初始化和 get 分开来,其他类大部分只写 get 方法,这样就不会在 import 其他类的时候,又初始化 poco 了