From APP Android end automation test beginner’s notes, write wrong place we a lot of advice oh.

Before, I introduced some commonly used element positioning methods. Recently, I found another convenient positioning method in the learning process, which is unique to Android: Android UIautomator is a localization method supported by the Android system. It is a powerful localization method. The principle is to find elements through the android class library of Android UIautomator. The Appium element positioning method is actually encapsulated based on Uiautomator. The method find_element_by_Android_UIautomator () can use Uiautomator element positioning.

The UiAutomator element locates the method name and corresponding Android properties as follows:

1. Positioning of Android UIautomator elements in normal mode

The following positioning methods are the same as those encapsulated in Appium, except that they are written android_UIautomator

1. The android uiautomator – resourceId positioning

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

2. The android uiautomator – text location

el2 = self.driver.find_element_by_android_uiautomator('new UiSelector().text(" phone number or email ")')
el1.send_keys("123")
Copy the code

3. The android uiautomator – the 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")
Copy the code

4. Android UIautomator — Description (corresponding to content-desc)

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

Ii. Positioning of Android UIautomator elements in PO mode

1. Base_page. py encapsulates the positioning of android UIautomator

Base class that encapsulates element positioning operations
from appium import webdriver

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

    # Locate according to Android-UIautomator
    def element_android_uiautomator(self, locator) :
        return self.driver.find_element_by_android_uiautomator(locator)

    # multiple elements resourceId className/text/description
    def elements_android_uiautomator(self, locator) :
        return self.driver.find_elements_by_android_uiautomator(locator)
Copy the code

2. Account_pwd_login. py Encapsulates the login operation of the account and password

A.a ndroid uiautomator – resourceId positioning

Encapsulate account password login page operation
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # Account input box
    _et_account = 'resourceId("com.sina.weibo:id/et_pws_username")'

    # Enter the account number and call Android-UIautomator to locate
    def input_account(self, account) :
        self.element_android_uiautomator(self._et_account).send_keys(account)
Copy the code

B.a ndroid uiautomator – text location

Base class that encapsulates element positioning operations
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # Account input box
    _et_account = 'text(" phone number or email address ")'

    # Enter the account number and call Android-UIautomator to locate
    def input_account(self, account) :
        self.element_android_uiautomator(self._et_account).send_keys(account)
Copy the code

C.a. ndroid uiautomator – the className

Base class that encapsulates element positioning operations
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # Account input box
    _et_account = 'className("android.widget.EditText")'

    # Enter the account number and call Android-UIautomator to locate
    def input_account(self, account) :
        self.element_android_uiautomator(self._et_account).send_keys(account)

or

    # Enter the account number and call Android-UIautomator to locate
    def input_account(self, account) :
        self.elements_android_uiautomator(self._et_account).send_keys(account)
Copy the code

D. ndroid UIautomator — description(corresponding to content-desc)

Base class that encapsulates element positioning operations
from appium import webdriver
import BasePage

class AccountPwdLogin:

    # Account input box
    _et_account = 'description("xxx")'

    # Enter the account number and call Android-UIautomator to locate
    def input_account(self, account) :
        self.element_android_uiautomator(self._et_account).send_keys(account)
Copy the code