3、处理 app 元素

3.1 常用查找元素的方法

Xpath语法:https://www.runoob.com/xpath/xpath-syntax.html

import time
from datetime import datetime
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy


class TestDemo:

    def setup(self):
        caps = {
            "platformName": "android",
            "deviceName": "008640dd0804",
            "automationName": "uiautomator2",
            "appPackage": "com.xueqiu.android",
            "appActivity": ".view.WelcomeActivityAlias",
            "autoGrantPermissions": "true"
        }

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_agree").click()
        self.driver.implicitly_wait(20)

    # (1)Xpath查找1
    def test_001(self):
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='基金']").click()
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='基金排行']").click()

    # (2)Xpath查找2:从多个元素里面找1个
    def test_002(self):
        self.driver.find_elements(AppiumBy.XPATH, "//*[@text='基金排行'][1]").click()
        self.driver.find_elements(AppiumBy.XPATH, "//*[@text='基金排行']")[0].click()

    # (3)Xpath条件匹配1
    def test_003(self):
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='基金' and contains(@resource-id, 'tab_name')]").click()
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='基金排行']").click()

    # (4)Xpath条件匹配2
    def test_004(self):
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='基金']").click()
        self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text,'雪球') and contains(@label, '雪球')]").click()

    # (4)Xpath:定位toast
    def test_005(self):
        # 定位toast属性
        self.driver.find_element(AppiumBy.XPATH, "//*[@class='android.widget.Toast']").click()
        # 定位toast文本属性
        self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text, 'xxxx')]")
        self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text,'雪球') and contains(@label, '雪球')]").click()

    def teardown(self):
        self.driver.quit()

3.2 定位器:Appium.By

查看源码:AppiumBy是继承了By

from selenium.webdriver.common.by import By


class AppiumBy(By):
	# ios定位
    IOS_PREDICATE = '-ios predicate string'
    IOS_UIAUTOMATION = '-ios uiautomation'
    IOS_CLASS_CHAIN = '-ios class chain'
    # 安卓定位
    ANDROID_UIAUTOMATOR = '-android uiautomator'
    ANDROID_VIEWTAG = '-android viewtag'
    ANDROID_DATA_MATCHER = '-android datamatcher'
    ANDROID_VIEW_MATCHER = '-android viewmatcher'
    # Deprecated 已废弃的
    WINDOWS_UI_AUTOMATION = '-windows uiautomation'
    ACCESSIBILITY_ID = 'accessibility id'
    IMAGE = '-image'
    CUSTOM = '-custom'



class By(object):
    """
    Set of supported locator strategies.
    """

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"

  1. Accessibility ID

读取 UI 元素的唯一标识符。对于 XCUITest,它是元素的accessibility-id属性。对于 Android,它是元素的content-desc属性。

  1. Class name

对于 IOS,它是 XCUI 元素的全名,以 XCUIElementType 开头。对于 Android,它是 UIAutomator2 类的全名(例如:android.widget.TextView)

  1. ID

本机元素标识符。resource-id对于安卓;name对于 iOS

  1. Name:元素名称
  2. XPath

使用 xpath 搜索应用 XML 源(不推荐,存在性能问题)

  1. Image

通过将元素与 base 64 编码图像文件匹配来定位元素

  1. Android UiAutomator (UiAutomator2 only)

使用 UI Automator API,尤其是UiSelector类来定位元素。在 Appium 中,您将 Java 代码作为字符串发送到服务器,服务器在应用程序的环境中执行它,返回一个或多个元素

  1. IOS UIAutomation

在自动化 iOS 应用程序时,可以使用Apple 的Instruments框架来查找元素

Instruments框架:https://appium.io/docs/en/drivers/ios-uiautomation/index.html

3.3 点击元素

# 在其中心点单击元素
el = self.driver.find_element_by_accessibility_id('SomeId')
el.click()

3.4 向元素发送文本

self.driver.find_element_by_accessibility_id('SomeAccessibilityID').send_keys('Hello world!')

3.5 清理元素

self.driver.find_element_by_accessibility_id('SomeAccessibilityID').clear()

3.6 元素属性

3.6.1 获取元素文本

el = self.driver.find_element_by_accessibility_id('SomeAccessibilityID')
text = el.text

3.6.2 获取元素的标签名称

tagName = self.driver.find_element_by_accessibility_id('SomeAccessibilityID').tag_name

3.6.3 获取元素属性

tagName = self.driver.find_element_by_accessibility_id('SomeAccessibilityID').get_attribute('content-desc')

3.6.4 元素是否被选中

self.driver.find_element_by_accessibility_id('SomeAccessibilityID').is_selected()

3.6.5 元素是否被启用

self.driver.find_element_by_accessibility_id('SomeAccessibilityID').is_enabled()

3.6.6 元素是否显示

self.driver.find_element_by_accessibility_id('SomeAccessibilityID').is_displayed()

3.6.7 获取元素位置

location = self.driver.find_element_by_accessibility_id('SomeAccessibilityID').location

3.6.8 获取元素大小

size = self.driver.find_element_by_accessibility_id('SomeAccessibilityID').size

3.6.9 获获取元素的尺寸和坐标

element = self.driver.find_element_by_accessibility_id('SomeAccessibilityID')
element.rect

3.6.10 获取 Web 元素的 CSS 计算属性值

cssProperty = self.driver.find_element_by_accessibility_id('SomeId').value_of_css_property("style")

3.7 清理元素

self.driver.find_element_by_accessibility_id('SomeAccessibilityID').clear()

3.8 提交表单

el = self.driver.find_element_by_accessibility_id('SomeAccessibilityID')
el.submit()

3.9 获取当前会话的活动元素

element = driver.switch_to.active_element

3.10 元素是否相等

// Overrides the Java Object .equals method
MobileElement elementOne = (MobileElement) driver.findElementByClassName("SomeClassName");
MobileElement elementTwo = (MobileElement) driver.findElementByClassName("SomeOtherClassName");
boolean isEqual = elementOne.equals(elementTwo);
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐