Appium自动化测试中获取Toast信息操作
作者:测试之路king 时间:2022-05-12 07:10:48
Toast简介
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。
Toast 定位
Appium 1.6.3开始支持识别Toast内容,主要是基于UiAutomator2,因此需要在Capablity配置参数
启动参数配置
desired_caps['automationName']='uiautomator2'
环境
Appium-Python-Client: 2.1.2
selenium: 4.1.0
Appium:v1.20.2
测试应用
网易云课堂
测试设备
夜神模拟器 Android 7.1.2
测试场景
进入登录界面输入用户名和错误的密码,获取Toast内容
代码实现
# _*_ coding:utf-8 _*_
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
desired_caps = {
"platformName": "Android",
"platformVersion": "7.1.2",
"udid": "127.0.0.1:62001",
"appPackage": "com.netease.edu.study",
"appActivity": "com.netease.edu.study.activity.ActivityWelcome",
"noReset": True,
'automationName': 'uiautomator2'
}
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
driver.implicitly_wait(30)
# 点击我的菜单
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tab_account").click()
# 点击登录注册按钮
driver.find_element(AppiumBy.XPATH, "//*[@text='登录/注册']").click()
# 点击手机号码登录
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/login_phone_login").click()
# 输入手机号码
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_num").send_keys("132****475")
# 输入错误密码
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/tv_phone_pwd").send_keys("wy12345")
# 点击登录按钮
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/button").click()
# 获取toast提示
toast_text = driver.find_element(AppiumBy.XPATH, "//*[@class=\"android.widget.Toast\"]").text
print(toast_text)
执行结果:
说明
toast 获取主要使用一个通用的class属性获取,通过xpath的方式://*[@class="android.widget.Toast"]
toast信息存在是否存在判断封装
代码
def is_toast_exist(driver,text,timeout=20,poll_frequency=0.5):
'''is toast exist, return True or False
:Agrs:
- driver - 传driver
- text - 页面上看到的文本内容
- timeout - 最大超时时间,默认20s
- poll_frequency - 间隔查询时间,默认0.5s查询一次
:Usage:
is_toast_exist(driver, "看到的内容")
'''
try:
toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)
WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
return True
except:
return False
toast信息内容获取
代码
def is_toast_exist(driver,timeout=20,poll_frequency=0.5):
'''is toast exist, return toast_text or None
:Agrs:
- driver - 传driver
- timeout - 最大超时时间,默认20s
- poll_frequency - 间隔查询时间,默认0.5s查询一次
:Usage:
is_toast_exist(driver)
'''
try:
toast_loc = ("xpath", "//*[@class=\"android.widget.Toast\"]")
WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
toast_text = driver.find_element(AppiumBy.XPATH, "//*[@class=\"android.widget.Toast\"]").text
return toast_text
except:
return None
来源:https://ceshizhilu.blog.csdn.net/article/details/122693435
标签:Appium,获取,Toast
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3中的re.findall()方法及re.compile()
2023-04-12 11:54:47
Django框架中方法的访问和查找
2022-07-23 16:31:35
asp如何让用户也能修改密码?
2010-05-13 16:41:00
Python工程师面试必备25条知识点
2023-10-31 00:30:53
superLink,让伪链接更有可用性
2009-06-02 12:35:00
网友分享:Oracle数据库开发技术经验浅谈
2009-04-22 13:11:00
JSON.stringify转换JSON时日期时间不准确的解决方法
2014-07-20 13:25:07
![](https://img.aspxhome.com/file/2023/7/55917_0s.jpg)
python 读取、写入txt文件的示例
2023-09-29 22:04:18
![](https://img.aspxhome.com/file/2023/7/63237_0s.png)
PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
2023-10-02 13:10:01
不拘小节的中文字体设计
2009-05-21 10:44:00
整理及优化CSS代码的七个原则[译]
2009-04-23 12:35:00
![](https://img.aspxhome.com/file/UploadPic/20094/23/01-21s.gif)
keras 模型参数,模型保存,中间结果输出操作
2023-06-05 09:52:33
![](https://img.aspxhome.com/file/2023/1/84921_0s.jpg)
跟老齐学Python之list和str比较
2021-02-26 22:36:36
HTML文件HEAD内部标签用法浅析
2008-07-06 20:56:00
Python中xlsx文件转置操作详解(行转列和列转行)
2022-02-18 03:36:34
![](https://img.aspxhome.com/file/2023/0/92310_0s.jpg)
Python的Bottle框架中返回静态文件和JSON对象的方法
2023-11-07 20:14:41
![](https://img.aspxhome.com/file/2023/4/66374_0s.png)
推荐:怎么用javascript进行拖拽
2007-09-21 20:14:00
![](https://img.aspxhome.com/file/UploadPic/up/2007092121195610.jpg)
使用PHP批量生成随机用户名
2023-07-22 13:10:10
python多维列表总是只转为一维数组问题解决
2022-12-27 00:25:21
![](https://img.aspxhome.com/file/2023/0/76920_0s.png)
用ASP实现在线压缩与解压缩
2007-09-29 12:13:00