『与善仁』Appium基础 — 29、获取toast信息
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()
『与善仁』Appium基础 — 29、获取toast信息的更多相关文章
- 『与善仁』Appium基础 — 8、Appium自动化测试框架介绍
目录 1.主流的移动端自动化测试框架 (1)Robotium (2)Macaca (3)Appium 2.自动化测试工具的选择 3.Appium简介 提示:我们前面说的Android环境搭建和adb命 ...
- 『与善仁』Appium基础 — 15、使用Appium的第一个Demo
我们使用Python语言作为测试脚本的编写语言. 执行脚本前提: Android模拟器或者手机是开机状态. 使用确保电脑和Android设备进行了链接. 也就是使用ADB命令adb connect链接 ...
- 『与善仁』Appium基础 — 3、ADB命令介绍
目录 1.ADB命令简介 2.ADB命令运行原理 3.通过ADB命令连接安卓模拟器 (1)安装安卓模拟器 (2)ADB命令连接安卓模拟器 (3)常用Android模拟器端口号 1.ADB命令简介 AD ...
- 『与善仁』Appium基础 — 4、常用ADB命令(一)
目录 1.启动和关闭ADB服务 2.查看ADB版本 3.指定adb server的网络端口 4.查询已连接设备/模拟器 5.获取安卓系统版本 6.为命令指定目标设备 7.发送文件到手机 8.从手机拉取 ...
- 『与善仁』Appium基础 — 9、补充:C/S架构和B/S架构说明
目录 1.C/S架构和B/S架构概念 2.C/S结构与B/S架构的区别 3.C/S架构和B/S架构优点和缺点 (1)B/S模式的优点和缺点: (2)C/S模式的优点和缺点: 1.C/S架构和B/S架构 ...
- 『与善仁』Appium基础 — 10、Appium基本原理
目录 1.Appium自动化测试架构 2.Appium架构图 3.Session说明 4.Desired Capabilities说明 5.Appium Server说明 6.Appium Clien ...
- 『与善仁』Appium基础 — 12、Appium的安装详解
目录 (一)Appium server安装 方式一:(桌面方式:推荐) 1.Appium Desktop下载 2.Appium Desktop安装 3.Appium Desktop使用 方式二:(No ...
- 『与善仁』Appium基础 — 14、Appium测试环境搭建
目录 1.Appium测试环境搭建整体思路 (1)Android测试环境搭建 (2)Appium测试环境搭建 (3)测试脚本语言的环境搭建 2.Appium在Android端和IOS端的工作流程 (1 ...
- 『与善仁』Appium基础 — 16、APPium基础操作API
目录 1.前置代码 2.安装和卸载APP 3.判断APP是否已安装 4.关闭APP软件和关闭驱动对象 5.发送文件到手机和获取手机中的文件 6.获取当前屏幕内元素结构(重点) 7.脚本内启动其他APP ...
随机推荐
- CentOS6配置邮件发送
CentOS6配置邮件发送 注意:要启用邮箱的服务端授权代理功能,并从中获取授权码 \cp /etc/mail.rc{,.bak} # 备份配置文件 cat >>/etc/mail.rc& ...
- kubernetes部署 etcd 集群
本文档介绍部署一个三节点高可用 etcd 集群的步骤: etcd 集群各节点的名称和 IP 如下: kube-node0:192.168.111.10kube-node1:192.168.111.11 ...
- An internal error occurred during: “Updating Maven Project”. Unsupported IClasspathEntry kind=4解决办法
An internal error occurred during: "Updating Maven Project". Unsupported IClasspathEntry k ...
- OC-引用计数器,内存管理,野指针
总结 全局断点 →-->+-->Add Exception Breakpoint 开启僵尸监听 打开Edit scheme -->Diagnostics-->Enable Zo ...
- delete() and free() in C++
In C++, delete operator should only be used either for the pointers pointing to the memory allocated ...
- 【spring AOP】@Pointcut的12种用法
@Pointcut用来标注在方法上来定义切入点. 使用格式:@ 注解(value="表达标签 (表达式格式)").如:@Pointcut("execution(* com ...
- 【力扣】973. 最接近原点的 K 个点
我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. (这里,平面上两点之间的距离是欧几里德距离.) 你可以按任何顺序返回答案.除了点坐标的顺序之外, ...
- IIS 发布 WebService 连接DB2数据库报错如下图
环境描述: 系统环境: Windows Server 2012 R2 IIS版本:IIS 6.2 C#环境:.NET Framework 4 DB2版本:9.7.500.702 ...
- Jenkins性能测试
目录 一.简介 二.JMeter测试 一.简介 Taurus是-个开源的自动化框架,用于运行各种开源负载测试工具和功能测试工具.其支持最流行的开源负载测试工具Apache JMeter.Seleniu ...
- 如何推翻JAVA的统治地位
"java越来越过份了."php狠狠的说,他转头看着C:"C哥,您可是前辈,java最近砸了我不少场子,你老再不出来管管,我怕他眼里就没有您了啊." C哥吸烟, ...