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

前言

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. HDU 1445 Ride to School

    http://acm.hdu.edu.cn/showproblem.php?pid=1445 Problem Description Many graduate students of Peking ...

  2. PHP蜘蛛爬虫开发文档

    <我用爬虫一天时间“偷了”知乎一百万用户,只为证明PHP是世界上最好的语言 >所使用的程序框架 编写PHP网络爬虫, 需要具备以下技能: 爬虫采用PHP编写 从网页中抽取数据需要用XPat ...

  3. Java 利用枚举实现单例模式

    引言 单例模式比较常见的实现方法有懒汉模式,DCL模式公有静态成员等,从Java 1.5版本起,单元素枚举实现单例模式成为最佳的方法. Java枚举 基本用法 枚举的用法比较多,本文主要旨在介绍利用枚 ...

  4. 前端MVC

    闲来没事,画了个张图,是我理解的MVC

  5. Dubbo 项目与传统项目

    1.什么是传统工程 单工程 MVC 架构 控制层通过调用服务层完成业务逻辑处理 业务层调用持久层进程数据操作 2.什么是分布式工程 将传统项目的单工程结构,拆分成多工程 一般会有这几个工程: 父工程: ...

  6. Hibernate对象状态

    对象状态 瞬时(transient) 自己new出来的对象,数据库没有记录与之对应,与session也没有关联 持久(persistent) 数据库中有记录与之对应,当前与session有关联,相关的 ...

  7. P3032 [USACO11NOV]二进制数独Binary Sudoku

    题目描述 Farmer John's cows like to play an interesting variant of the popular game of "Sudoku" ...

  8. 防恶意解析,禁止用IP访问网站的Apache设置

    一般来说,网站可以用域名和IP来访问.你的网站可以通过IP直接访问,本来这没什么问题,但是会有些隐患: 由于搜索引擎也会收录你的IP地址的页面,所以同一个页面搜索引擎会重复收录,造成页面的权重不如单个 ...

  9. bulk_insert_buffer_size and InnoDB

    Q: I read the following on this page http://dev.mysql.com/doc/mysql/en/server-system-variables.html ...

  10. Fabric证书解析

    一.证书目录解析   通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下:   ca: 存放组织的根证书和对应的私 ...