美文网首页
Appium UiAutomator2驱动:界面元素属性介绍

Appium UiAutomator2驱动:界面元素属性介绍

作者: Domibaba | 来源:发表于2023-08-07 12:07 被阅读0次

    1、UiAutomator2驱动支持界面元素属性如下表

    属性名称 具体描述 举例说明
    checkable 元素是否可检查。 true
    checked 元素是否是检查状态,通常false表示元素不可检查。 false
    classclassName 元素类型全名,对于某些元素类型名称可能是null android.view.View
    clickable 元素是否可被点击。 false
    content-desccontentDescription 可访问元素的内容描述属性。 Contacts
    enabled 元素是否可被点击。(存疑) true
    focusable 元素是否可被聚焦(焦点)。 true
    focused 元素当前是否是焦点,通常false表示元素不可聚焦。 false
    long-clickablelongClickable 元素是否支持长按。 false
    package 元素所属的包的标识符。 com.google.android.diale
    password 元素是否是密码输入字段。 true
    resource-idresourceId 元素的资源标识符(资源id),可能是null com.google.android.dialer:id/tab_contacts
    scrollable 元素是否可滚动。 true
    selection-start 包含开始选择的字符的索引,若元素未提供范围信息,该值可能是null 5
    selection-end 包含结束选择的字符的索引,若元素未提供范围信息,该值可能是null 8
    selected 元素是否被选中。 false
    textname 元素的文本内容,不会为空。 Contacts
    hint 元素的提示文本,低于Android版本Oreo,该值通常为null my hint text
    bounds 元素可见框架([left, top][right, bottom]) [0,0][100,100]
    displayed 元素对于用户是否可见。 true
    contentSize 元素内容区域的信息,例如长度宽度等 {"left": 0, "top":0, "width": 100, "height": 100, "scrollableOffset": 10, "touchPadding": 0}
    extras getExtras的结果,结果包含以分号;分隔的所有key=value键值对,如果结果是空,那么只包含key=部分。 chrome浏览器extras的结果:AccessibilityNodeInfo.roleDescription=; AccessibilityNodeInfo.chromeRole=rootWebArea; ACTION_ARGUMENT_HTML_ELEMENT_STRING_VALUES= ARTICLE,BLOCKQUOTE,BUTTON,CHECKBOX

    2、界面元素属性示例展示

    本文所有测试代码的前提是已经安装Appium和相关环境(例如JDKAndroid SDKAVD模拟器),可以参考Appium环境搭建

    所使用的相关软件及版本如下:

    软件名称 软件版本
    Linux操作系统 Ubuntu 22.04
    JDK 20.0.1
    Android SDK 33
    node.js 18.17
    npm 9.6.7
    Appium Server 2.0.1
    Appium Client 2.11.1
    AVD(模拟器) /
    Python 3.10.6

    下面演示了从模拟器获取到联系人页面后,获取联系人页面元素属性,所有的属性需要通过元素的接口get_attribute进行获取,代码示例如下:

    # -*- coding: utf-8 -*-
    
    import pytest
    
    from appium import webdriver
    from appium.options.android import UiAutomator2Options
    from appium.webdriver.appium_service import AppiumService
    from appium.webdriver.common.appiumby import AppiumBy
    
    # 开启服务端
    APPIUM_HOST = '127.0.0.1'
    APPIUM_PORT = 4723
    @pytest.fixture(scope="session")
    def start_appium_service():
     server = AppiumService()
     server.start(args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)], timeout_ms=60000)
     yield server
     server.stop()
    
    # 创建客户端到服务端的会话
    def create_appium_session_by_api(custom_opts = None):
     options = UiAutomator2Options()
     if custom_opts is not None:
     options.load_capabilities(custom_opts)
     return webdriver.Remote(f'http://{APPIUM_HOST}:{APPIUM_PORT}', options=options)
    
    def test_element_attribute(start_appium_service):
     custom_opts = {
     "appium:appPackage": "com.google.android.dialer",
     "appium:appActivity": ".extensions.GoogleDialtactsActivity",
     "appium:avd": "testPhone",
     }
     driver = create_appium_session_by_api(custom_opts)
    
     # 获取联系人页面
     contact_id_full = "com.google.android.dialer:id/tab_contacts"
     contact = driver.find_element(AppiumBy.ID, value=contact_id_full)
    
     print("\n=======print element attribute==========")
     print(f"======= checkable: {contact.get_attribute('checkable')}")
     print(f"======= checked: {contact.get_attribute('checked')}")
     print(f"======= class/className: {contact.get_attribute('className')}")
     print(f"======= clickable: {contact.get_attribute('clickable')}")
     print(f"======= content-desc/contentDescription: {contact.get_attribute('content-desc')}")
     print(f"======= enabled: {contact.get_attribute('enabled')}")
     print(f"======= focusable: {contact.get_attribute('focusable')}")
     print(f"======= focused: {contact.get_attribute('focused')}")
     print(f"======= long-clickable/longClickable: {contact.get_attribute('long-clickable')}")
     print(f"======= package: {contact.get_attribute('package')}")
     print(f"======= password: {contact.get_attribute('password')}")
     print(f"======= resource-id/resourceId: {contact.get_attribute('resource-id')}")
     print(f"======= scrollable: {contact.get_attribute('scrollable')}")
     print(f"======= selection-start: {contact.get_attribute('selection-start')}")
     print(f"======= selection-end: {contact.get_attribute('selection-end')}")
     print(f"======= selected: {contact.get_attribute('selected')}")
     print(f"======= text/name: {contact.get_attribute('name')}")
     print(f"======= hint: {contact.get_attribute('hint')}")
     print(f"======= bounds: {contact.get_attribute('bounds')}")
     print(f"======= displayed: {contact.get_attribute('displayed')}")
     print(f"======= contentSize: {contact.get_attribute('contentSize')}")
     print(f"======= extras: {contact.get_attribute('extras')}")
    

    可能的运行结果:

    =======print element attribute==========
    ======= checkable: false
    ======= checked: false
    ======= class/className: android.widget.FrameLayout
    ======= clickable: true
    ======= content-desc/contentDescription: Contacts
    ======= enabled: true
    ======= focusable: true
    ======= focused: false
    ======= long-clickable/longClickable: false
    ======= package: com.google.android.dialer
    ======= password: false
    ======= resource-id/resourceId: com.google.android.dialer:id/tab_contacts
    ======= scrollable: false
    ======= selection-start: None
    ======= selection-end: None
    ======= selected: false
    ======= text/name: 
    ======= hint: None
    ======= bounds: [214,560][320,640]
    ======= displayed: true
    ======= contentSize: {"width":106,"height":80,"top":560,"left":214,"scrollableOffset":0,"touchPadding":8}
    ======= extras: AccessibilityNodeInfo.roleDescription=Tab
    

    附录

    1.官方介绍https://github.com/appium/appium-uiautomator2-driver
    2.文章初始来源https://gitee.com/shengzhemi/MyPractice/wikis/projects/appium/Appium%20UiAutomator2%E9%A9%B1%E5%8A%A8%EF%BC%9A%E7%95%8C%E9%9D%A2%E5%85%83%E7%B4%A0%E5%B1%9E%E6%80%A7%E4%BB%8B%E7%BB%8D

    相关文章

      网友评论

          本文标题:Appium UiAutomator2驱动:界面元素属性介绍

          本文链接:https://www.haomeiwen.com/subject/ntdgpdtx.html