ATX atx-uiautomator2 识别 android toast 测试

老马 · 2018年01月27日 · 最后由 老马 回复于 2018年03月09日 · 2438 次阅读

书接上回:

Appium1.7.2 android toast 消息测试
弄完 appium android toast 消息测试后,便继续鼓捣 atx-uiautomator2 利用 atx-uiautomator2 的 u2 server 来启动 app。
因为用的 u2 的 server 包方法 u2.connect('') 连接的 u2 server.然后又用的 selenium.webdriver 的 WebDriverWait 和 expected_conditions 来做的
toast 消息方法 is_toast_exist(),刚开始以为这样 server 间会有冲突,结果调整了一会儿后,居然成功看到了效果。

预置条件:

USB 连接我的小米 mix2 真机 ,开启手机开发者选项,开始 usb 调试模式和其它相关。

cmd@TR:~$ adb devices -l
List of devices attached
yournumber               device usb:3-1 product:chiron model:MIX_2 device:chiron

向手机安装了 atx 的 uiautomator2 相关手机端。(此时手机端已经有上一次测试 appium toast 消息安装的 appium 相关,并未做任何清理和卸载)

(ATX363) cmd@TR:~$ python -m uiautomator2 init
2018-01-27 15:29:30,409 - __main__.py:241 - INFO - Device(yournumber) initialing ...
2018-01-27 15:29:30,559 - __main__.py:120 - INFO - install minicap
2018-01-27 15:29:30,629 - __main__.py:127 - INFO - install minitouch
2018-01-27 15:29:31,446 - __main__.py:148 - INFO - app-uiautomator.apk(1.0.9) installing ...
2018-01-27 15:29:42,373 - __main__.py:162 - INFO - atx-agent(0.1.5) already installed, skip
2018-01-27 15:29:45,332 - __main__.py:211 - INFO - atx-agent output: server started, listening on 172.25.69.55:7912
2018-01-27 15:29:45,333 - __main__.py:212 - INFO - success

且手机端已有上次安装过的百度阅读 app

编辑脚本:

U2Toast.py


# coding=utf-8
import uiautomator2 as u2
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
import unittest
import time


packagename = 'com.baidu.yuedu'
activity = 'com.baidu.yuedu.splash.SplashActivity'
u2.DEBUG = True

class U2ToastTest(unittest.TestCase):

    def setUp(self):
        self.d = u2.connect('')
        print(self.d.device_info)
        self.d.app_start(packagename,activity)


    def is_toast_exist(driver, toastmessage, timeout=30, poll_frequency=0.5):
        """is toast exist, return True or False
        :Agrs:
         - driver - 传driver
         - toastmessage   - 页面上看到的toast消息文本内容
         - timeout - 最大超时时间,默认30s
         - poll_frequency  - 间隔查询时间,默认0.5s查询一次
        :Usage:
         is_toast_exist(driver, "toast消息的内容")
        """
        try:
            toast_loc = ("xpath", ".//*[contains(@text,'%s')]" % toastmessage)
            WebDriverWait(driver, timeout, poll_frequency).until(
                expected_conditions.presence_of_element_located(toast_loc))
            return True
        except:
            return False

#       element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT,message)))



    def test_U2toast(self):
        #暂时atx-uiautomator2中没找到类似selenium.webdriver的wait_activity方法 先暂时sleep 9s等待主页面出现
        time.sleep(9)
        #self.d.wait_activity(".base.ui.MainActivity", 10)
        # 点击返回,退出app
                #此处修改用atx-uiautomator2下的press方法 点击back退出app
        self.d.press("back")
        toastresult = self.is_toast_exist(self.d,'百度阅读:再按一次退出')
        if toastresult is True:
            print('Test Success find toast message')
        else:
            print('Test Failed not find toast message')

    def tearDown(self):
        try:
            self.d.app_stop(packagename)
        except:
            pass

if __name__ == '__main__':
    try:
        suite = unittest.TestLoader().loadTestsFromTestCase(U2ToastTest)
        unittest.TextTestRunner(verbosity=2).run(suite)
    except SystemExit:
        pass

测试通过:

事后一支烟:

1 仅从感官体验上,来看这两种的执行速度上,atx-uiautomator2 是远远快于 appium uiautomator2 的 。你用 appium uiautomator2 还要先启动 appium server 且执行过程中,可以看 server log 那是相当的繁琐,光启动个 app 就耗了相当多 server 操作和时间。而 atx-uiautomator2 是秒连,只是前提你要手机端保证 uiautomator2 的开启了即可(实际本质无区别,都需要连接个 uiautomator2 server)

2 这样就可以对 python 写的 atx-uiautomator2 进行功能 API 扩展了,利用 selenium.webdriver 的 API 来弥补了。
具体原理我还未吃透:
比如,我卸载掉手机端的 appium 相关,只留 atx-uiautomator2 相关再跑 U2Toast.py 是否会通过。
还是要,先执行过第一篇内容的 FindToastTest.py 让手机端安装过 io.appium.uiautomator2.server 和 io.appium.uiautomator2.server.test Unlock 和 Appium Setting 等,然后在执行此次的
U2Toast.py 才可以通过?

3 2 月 11 日补充:
实际手机里没安装过 io.appium.uiautomator2.server 和 io.appium.uiautomator2.server.test 这两个,也可以.
目前我的手机里只有 Unlock 和 Appium Setting 和 ATX 向手机安装的 python -m uiautomator2 init .
目前我的脚本端,即 python 环境 也只有安装过 atx 和 Appium-Python-Client 所以只要能导入 Appium-Python-Client 自带安装的 selenium.webdriver 即可.

附上 pip list

(venv) C:\Users\cmd\venv\Scripts>pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
aircv (1.4.6)
**Appium-Python-Client (0.26)**
**atx (1.2.1.dev2)**
AxmlParserPY (0.0.3)
certifi (2018.1.18)
chardet (3.0.4)
colorama (0.3.9)
decorator (4.2.1)
facebook-wda (0.2.2.dev1)
fire (0.1.2)
futures (3.0.5)
humanize (0.5.1)
idna (2.6)
imageio (2.2.0)
maproxy (0.0.12)
numpy (1.14.0)
**opencv-contrib-python (3.4.0.12)**
Pillow (5.0.0)
pip (9.0.1)
progress (1.3)
py (1.5.2)
PyYAML (3.11)
requests (2.18.4)
retry (0.9.2)
**selenium (3.9.0)**
setuptools (38.5.1)
six (1.11.0)
tornado (5.0b1)
tqdm (4.5.0)
**uiautomator2 (0.1.1.dev3)**
urllib3 (1.22)
weditor (0.0.4.dev6)
wheel (0.30.0)
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞
老马 Appium1.7.2 android toast 消息测试 中提及了此贴 01月27日 16:25

很值得参考,感谢!

想问问 这个 uiautomator2 与 appium 的这个 uiautomaotor2 server 是一样的么

我叫胖子 回复

appium 在 android 底层用的也是 uiautomator2.
uiautomator2 的原生是谷歌支持的开源 UI 自动化框架。atx 的 uiautomator2 是基于谷歌的 uiautomator2(谷歌的这个好像是 java 的)的一种 python 语言重写的 API 又扩展了一些便利特性。所以是独立分别发展的两个开源项目,一个是谷歌 java,一个是 python。

我叫胖子 回复

atx-uiautomator2 需要向手机安装个 atx-agent 这个其实就相当于启动的一个 appium server 来接收和处理脚本端的操作请求。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册