Appium获取toast方法封装

简介: 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 ==============================
目录
相关文章
|
9月前
Appium使用UiSelector封装文本定位方法find_element_by_text
Appium使用UiSelector封装文本定位方法find_element_by_text
103 1
|
9月前
|
测试技术 API Python
Appium控件交互策略:优化自动化测试效率的关键方法
该文介绍了如何使用Selenium与APP进行交互,包括点击、输入和状态判断等操作。例如,通过element.click()点击控件,element.send_keys()输入文本,以及element.is_displayed()检查元素是否可见。还展示了如何获取元素属性,如resource-id、text和class,并提供了Python代码示例来定位并操作APP元素,如滑动条的显示、可点击性检测及点击滑动条中心位置。在编写测试脚本时,应注意元素定位和状态验证以确保测试稳定性。
|
9月前
|
编解码 Java 测试技术
『App自动化测试之Appium应用篇』| uiautomator + accessibility_id定位方法完全使用攻略
『App自动化测试之Appium应用篇』| uiautomator + accessibility_id定位方法完全使用攻略
323 0
|
9月前
|
移动开发 安全 测试技术
『App自动化测试之Appium应用篇』| 继承于selenium常用的元素定位方法有哪些?如何使用?
『App自动化测试之Appium应用篇』| 继承于selenium常用的元素定位方法有哪些?如何使用?
272 0
|
9月前
|
测试技术 Python
多种方法实现Appium屏幕滑动:让用户仿真动作更简单
本文介绍了Appium在移动端自动化测试中如何模拟用户滑动操作。滑动常见于触摸事件模拟,坐标计算和惯性滑动场景。Appium提供了`swipe`和`scroll`两种方法:`swipe`需要指定起始和结束坐标及可选的持续时间;`scroll`则直接使用起始和结束元素进行滑动。文中给出了Python示例代码,展示了如何在不同场景下执行滑动操作。
|
9月前
|
Python
Python Appium Selenium 查杀进程的实用方法
Python Appium Selenium 查杀进程的实用方法
101 1
|
9月前
Appium文本定位方法实现find_element_by_text
Appium文本定位方法实现find_element_by_text
80 0
|
9月前
|
小程序 Android开发
Appium微信小程序自动化之开启webview调试功能方法封装
Appium微信小程序自动化之开启webview调试功能方法封装
246 0
|
9月前
Appium自动化常用adb操作封装
Appium自动化常用adb操作封装
83 0
|
9月前
|
测试技术
Appium自动化测试swipe滑动封装
Appium自动化测试swipe滑动封装
109 0