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 logging
    logging.basicConfig(level=logging.DEBUG)

    from driver.appium.driver import WebDriver

    appium_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 item

test_appium.py::test_get_toast PASSED

============================= 1 passed in 19.91s ==============================

欢迎技术交流:

相关推荐

  1. Toast.makeText() 使用方法

    2023-12-09 00:52:01       12 阅读
  2. <span style='color:red;'>appium</span>

    appium

    2023-12-09 00:52:01      89 阅读
  3. <span style='color:red;'>appium</span>

    appium

    2023-12-09 00:52:01      10 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 00:52:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 00:52:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 00:52:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 00:52:01       20 阅读

热门阅读

  1. CentOS 7 安装 Nginx

    2023-12-09 00:52:01       34 阅读
  2. 解决Nginx访问php文件直接下载

    2023-12-09 00:52:01       33 阅读
  3. React 好用的工具库

    2023-12-09 00:52:01       35 阅读
  4. 关于 React 如何自定义配置 HighCharts tooltip

    2023-12-09 00:52:01       33 阅读
  5. CentOS 7 安装 Nginx

    2023-12-09 00:52:01       36 阅读
  6. 与脾气不太好的领导,相处原则和相处技巧分享

    2023-12-09 00:52:01       33 阅读
  7. flutter布局详解及代码示例(补充)

    2023-12-09 00:52:01       34 阅读
  8. MySQL 错误 1292 是什么?怎么解决?

    2023-12-09 00:52:01       38 阅读
  9. MySQL查看和修改时区

    2023-12-09 00:52:01       39 阅读
  10. 数据结构详解各种算法

    2023-12-09 00:52:01       35 阅读