The secret of Appium element positioning method


I believe that when you use Appium, you will encounter a problem, how to better locate a certain element in a page faster. This article is based on the fact that you are new to Appium, and the positioning of elements is still relatively vague.
Appium's positioning method is dependent on Selenium. So Selenium's positioning method is supported by Appium, plus the native positioning methods of Android and iOS. In this way, there are more than ten positioning methods, and it is also a bit particular about which one to use.

1. Types of Appium positioning methods

Currently, the positioning methods supported by Appium are as follows:

cssSelector # Selenium's most powerful positioning method, faster than xpath, but more difficult to use than xpath
linkText # All display text of link elements
partialLinkText # Part of link element display text
name # The name attribute of the element, which is currently officially removed on the mobile terminal Positioning method, use AccessibilityId instead of
tagName # Element's tag name
className # Element's class attribute
id # Element's id attribute
xpath # Positioning method is slightly weaker than css positioning method, but it is easy to use and easy to use. The disadvantage is speed slow down.
AccessibilityId # Appium is used to replace name positioning method
AndroidUIAutomator # Android test, the most powerful and fastest positioning method
iOSNsPredicateString # iOS predicate positioning method, only supports XCTest framework, needs to be greater than iOS 9.3 or above
IosUIAutomation # iOS predicate positioning method , only supports UIAutomation framework, must be greater than iOS 9.3 or below
iOSClassChain # Foreign god Mykola Mokhnach developed a positioning method similar to xpath, only supports the XCTest framework, not as good as xpath and iOSNsPredicateString
windowsAutomation # Windows application automation positioning method

In the positioning method shown above, name is discarded due to official reasons, so it will not be described here. In my understanding, tagName, linkText and partialLinkText are generally used on web pages, and rarely used on mobile terminals.

Next, let me talk about the detailed usage of positioning methods that I think are commonly used in App UI automation.

1.1 className
Use the className attribute of the element to locate, support: Android and iOS, recommended.
MobileBy. className("XCUIElementTypeButton")

1.2 id
Use the resource-id attribute of the element to locate, support: Android, only support Android 4.2 or above, recommended to use, generally speaking, using id can accurately locate, use id, the positioning information is concise, and it is not easy to make mistakes. Anyway, I haven't used it on iOS. If you have an example of using it correctly, you can share it.
MobileBy.id("package.name:id/android")

1.3 xpath
support: Android and iOS. However, since the original sound of the XCUITest framework used in iOS 10 does not support it, and the positioning speed is very slow, it is officially not recommended for everyone to use, and there are other alternative positioning methods that can be used.
Use absolute path positioning, as shown in the screenshot xpath path
MobileBy.xpath("className/className/className/className")
Use relative path positioning
MobileBy.xpath("//className")
Locate
MobileBy.xpath(" //className[index]") Locate MobileBy.xpath ("//className[@label='more information']") through the attribute
of the element ='more information'][@isVisible='1']") # Use two attributes to locate MobileBy.xpath("//className[contains(@label,'more')]") # Use some attributes to locate (most powerful)


1.4 AccessibilityId
replaces the previous name positioning method and is recommended.
On Android, the content-desc attribute of the element is mainly used. If the attribute is empty, this positioning method cannot be used.
On iOS, the label or name attribute of the element (both attributes have the same value) is mainly used for positioning. If the attribute is empty, this attribute cannot be used.
MobileBy.AccessibilityId("More information")

1.5 AndroidUIAutomator
only supports Android 4.2 or above, and supports single attribute and multiple attribute positioning of elements, which is recommended.
The following attributes of the element are supported:
index(int ​​index)
text(String text)
resourceId(String id)
className(String className)
packageName(String packageName)
description(String desc)
checked(boolean val)
clickable(boolean val)
enabled(boolean val )
longClickable(boolean val)
password(boolean val)
selected(boolean val)
instance(int val)
# For some other detailed methods (including regular expression matching), please check the methods defined by the UiSelector class in the Android source code
Example:
MobileBy.AndroidUIAutomator ("new UiSelector().text(\"Send\")") # Use a property to locate
MobileBy.AndroidUIAutomator("new UiSelector().text(\"Send\").
All attributes of elements can be used for positioning, which is very powerful and fast.

1.6 iOSNsPredicate
only supports iOS 10 or above. It supports single attribute and multiple attribute positioning of elements, and is recommended. For details, please refer to
iOSNsPredicate positioning.
MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton'") # Use one attribute to locate
MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton' AND label == 'More Information'") # Use two attributes to locate
specific iOSNsPredicate Syntax The structure can be viewed in the official documentation, or in another post of mine.

1.7 iOSClassChain
only supports iOS 10 or above. This is developed by Mykola Mokhnach of github. It can only be used in the WebDriverAgent framework to replace xpath. However, after using it for a while, I feel that it is not as flexible as xpath and iOSNsPredicate, and it should not be perfect Bar. For details on how to use it, see: iOSClassChain.
MobileBy.iOSClassChain('XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeNavigationBar[1]/XCUIElementTypeOther[1]/XCUIElementTypeButton[2]')

1.8 IosUIAutomation
only supports iOS 9.3 or below. It is the positioning method of the old iOS framework UIAutomation. This positioning type can also be positioned using iOS predicates. For details, please refer to: iOSNsPredicate MobileBy.IosUIAutomation("type == 'UIAButton'") # Use
one Attribute location
MobileBy.IosUIAutomation("type == 'UIAButton' AND label == 'more information'") # Use two attributes to locate

2. Complex positioning of elements

On the login page of Beichat parent version Android, the information of the two elements of the mobile phone number input box and the password input box are shown in the figure below.

Most of the attributes of the mobile phone number input box and the password input box are the same, and it is no longer feasible to use className and id alone. Some students may ask, doesn’t the input box have a default copy (text attribute)? Use the text attribute to locate it.
During the initial login, it is indeed possible to use this text attribute to locate, but if it is not the initial login, the mobile phone number input box remains the last logged-in mobile number, and the text attribute becomes the last logged-in mobile number, as shown in Figure 3.
In an input box like this, the element's text attribute is constantly changing. When positioning elements, we try to avoid using attributes with variable values ​​for positioning.
It can be seen that the two input boxes of mobile phone number and password basically cannot be positioned using a single attribute. If this is the case, we can use element multi-attributes for positioning. Looking at Figure 1 and Figure 2, most of the attributes of the two elements are consistent, but there are still three attributes that are different: focused, password, and instance. Combined with only two input boxes with the same attribute value, the element can be found after positioning in this way.
Positioning using AndroidUIAutomator. UiSelector does not support password attribute positioning.

# Mobile phone number input box
MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.babychat:id/edit_content\").focused(true)")
MobileBy.AndroidUIAutomator("new UiSelector().className(\" android.widget.EditText\").instance(0)")

# Password input box
MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.babychat:id/edit_content\").focused(false)")
MobileBy.AndroidUIAutomator("new UiSelector().className(\"android .widget.EditText\").instance(1)")
uses xpath for positioning, does not support using instance attribute for positioning

# Mobile phone number input box
MobileBy.xpath("//android.widget.EditText[@focused='true']")
MobileBy.xpath("//android.widget.EditText[@password='false']")

# Password input box
MobileBy.xpath("//android.widget.EditText[@focused='false']")
MobileBy.xpath("//android.widget.EditText[@password='true']")

3. Summary

Some positioning methods of Appium elements are generally as mentioned above. As long as you can locate the elements you want, which one to use depends on your personal habits. But if the attributes of the element you want to locate are more similar to other elements, you need to use two or even three attributes for positioning. Which attributes to use, you have to compare them with other attributes one by one to find out the difference Attributes, use the positioning type according to the attributes, which is more reliable.
 

Guess you like

Origin blog.csdn.net/qq_30273575/article/details/131829537