您的位置:首页 > 移动开发

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

2021-12-26 20:13 876 查看

[toc]

1、toast介绍

Android中的

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

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

再来看下

toast
长什么样,像这种弹出来的消息**"再按一次退出百度App"**,这种消息提示框就是
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: