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 ==============================
目录
相关文章
|
3月前
Appium使用UiSelector封装文本定位方法find_element_by_text
Appium使用UiSelector封装文本定位方法find_element_by_text
29 1
|
3月前
|
小程序 Android开发
Appium微信小程序自动化之开启webview调试功能方法封装
Appium微信小程序自动化之开启webview调试功能方法封装
82 0
|
3月前
Appium自动化常用adb操作封装
Appium自动化常用adb操作封装
25 0
|
3月前
|
测试技术
Appium自动化测试swipe滑动封装
Appium自动化测试swipe滑动封装
24 0
|
8月前
|
JavaScript 前端开发 Python
appium--使用PyYAML封装Capability
appium--使用PyYAML封装Capability
|
测试技术
Appium自动化框架从0到1之 测试用例封装
Appium自动化框架从0到1之 测试用例封装
93 0
|
数据安全/隐私保护
Appium自动化框架从0到1之 业务模块封装(登录页面业务操作)
Appium自动化框架从0到1之 业务模块封装(登录页面业务操作)
111 0
Appium自动化框架从0到1之 业务模块封装(登录页面业务操作)
Appium自动化框架从0到1之 基类的封装
Appium自动化框架从0到1之 基类的封装
102 0
|
测试技术
Appium自动化框架从0到1之 公共方法的封装
Appium自动化框架从0到1之 公共方法的封装
91 0
Appium自动化框架从0到1之Driver驱动的封装
Appium自动化框架从0到1之Driver驱动的封装
197 0