本文原文链接:未必平凡博客 https://vv2014.com/393.html 转载请注明出处[vv2014.com]

toast提示是app中最常见的,UI自动化中自然也要对其进行测试,故测试完成以此贴记录下

一 环境配置

如果环境配置OK直接跳到第6步安装uiautomator2

1.下载Appium最新版

地址:https://github.com/appium/appium-desktop/releases

2.下载Python3

地址:https://www.python.org/getit/

3.下载Pycharm

地址:https://blog.csdn.net/pdcfighting/article/details/80297499

4.下载Android SDK

 - -!没有地址,自己去下

5.看下UIAutomator2文档

地址:https://github.com/openatx/uiautomator2

6.安装uiautomator2

-  安装NPM镜像,地址:https://npm.taobao.org/

-  执行命令:npm install -g cnpm --registry=https://registry.npm.taobao.org

-  安装uiautomator2的配置文件执行命令:cnpm install appium-uiautomator2-driver

二 获取Toast方法

1.引入Uiautomator2

class DriverConfig(object):

    def get_driver(self):
        try:
            self.desired_caps = {}
            self.desired_caps['platformName'] = 'Android'  # 平台
            self.desired_caps['platformVersion'] = '5.0.2'  # 系统版本
            self.desired_caps['app'] = 'resource\\safebox.apk'  # 指向.apk文件,如果设置appPackage和appActivity,那么这项会被忽略
            self.desired_caps['appPackage'] = 'com.notordinary'
            self.desired_caps['appActivity'] = 'com.com.notordinary.ui.activity.SplashActivity'
            self.desired_caps['unicodeKeyboard'] = 'true'   # 是否支持unicode的键盘。如果需要输入中文,要设置为“true”
            self.desired_caps['resetKeyboard'] = 'true'  # 是否在测试结束后将键盘重置为系统默认的输入法。
            self.desired_caps['newCommandTimeout'] = '120'  # Appium服务器待appium客户端发送新消息的时间。默认为60秒
            self.desired_caps['deviceName'] = 'c8c8d64a'  # 手机ID
            self.desired_caps['noReset'] = True   # true:不重新安装APP,false:重新安装app
            self.desired_caps['automationName'] = 'UiAutomator2'
            driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", self.desired_caps)
            return driver
        except Exception as e:
            raise e

2.导入模块

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

3.toast定位方法封装

def get_toast_text(self, text, timeout=5, poll_frequency=0.01):
        """
        ########################################
        描述:获取Toast的文本信息
        参数:text需要检查的提示信息  time检查总时间  poll_frequency检查时间间隔
        返回值:返回与之匹配到的toast信息
        异常描述:none
        ########################################
        """
        toast_element = (By.XPATH, "//*[contains(@text, " + "'" + text + "'" + ")]")
        toast = WebDriverWait(self.driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_element))
        return toast.text

注意上述代码段:" + "'" + text + "'" + "

因为当前方法编辑器识别为://*[contains(@text, 'toast测试')],我们需要让传入的参数带引号才能被识别,所以才有上述不好看的一段代码

4.使用已封装的toast方法

self.driver.get_toast_text('toast测试')

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐