python+appium自动化测试-元素定位之android uiautomatorandroid uiautomator(一)

1,265 阅读2分钟

来自APP Android端自动化测试初学者的笔记,写的不对的地方大家多多指教哦。

在之前有介绍一些平时常用的元素定位方式,最近在学习过程中有发现另一个比较好用的定位方式,是Android独有的定位方式:android uiautomator,是Android 系统原生支持的定位方式,一种强有力的定位方式,原理是通过android 自带的android uiautomator的类库去查找元素。Appium元素定位方法其实也是基于Uiautomator来进行封装的,使用方法 find_element_by_android_uiautomator() 可以运用UiAutomator元素定位。

UiAutomator元素定位的方法名和对应Android的属性如下:

image.png

image.png

一、常规模式下的android uiautomator元素定位

以下定位方式都与appium封装好的定位方式是一样的,只是将写法换成了android_uiautomator

1.android uiautomator—resourceId定位

el1 = self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.sina.weibo:id/et_pws_username")')
el1.send_keys("123")

2.android uiautomator—text定位

el2 = self.driver.find_element_by_android_uiautomator('new UiSelector().text("手机号或者邮箱")')
el1.send_keys("123")

3.android uiautomator—className

el2 = self.driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")')
el1.send_keys("123")
or:
el2 = self.driver.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.EditText")')
el1.send_keys("123")

4.android uiautomator—description(对应content-desc)

el2 = self.driver.find_element_by_android_uiautomator('new UiSelector().description("xxx")')
el1.send_keys("123")

二、PO模式下的android uiautomator元素定位

1.base_page.py 封装android uiautomator定位

# 基础类,封装元素定位操作
from appium import webdriver

class BasePage:
    def __init__(self, driver: webdriver):
        self.driver = driver

    # 根据android-uiautomator定位
    def element_android_uiautomator(self, locator):
        return self.driver.find_element_by_android_uiautomator(locator)

    # 多个元素的resourceId/className/text/description一致
    def elements_android_uiautomator(self, locator):
        return self.driver.find_elements_by_android_uiautomator(locator)

2.account_pwd_login.py 封装账号密码登录页面操作

a.android uiautomator—resourceId定位

# 封装账号密码登录页面操作
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # 账号输入框
    _et_account = 'resourceId("com.sina.weibo:id/et_pws_username")'

    # 输入账号,调用android-uiautomator定位
    def input_account(self, account):
        self.element_android_uiautomator(self._et_account).send_keys(account)

b.android uiautomator—text定位

# 基础类,封装元素定位操作
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # 账号输入框
    _et_account = 'text("手机号或者邮箱")'

    # 输入账号,调用android-uiautomator定位
    def input_account(self, account):
        self.element_android_uiautomator(self._et_account).send_keys(account)

c.android uiautomator—className

# 基础类,封装元素定位操作
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # 账号输入框
    _et_account = 'className("android.widget.EditText")'

    # 输入账号,调用android-uiautomator定位
    def input_account(self, account):
        self.element_android_uiautomator(self._et_account).send_keys(account)

or

    # 输入账号,调用android-uiautomator定位
    def input_account(self, account):
        self.elements_android_uiautomator(self._et_account).send_keys(account)

d.android uiautomator—description(对应content-desc)

# 基础类,封装元素定位操作
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # 账号输入框
    _et_account = 'description("xxx")'

    # 输入账号,调用android-uiautomator定位
    def input_account(self, account):
        self.element_android_uiautomator(self._et_account).send_keys(account)