Appium+python自动化29-toast消息(亲测 ok)
本篇转自博客:上海-悠悠
前言
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, "再按一次退出")
```
参考: https://www.cnblogs.com/yoyoketang/p/7810507.html
自己的实验:
# 定位toast元素
toast_loc = ("xpath", ".//*[contains(@text,'分享成功')]")
t = WebDriverWait(dr2, 10, 0.1).until(EC.presence_of_element_located(toast_loc))
print t.text.encode('utf-8')
ok的
Appium+python自动化29-toast消息(亲测 ok)的更多相关文章
- 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自动化8-Appium Python API
Appium+python自动化8-AppiumPython API 前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts conte ...
- Appium+python自动化
名称 链接地址 Appium+python自动化8-Appium Python API(上) http://mp.weixin.qq.com/s/WvpT5oRrYY22avI95FuypQ Appi ...
- appium+python自动化61-中文输入乱码问题解决
前言 在夜神模拟器上输入中文,发现是乱码,将unicodeKeyboard和resetKeyboard参数设置为True了,发现还是没法解决. 打开手机设置语言和输入法,发现找不到Appium And ...
- appium+python自动化52-多点触控MultiAction
前言 MultiAction是针对多点触控操作的,是TouchAction的一个补充模块 TouchAction用法参考前面的一篇:appium+python自动化33-TouchAction 多点触 ...
- 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 ...
- appium+python自动化50-生成定位对象模板templet(jinja2)
前言 每次自己写pageobject定位元素对象太繁琐,格式都差不多,只是换个定位方法,这种就可以才有模板的方式,批量生成pageobject定位元素对象的模板 python里面生成模板有两个模块可以 ...
- Appium+python自动化20-查看iOS上app元素属性【转载】
前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素Mac版的appium1.6的版 ...
随机推荐
- VS路径定义
你可以在项目“属性页”对话框中接受字符串的任意位置使用这些宏. 这些宏不区分大小写. 若要显示当前可用的宏,请在属性名称右侧列中单击下拉箭头. 如果“编辑”可用,请单击它,然后在“编辑”对话框中单击“ ...
- Win10/Server2016镜像集成离线补丁
Win10镜像集成离线补丁 因为正常安装系统后再打补丁比较漫长,可以事先做好打过补丁的iso,备将来使用. 以管理员身份运行cmd,然后通过dism提取.挂载.集成补丁.保存install.wim镜像 ...
- mysql 自查询
先介绍用法: 用法解释 select * from a,b : shopping 商品表 , baixitest SELECT b.name from shopping as a, baixi ...
- VMware虚拟机克隆Linux系统引起的网卡问题
1. 手动配置静态网卡地址不生效2. 网卡名变成了eth1[root@localhost network-scripts]# ls |grep ifcfg ifcfg-eth0 ifcfg-lo [r ...
- 011-对象——interface接口说明与使用方式实例
<?php /** interface接口说明与使用方式实例 * * 接口里面的方法全是抽象方法,没有实体的方法.这样的类我们就叫做接口.定义的时候用Interface定义.实现接口时用impl ...
- LeetCode OJ:Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 更改Linux栈空间大小
1.通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M 2.通过命令 ulimit -s 设置大小值 临时改变栈空间大小:ulimit -s 102400 ...
- [置顶]
【Android实战】----从Retrofit源码分析到Java网络编程以及HTTP权威指南想到的
一.简介 接上一篇[Android实战]----基于Retrofit实现多图片/文件.图文上传中曾说非常想搞明白为什么Retrofit那么屌.最近也看了一些其源码分析的文章以及亲自查看了源码,发现其对 ...
- Helix Server 支持的文件格式
比如,对于WMV格式的文件,访问路径可以是:mms://192.168.1.1/mov/music.wmv 对于rm格式的文件rtsp://192.168.1.1/mov/123.rm 但,比如对于格 ...
- (三十六)类数组对象arguments
类数组对象:arguments 在函数调用时,我们总能见到arguments这个对象,它具体是用来干什么的呢?感觉逼格非常高呢 函数在使用时,我们总会位函数传入各种参数,arguments会将参数储存 ...