appium之toast处理
注意
toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量。
toast定位
1.先看下toast长什么样,如下图,像这种弹出来的消息"再按一次退出",这种就是toast了。

2.想定位toast元素,这里一定要注意automationName的参数必须是Uiautomator2才能定位到。
'automationName': 'Uiautomator2'
# coding:utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
desired_caps = {
'platformName': 'Android',
'deviceName': '127.0.0.1:62001',
'platformVersion': '4.4.2',
'appPackage': 'com.baidu.yuedu',
'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
'noReset': 'true',
'automationName': 'Uiautomator2'
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 等主页面activity出现
driver.wait_activity(".base.ui.MainActivity", 10)
driver.back() # 点返回
# 定位toast元素
toast_loc = ("xpath", ".//*[contains(@text,'再按一次退出')]")
t = WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located(toast_loc)) #注意等待必须是元素存在,元素可见会报错
print t

3.打印出来的结果,出现如下信息,说明定位到toast了
<appium.webdriver.webelement.WebElement (session="02813cce-9aaf-4754-a532-07ef7aebeb88", element="339f72c4-d2e0-4d98-8db0-69be741a3d1b")>
封装toast判断
1.单独写一个函数来封装判断是否存在toast消息,存在返回True,不存在返回False
def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5):
'''is toast exist, return True or False
:Agrs:
- driver - 传driver
- text - 页面上看到的文本内容
- timeout - 最大超时时间,默认30s
- 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
参考代码
# coding:utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_caps = {
'platformName': 'Android',
'deviceName': '127.0.0.1:62001',
'platformVersion': '4.4.2',
'appPackage': 'com.baidu.yuedu',
'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
'noReset': 'true',
'automationName': 'Uiautomator2'
}
def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5):
'''is toast exist, return True or False
:Agrs:
- driver - 传driver
- text - 页面上看到的文本内容
- timeout - 最大超时时间,默认30s
- 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
if __name__ == "__main__":
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 等主页面activity出现
driver.wait_activity(".base.ui.MainActivity", 10)
driver.back() # 点返回
# 判断是否存在toast-'再按一次退出'
print is_toast_exist(driver, "再按一次退出")
appium之toast处理的更多相关文章
- appium获取toast和操作webview实例
从testerhome上看到很多测试同学分享了toast的获取方式,其中大部分是java版本的,但也有个python版本的:Appium1.7.2 android toast 消息测试 Appium获 ...
- Appium Android Toast控件
Android Toast控件是Android系统级别的控件,不是App的控件,getPageSource是⽆法找到的. Toast介绍 1.背景 在安卓设备里面,使用各种手机应用程序的时候,需要先进 ...
- appium 3-31626 toast识别
1.toast弹窗,普通方式不能获取 例如使用getPageSource是无法找到toast的信息,uiautomatorViewer加载页面时间较长,也很难采集到toast信息 2.通过curl命令 ...
- appium获取toast方法
配置toast请注意: 1.指定desired_caps["automationName"] = "UiAutomator2" 2.要求安装jdk1.8 64位 ...
- Appium之Toast元素识别
问题思考 在日常使用App过程中,经常会看到App界面有一些弹窗提示(如下图所示)这些提示元素出现后等待3秒左右就会自动消失,那么我们该如何获取这些元素文字内容呢? Toast简介 Android中的 ...
- Appium获取toast消息
Android获取toast,需要在参数里设置automationName:Uiautomator2 设置设备的信息 desired_caps = { 'platformName': 'Android ...
- Appium获取toast消息遇到的问题(一)
一.运行错误 Android获取toast,需要在参数里设置automationName:Uiautomator2 1 # 设置设备的信息 2 desired_caps = { 3 'platform ...
- Appium获取toast消息(二)
刚接触appium进行移动端设备的UI自动化,在遇到toast消息的时候很是苦恼了一阵,最后通过强大的搜索引擎找到了个相对解决方法,废话不多说,直接贴代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ...
- Python+Appium 获取 toast 文本值方法的封装
获取toast内容方法封装如下: def get_Toast(self,message): #查找toast值 ''' method explain:查找toast的值,与find_Toast实现方法 ...
随机推荐
- Oracle 修改密码(忘记登录密码,用户System)
1.修改计算机环境变量,把oracle服务端路径放在最前面 2.输入cmd 3.输入命令:sysplus /nolog SQL>conn sys/syspwd as sysdba SQL> ...
- 完成FileUpload的文件上传功能,且可改按钮样式
FileUpload控件: 更改按钮样式思路: 自己定义一个按钮,设置该按钮的样式,然后将FileUpload控件通过定位定在自己定义的按钮上面,设置z-index,使得控件浮在自己定义的按钮上面,记 ...
- Spring Cloud Config git版
由于在学习这块内容的时候还不会使用gitHub所以就用了osc的码云 config server POM文件 <dependency> <groupId>org.springf ...
- phpmyadmin消除无法保存最近表的提示
运行 sudo dpkg-reconfigure phpmyadmin 重新配置phpmyadmin ip选择127.0.0.1,端口3306,"MySQL username for php ...
- IBatis.net特性展示代码
最近公司计划设计新业务平台架构.数据访问层框架要使用ibatis.net.头让我做些例子给其他同事演示下 ibatis的基本特性.然后评估下看是否使用.本来以后上官方下载NPetshop演示下就行了那 ...
- CSS子元素设置margin-top作用于父容器?
CSS子元素设置margin-top作用于父容器? 原因: In this specification, the expression collapsing margins means that ad ...
- Error CS0579 Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute
今天在引入ClassLibraryQikuo的时候突然报错 Error CS0579 Duplicate 'System.Reflection.AssemblyTitleAttribute' attr ...
- 【实用】Html5实现文件异步上传
1 简介 开发文件上传功能从来不是一件愉快的事,异步上传更是如此,使用过iframe和Flash的上传方案,也都感觉十分的别扭.本文简要简绍利用Html5的FormData实现文件的异步上传,还可以实 ...
- Slacklining 2017/2/7
原文 Proline Slacklining's expansion is still in process,but it already has a professional scene.Some ...
- 删除.cpp文件
今天启动vc6.0后随手直接建了一个.cpp文件(没有建什么工程的),编译运行成功后,就把vc关了.后想把这个随手建的文件给删掉,却怎么也找不到这个文件,文件搜索或改变文件的属性也无法找到这个文件,即 ...