当前位置: 首页 > news >正文

Appium获取toast方法封装

一、前置说明

toast消失的很快,并且通过uiautomatorviewer也不能获取到它的定位信息,如下图:

二、操作步骤

toast的class name值为android.widget.Toast,虽然toast消失的很快,但是它终究是在Dom结构中出现过,所以我们可以使用xpath来定位toast元素:

    def get_toast(self, text=None, timeout=3, interval=0.5):# 如果同时出现多个toast,可以使用这种方式if text:return WebDriverWait(self, timeout, interval).until(EC.presence_of_element_located(('xpath', f'//*[contains(@text, "{text}")]')))return WebDriverWait(self, timeout, interval).until(EC.presence_of_element_located(('xpath', '//*[@class="android.widget.Toast"]')))

三、Demo验证

注意:appium在v1.6.3以上才支持获取toast,并且需要指定使用Uiautomator2库。

def test_get_toast():import logginglogging.basicConfig(level=logging.DEBUG)from driver.appium.driver import WebDriverappium_server_url = 'http://localhost:4723'capabilities = {"platformName": "Android","automationName": "uiautomator2",  # 注意:要指定uiautomator2"deviceName": "127.0.0.1:62001","app": "D:\\resources\\ApiDemos-debug.apk",}driver = WebDriver(command_executor=appium_server_url, capabilities=capabilities)driver.smart_find_element(by='text', value='App').click()driver.smart_find_element(by='text', value='Notification').click()driver.smart_find_element(by='text', value='NotifyWithText').click()driver.smart_find_element(by='text', value='SHOW SHORT NOTIFICATION').click()element = driver.get_toast('Short notification')assert element.text == 'Short notification'

日志输出:

============================= test session starts =============================
collecting ... collected 1 itemtest_appium.py::test_get_toast PASSED============================= 1 passed in 19.91s ==============================

欢迎技术交流:

http://www.lryc.cn/news/256255.html

相关文章:

  • Google Guava简析
  • 反序列化漏洞详解(二)
  • React全站框架Next.js使用入门
  • 【操作系统笔记】-文件系统
  • 第二十一章 网络通信
  • 【漏洞复现】万户协同办公平台ezoffice wpsservlet接口存在任意文件上传漏洞 附POC
  • 【uniapp】小程序中input输入框的placeholder-class不生效解决办法
  • SimplePIR——目前最快单服务器匿踪查询方案
  • Spring Boot中使用Swagger
  • uniapp实战 —— 竖排多级分类展示
  • SAP UI5 walkthrough step6 Modules
  • 时间相关类
  • 数据库事务:保障数据一致性的基石
  • 自动化操作脚本
  • MVC、MVP、MVVM模式的区别
  • 【Vue】日常错误总结(持续更新)
  • java多线程(常用方法、实现方式、线程安全问题、生命周期、线程池)
  • Day05 linux高级系统设计 - 管道
  • 低代码:美味膳食或垃圾食品?
  • 免费网页抓取工具大全【附下载和工具使用教程】
  • Leetcode 39 组合总和
  • Windows下使用AndroidStudio及CMake编译Android可执行程序或静态库动态库
  • MySQL七 | 存储引擎
  • 网上下载的pdf文件,为什么不能复制文字?
  • Linux下apisix离线安装教程
  • 基于STM32 + DMA介绍,应用和步骤详解(ADC多通道)
  • openGauss学习笔记-144 openGauss 数据库运维-例行维护-慢sql诊断
  • 计算机毕业设计springboot+ssm停车场车位预约系统java
  • 打破常规思维:Scrapy处理豆瓣视频下载的方式
  • 系列学习前端之第 2 章:一文精通 HTML