『与善仁』Appium基础 — 32、获取toast信息

文章目录

1、toast介绍

Android中的toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据所设置的显示时间自动消失。

toasappium1.6.3以上版本才支持,appium1.4的版本就别浪费时间了。

再来看下toast长什么样,如下图:

像这种弹出来的消息"再按一次退出百度App",这种消息提示框就是toast了。

『与善仁』Appium基础 — 32、获取toast信息

2、toast定位

toast定位需要添加两步操作:

  • 添加启动参数

    想要定位toast元素,Desired capabilities对象中要添加一个启动参数,如下:

    "automationName": "Uiautomator2"

    这样才能定位到toast

  • 除了使用Appium-Python-Client

    也就是导入from appium import webdriver

    还需要用到selenium中的类,导入:

    from selenium.webdriver.support.wait import WebDriverWaitfrom

    selenium.webdriver.support import expected_conditions as EC

3、示例

"""
1.学习目标
    掌握toast操作方法
        目的,获取toast中文本内容
        作用,获取toast文本,有时候用在测试用例的断言中

2.操作步骤
    2.1 找到触发toast出现的元素,并操作
    2.2 获取toast中文本内容
        借助selenium中显式等待,是WebDriverWait类
        WebDriverWait(driver,最大等待时间,轮寻时间).(EC.presence_of_element_located(locator))
    2.3 toast元索定位方法 xpath定位方法
        driver.find element-by-xpath(//Lcontains(etext,'再按一次')1")
    2.4 打印ltoast中文本的值
        元素.text

    2.5 注意事项
        (1)需要在启动参数中添加一个参数(Desired capabilities对象)
            'automationName': 'Uiautomator2'

        (2)appium server版本1.6.3以上版本才支持。

3.需求
    在文件管理器中实现toast获取
        打开文件管理器app,点击返回,获取toast的值
"""

# 1.导入appium和TouchAction
import time
from appium import webdriver
# 添加WebDriverWait 类
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
    "platformName": "Android",  # 系统名称
    "platformVersion": "7.1.2",  # 系统版本
    "deviceName": "127.0.0.1:21503",  # 设备名称
    "appPackage": "com.cyanogenmod.filemanager",  # APP包名
    "appActivity": ".activities.NavigationActivity",  # APP启动名
    "automationName": "Uiautomator2"  # 获取toast使用
}

# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
time.sleep(6)

# 4.操作APP,获取toast信息
#   打开文件管理器app,点击返回,获取toast的值
# 4.1 直接进入到文件管理器app,点击两下返回键,触发toast出现
driver.back()

# 4.2 定位(捕获)toast元素
# 定位器
locator = ("xpath", "//*[contains(@text,'再次点击')]")
# 定位
toast = WebDriverWait(driver, 10, 0.01).until(EC.presence_of_element_located(locator))

# 4.3 输出toast信息
print(toast.text)

# 5.关闭APP
time.sleep(3)
driver.quit()

4、封装toast判断

单独写一个函数来封装“判断是否存在toast消息”,存在返回True,不存在返回False。

# 1.导入appium和所需要的包
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

# 2.创建Desired capabilities对象,添加启动参数
desried_caps = {
    "platformName": "Android",
    "deviceName": "9665cc4e",
    "platformVersion": "7.1.1",
    "appPackage": "com.baidu.yuedu",
    "appActivity": "com.baidu.yuedu.splash.SplashActivity",
    "automationName": "uiautomator2",
    "noReset": True
}


def is_toast_exist(driver, text, timeout=30, poll_frequency=0.5):
    """
    is toast exist, return True or False

    :param driver: 传入driver
    :param text: 页面上看到的文本内容
    :param timeout: 大超时时间,默认30s
    :param poll_frequency: 间隔查询时间,默认0.5s查询一次
    :return: return True or False

    Usage:
        is_toast_exist(driver, "看到的内容")
    """

    try:
        message_loc = ("xpath", "//*[contains(@text,'%s')]" % text)
        WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(message_loc))
        return True
    except:
        return False


if __name__ == "__main__":
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desried_caps)
    print("打开百度阅读应用")

    # 等主页面activity出现
    # driver.wait_activity(".base.ui.MainActivity", 10)
    time.sleep(15)

    # 点击返回
    driver.back()
    print("点击返回成功")

    # 判断是否存在toast'再按一次退出'
    value = is_toast_exist(driver, "再按一次退出")

    print("是否存在toast:", value)

    # 5.关闭APP
    time.sleep(2)
    driver.quit()

上一篇:Vue3 teleport 瞬间移动


下一篇:Android学习目录