Airtest 关于 poco 初始化的问题

渐渐 · March 10, 2022 · Last by hakaboom replied at May 19, 2022 · 3253 hits

这两天在写游戏自动化的时候遇到 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 了

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 6 条回复 时间 点赞
1Floor has deleted

poco 得在应用启动后才有链接啊。其他文件里不要初始化 poco 不就好了

渐渐 #3 · March 10, 2022 Author
hakaboom 回复

是的,说起来也很简单,但是忽略了主入口文件最上方的 import 会执行其他的类文件里面的代码,导致首次初始化,实际是在启动 app 之前。

渐渐 #4 · May 18, 2022 Author
hakaboom 回复

另外想问下,如果不在其它文件初始化的话,页面类文件里面怎么用啊

渐渐 回复

写个函数包起来?

渐渐 #6 · May 19, 2022 Author
hakaboom 回复

我现在是写了个工具类包装起来~但是写页面代码的时候不会自动补全或者显示可用的方法,不知道是否还有其它更好的方式~~代码如下:

from poco.drivers.unity3d import UnityPoco
from poco.drivers.android.uiautomation import AndroidUiautomationPoco

__poco = False
__androidPoco = False


def __initPoco():
    global __poco
    __poco = UnityPoco()


def __initAndroidPoco():
    global __androidPoco
    __androidPoco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)


def getPoco():
    if not __poco:
        __initPoco()
    return __poco


def getAndroidPoco():
    if not __androidPoco:
        __initAndroidPoco()
    return __androidPoco

渐渐 回复

代码提示/补全可以写 pyi 文件或者typehint.其他方法的话,我也不太清楚了,没用过 poco

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up