Appium文本定位方法实现find_element_by_text

简介: Appium文本定位方法实现find_element_by_text

一、前置说明


Appium升级后不再支持使用name定位,我们可以使用UiSelector文本定位解决这个问题,而且UiSelector支持更加灵活的text定位方式。


二、代码实现


def find_elements_by_text(self, value, platform='android', rule='full'):
        CommonChecker.check_item_in_list(platform, ['android', 'ios'], field_name='platform')
        CommonChecker.check_item_in_list(rule, ['full', 'partial', 'startswith', 'regex'], field_name='rule')
        selector_method = {
            'full': 'text',
            'partial': 'textContains',
            'startswith': 'textStartsWith',
            'regex': 'textMatches'
        }
        statement = f'new UiSelector().{selector_method[rule]}("{value}")'
        if platform == 'android':
            return self.find_elements(AppiumBy.ANDROID_UIAUTOMATOR, statement)
        elif platform == 'ios':
            return self.find_elements(AppiumBy.IOS_UIAUTOMATION, statement)
        else:
            raise ValueError(f"Unsupported platform: {platform}")
    def find_element_by_text(self, value, platform='android', rule='full', index=0) -> WebElement:
        elements = self.find_elements_by_text(value, platform, rule)
        if not elements:
            raise NoSuchElementException(f"No elements found for value: {value}, platform: {platform}, rule: {rule}")
        return CommonChecker.check_and_get_element_by_index(elements, index, field_name='index')


三、Demo验证


测试代码:

def test_find_element_by_text():
    from driver.appium.driver import WebDriver
    appium_server_url = 'http://localhost:4723'
    capabilities = {
        "platformName": "Android",
        "automationName": "uiautomator2",
        "deviceName": "127.0.0.1:62001",
        "app": "D:\\resources\\imooc.apk",
    }
    driver = WebDriver(command_executor=appium_server_url, capabilities=capabilities)
    time.sleep(2)
    driver.swipe_to('left', num=2)
    time.sleep(2)
    driver.find_element('id', 'cn.com.open.mooc:id/viewpager').click()
    time.sleep(3)
    ele = driver.find_element_by_text(value='注册', rule='full')
    assert ele.text == '注册'
    ele = driver.find_element_by_text(value='去登录', rule='partial')
    assert ele.text == '已有慕课网账号? 去登录'
    ele = driver.find_element_by_text(value='注册慕课网账号', rule='startswith')
    assert ele.text == '注册慕课网账号'
    ele = driver.find_element_by_text(value='^注册.*账号$', rule='regex')
    assert ele.text == '注册慕课网账号'

控制台输出结果:

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