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

前言

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的

最新文章

  1. ABAP实现屏幕自己刷新和跳转功能
  2. 学习笔记:Hashtable和HashMap
  3. 前端 head 中mate 详解
  4. 诺基亚XL中Intent.ACTION_VIEW无效的问题
  5. LOB字段存放在指定表空间 清理CLOB字段及压缩CLOB空间
  6. hql抓取要注意的点
  7. 移动开发 android 入门开发 阶段视频
  8. Oracle把两个空格以上的空格,替换为两个空格
  9. Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法
  10. IOS UI篇—UILabel的文字顶部对齐
  11. Unity3D游戏开发从零单排(五) - 导入CS模型到Unity3D
  12. 移动app接口编程技术-学习实现之PHP进阶 数组
  13. Mac OS启动服务优化高级篇(launchd tuning)
  14. Java内部类之匿名内部类
  15. socket系列之什么是socket
  16. Servlet常用的接口和类
  17. PHP安装Commposer
  18. cookie、localStorage、sessionStorage的区别
  19. 13.14.15.16.17&amp;《一个程序猿的生命周期》读后感
  20. Linux账号管理

热门文章

  1. CocoaPods学习系列5——错误集锦
  2. 使用Entity Framework时遇到的各种问题总结
  3. [javascript]JS如何获取当前时间戳
  4. Java动态代理的总结
  5. SSL和SSH的区别
  6. Codeforces Round #299 (Div. 2)D. Tavas and Malekas
  7. VMware 虚拟镜像转 Hyper-V(Win10/2016)
  8. linux应用之wget命令详解
  9. Linux CentOS服务启动
  10. 利用pycharm远程调试openstack代码