Appium+python自动化29-toast消息【转载】
本篇转自博客:上海-悠悠
前言
appium1.5以后的版本才支持toast定位,并且 'automationName'得设置为'Uiautomator2',才能捕获到。
一、 Supported Platforms
1.查看appium v1.7版本[官方文档](https://github.com/appium/appium/)
**Supported Platforms**
Appium supports app automation across a variety of platforms, like iOS, Android, and Windows. Each platform is supported by one or more "drivers", which know how to automate that particular platform. Choose a driver below for specific information about how that driver works and how to set it up:
- iOS
- The [XCUITest Driver]
- (DEPRECATED) The [UIAutomation Driver]
- Android
- (BETA) The [Espresso Driver]
- The [UiAutomator2 Driver]
- (DEPRECATED) The [UiAutomator Driver]
- (DEPRECATED) The [Selendroid Driver]
- The [Windows Driver](for Windows Desktop apps)
- The [Mac Driver] (for Mac Desktop apps)
2.从上面的信息可以看出目前1.7的android版可以支持:Espresso、UiAutomator2、UiAutomator、Selendroid四种驱动模式,后面两个不推荐用了,太老了,Espresso这个是最新支持的处于beta阶段,UiAutomator2是目前最稳的。
3.appium最新版本还能支持windows和mac的桌面app程序了,这个是否稳定,拭目以待!
二、 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"></appium.webdriver.webelement.webelement>
三、 封装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+python自动化29-toast消息【转载】的更多相关文章
- Appium+python自动化获取toast消息的方法
转载地址:https://www.cnblogs.com/shangren/p/8191879.html 1. 首先执行这个命令:npm install -g cnpm --registry=http ...
- Appium+python自动化获取toast消息(windows版)的方法
原来用的Appium1.5.3GUI版本,那为什么升级呢? 为了兼容最新版本的iOS10和Android7 Xcode8升级后,将不支持使用UIAutomation,而是改为使用XCUITest了,并 ...
- Appium+python自动化22-Appium Desktop【转载】
Appium Desktop 原滋原味的官方文档 Appium Desktop是一款用于Mac.Windows和Linux的开源应用,它提供了Appium自动化服务器在一个漂亮灵活的UI中的强大功能. ...
- Appium+python自动化6-Remote远程控制【转载】
前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...
- Appium+python自动化28-name定位【转载】
本篇转自博客:上海-悠悠 前言 appium1.5以下老的版本是可以通过name定位的,新版本从1.5以后都不支持name定位了 一. name定位报错 1.最新版appium V1.7用name定位 ...
- Appium+python自动化5-Appium Inspector【转载】
前言 appium Inspector从入门到放弃!反正你都打开了,那就看下为什么要放弃吧! Appium Inspector是appium自带的一个元素定位工具,上一篇介绍了如何使用uiaut ...
- Appium+python自动化10-AVD 模拟器【转载】
前言 有些小伙伴没android手机,这时候可以在电脑上开个模拟器玩玩 一.模拟器配置 1.双击启动AVD Manager,进入配置界面
- Appium+python自动化9-SDK Manager【转载】
前言 SDK Manager到有哪些东西是必须安装的呢? 一.SDK Manager 1.双击打开SDK Manager界面
- Appium+python自动化20-查看iOS上app元素属性【转载】
前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素Mac版的appium1.6的版 ...
- Appium+python自动化19-iOS模拟器(iOS Simulator)安装自家APP【转载】
前言 做过iOS上app测试的小伙伴应该都知道,普通用户安装app都是从appstore下载安装,安装测试版本的app,一般就是开发给的二维码扫码安装, 或者开发给个.ipa的安装包文件,通过itoo ...
随机推荐
- 接触到的一些数据结构: LIST_ENTRY, TAILQ
双链表: LIST_ENTRY: typedef struct _LIST_ENTRY { struct _LIST_ENTRY *Flink; follow: next entry, header ...
- 【其他】Windows 系统安装IIS 打开页面出现空白解决方案
部署IIS过程中遇到了一个奇怪的问题,就是怎么设置打开的页面都是一篇空白,IIS也没有任何报错,翻遍互联网好不容易找到了解决方法,今天就教给大家,希望大家不要走弯路.此方法Windows xp.7.8 ...
- 大数据分析中Redis应用
大数据分析中Redis 大数据时代,海量数据分析就像吃饭一样,成为了我们每天的工作.为了更好的为公司提供运营决策,各种抖机灵甚至异想天开的想法都会紧跟着接踵而来!业务多变,决定了必须每天修改系统,重新 ...
- BZOJ5340 [Ctsc2018]假面 【概率dp】
题目链接 BZOJ5340 题解 我们能很容易维护每个人当前各种血量的概率 设\(p[u][i]\)表示\(u\)号人血量为\(i\)的概率 每次攻击的时候,讨论一下击中不击中即可转移 是\(O(Qm ...
- 解决es6中webstrom不支持import的一个简单方法
代码如下: export_one.js的代码如下: export function one() { console.log('one'); } export function two() { cons ...
- js 读写文件
读写文件: var f = fso.CreateTextFile("c:\\pexam\\"+name+".txt", true); f.write(arr); ...
- [洛谷P3942] 将军令
洛谷题目链接:将军令 题目背景 历史/落在/赢家/之手 至少/我们/拥有/传说 谁说/败者/无法/不朽 拳头/只能/让人/低头 念头/却能/让人/抬头 抬头/去看/去爱/去追 你心中的梦 题目描述 又 ...
- OOP第三次上机
上机问题 T1 CSet 还是熟悉的CSet,只是多了个构造函数以及收缩空间. T2 SingleTon 单例问题. 用一个指针保存唯一的实例,用户无法在外部直接新建实例,只能使用外部接口(函数),函 ...
- [POJ3237]Tree解题报告|树链剖分|边剖
关于边剖 之前做的大多是点剖,其实转换到边剖非常简单. 我的做法是每个点的点权记录其到父亲节点的边的边权. 只要solve的时候不要把最上面的点记录在内就可以了. Tree Description Y ...
- #error#学习方法,如何避免初始化错误
#error#学习方法,如何避免初始化错误.错误来自:本博客的另一篇文章Demo示例程序源代码: ,01-导航实例-QQ空间.xcodeproj - CYLLoginViewController.mD ...