注意

toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量。

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")>

封装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, "再按一次退出")

最新文章

  1. Mysql数据库的基本概念和架构
  2. Cent OS yum 安装 Adobe flash player
  3. hdu 5249 KPI
  4. 操作失败,没有该服务权限![ 机构号:99 ,用户ID:50000009 ,服务号:0101030112 ]
  5. 【转】命令行使用7zip
  6. java selenium webdriver实战 seleniumIDE
  7. Hbase0.96源码之HMaster(一)
  8. UWP 手绘视频创作工具 “来画Pro” 技术分享系列
  9. Android studio怎么使用自定义的framework而避免冲突报错和点不进去报红。
  10. 用js提取字符串中的某一段字符
  11. 一些面试题(关于string的)
  12. 一脸懵逼学习KafKa集群的安装搭建--(一种高吞吐量的分布式发布订阅消息系统)
  13. springboot+websocket 归纳收集
  14. 使用spring的特殊bean完成分散配置
  15. STL之Deque容器
  16. Cracking The Coding Interview 9.7
  17. 【转载】 H264的I/P/B帧类型判断
  18. Axiom3D写游戏:第一个窗口
  19. js中作用域和闭包
  20. Angular4 自制分页控件

热门文章

  1. 我的NopCommerce之旅(5): 缓存
  2. JsonConvert对象实现json与对象之间的转换
  3. bootstrop媒体对象、面板和Well
  4. 海康威视采集卡结合opencv使用(两种方法)-转
  5. Lucene-安装和运行Demo程序
  6. github小技巧之Creating a pull request 创建 pull 请求
  7. C++拾遗(三)——函数
  8. ADO1
  9. 从汇编看c++中的多态
  10. iview 表单验证 input 用失去焦点事件 blur, select下拉选框 要用change事件 验证