本篇转自博客:上海-悠悠

前言

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消息【转载】的更多相关文章

  1. Appium+python自动化获取toast消息的方法

    转载地址:https://www.cnblogs.com/shangren/p/8191879.html 1. 首先执行这个命令:npm install -g cnpm --registry=http ...

  2. Appium+python自动化获取toast消息(windows版)的方法

    原来用的Appium1.5.3GUI版本,那为什么升级呢? 为了兼容最新版本的iOS10和Android7 Xcode8升级后,将不支持使用UIAutomation,而是改为使用XCUITest了,并 ...

  3. Appium+python自动化22-Appium Desktop【转载】

    Appium Desktop 原滋原味的官方文档 Appium Desktop是一款用于Mac.Windows和Linux的开源应用,它提供了Appium自动化服务器在一个漂亮灵活的UI中的强大功能. ...

  4. Appium+python自动化6-Remote远程控制【转载】

    前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...

  5. Appium+python自动化28-name定位【转载】

    本篇转自博客:上海-悠悠 前言 appium1.5以下老的版本是可以通过name定位的,新版本从1.5以后都不支持name定位了 一. name定位报错 1.最新版appium V1.7用name定位 ...

  6. Appium+python自动化5-Appium Inspector【转载】

    前言    appium Inspector从入门到放弃!反正你都打开了,那就看下为什么要放弃吧! Appium Inspector是appium自带的一个元素定位工具,上一篇介绍了如何使用uiaut ...

  7. Appium+python自动化10-AVD 模拟器【转载】

    前言 有些小伙伴没android手机,这时候可以在电脑上开个模拟器玩玩 一.模拟器配置 1.双击启动AVD Manager,进入配置界面

  8. Appium+python自动化9-SDK Manager【转载】

    前言 SDK Manager到有哪些东西是必须安装的呢? 一.SDK Manager 1.双击打开SDK Manager界面

  9. Appium+python自动化20-查看iOS上app元素属性【转载】

    前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素Mac版的appium1.6的版 ...

  10. Appium+python自动化19-iOS模拟器(iOS Simulator)安装自家APP【转载】

    前言 做过iOS上app测试的小伙伴应该都知道,普通用户安装app都是从appstore下载安装,安装测试版本的app,一般就是开发给的二维码扫码安装, 或者开发给个.ipa的安装包文件,通过itoo ...

随机推荐

  1. JavaScript Map数据结构

    Array.prototype.remove = function (s) { for (var i = 0; i < this.length; i++) { if (s == this[i]) ...

  2. lintcode-49-字符大小写排序

    49-字符大小写排序 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序. 注意事项 小写字母或者大写字母他们之间不一定要保持在原始字符串中的相对位置. 样例 给出"abAc ...

  3. 【log4net】- 日志使用教程

    一.log4net简介: 1. Log4net的优点: 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的 ...

  4. input标签与label标签的“合作关系”

    一直忽略了input和label的关系.一次在做自定义单选框的时候又重新捡起来这对“兄弟”. label的for属性和input的id值一致的话,input和label就会组成一个组.例如: < ...

  5. js表单验证工具包

    常用的js表单验证方法大全 /* 非空校验 : isNull() 是否是数字: isNumber(field) trim函数: trim() lTrim() rTrim() 校验字符串是否为空: ch ...

  6. 集群hadoop ubuntu版

    搭建ubuntu版hadoop集群 用到的工具:VMware.hadoop-2.7.2.tar.jdk-8u65-linux-x64.tar.ubuntu-16.04-desktop-amd64.is ...

  7. 【bzoj3813】奇数国 线段树

    题目描述 给出一个长度为n的序列,每个数都可以由前60个质数的乘积表示,初始每个数都是3.支持两种操作:(1)修改一个数 (2)查询一段区间内所有数的乘积的欧拉函数值模19961993. 输入 第一行 ...

  8. BZOJ4105 THUSC2015平方运算(线段树)

    注意到模数被给出且非常小,做法肯定要依赖于一些与此相关的性质.找题解打表可以发现循环节长度的lcm不超过60. 考虑怎么用线段树维护循环.对线段树上每个点维护这段区间的循环节.在循环中的位置,如果未进 ...

  9. ARC075 F.Mirrored

    题目大意:给定D,询问有多少个数,它的翻转减去它本身等于D 题解做法很无脑,利用的是2^(L/2)的dfs,妥妥超时 于是找到了一种神奇的做法. #include <iostream> u ...

  10. 一张图彻底搞懂JavaScript的==运算

    一张图彻底搞懂JavaScript的==运算 来源 https://zhuanlan.zhihu.com/p/21650547 PS:最后,把图改了一下,仅供娱乐 : ) 大家知道,==是JavaSc ...