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 ==============================
目录
相关文章
|
4月前
Appium使用UiSelector封装文本定位方法find_element_by_text
Appium使用UiSelector封装文本定位方法find_element_by_text
29 1
|
5月前
|
编解码 Java 测试技术
『App自动化测试之Appium应用篇』| uiautomator + accessibility_id定位方法完全使用攻略
『App自动化测试之Appium应用篇』| uiautomator + accessibility_id定位方法完全使用攻略
129 0
|
JSON 监控 测试技术
app自动化测试之Appium问题分析及定位
app自动化测试之Appium问题分析及定位
210 0
|
JSON 监控 测试技术
干货|app自动化测试之Appium问题分析及定位
干货|app自动化测试之Appium问题分析及定位
|
XML JavaScript Java
appium定位-控件定位
客户端的页面通过 XML 来实现 UI 的布局,页面的 UI 布局作为一个树形结构,而树叶被定义为节点。这里的节点也就对应了要定位的元素
|
前端开发
Appium自动化(8) - 可定位的控件属性
Appium自动化(8) - 可定位的控件属性
159 0
Appium自动化(8) - 可定位的控件属性
|
XML JSON Android开发
Appium自动化(7) - 控件定位工具之Appium 的 Inspector
Appium自动化(7) - 控件定位工具之Appium 的 Inspector
558 0
Appium自动化(7) - 控件定位工具之Appium 的 Inspector
|
XML 存储 开发工具
Appium自动化(6) - 控件定位工具之uiautomatorviewer 的详细介绍
Appium自动化(6) - 控件定位工具之uiautomatorviewer 的详细介绍
236 0
Appium自动化(6) - 控件定位工具之uiautomatorviewer 的详细介绍
|
5月前
|
XML Java 测试技术
『App自动化测试之Appium应用篇』| 元素定位工具Appium-Inspector从简介、安装、配置到使用的完整攻略
『App自动化测试之Appium应用篇』| 元素定位工具Appium-Inspector从简介、安装、配置到使用的完整攻略
181 3
|
5月前
|
JavaScript Java 测试技术
『App自动化测试之Appium基础篇』| 从定义、原理、环境搭建、安装问题排查等深入了解Appium
『App自动化测试之Appium基础篇』| 从定义、原理、环境搭建、安装问题排查等深入了解Appium
692 0