当前位置:网站首页>Uiobject2 of uiautomator2 common classes
Uiobject2 of uiautomator2 common classes
2022-07-26 19:40:00 【Xiao Liu xuezhuo】
UiAutomator The classes involved are : UiObject、UiObject2、UiDevice、UiWatcher、BySelector、AccessibilityNodeInfo、Gestures、GestureController、Instrumentation
One 、UiObject and UiObject2
UiObject2 Representing one UI Control . It is bound to a specific view instance , If the underlying view object is destroyed , It may become obsolete . therefore , If UI Significant changes have taken place , May need to call UiDevice#findObject(BySelector) To get new UiObject2 example .
UiObject Representing one UI Control . It does not bind directly to the view in any way as an object reference . UiObject Contains help at run time according to {@link UiSelector} Attribute locates the information that matches the view . Once you create a UiObject Example , It can be reused in different views that match selector criteria .
UiObject yes UI Automator Important classes in the early days of the testing framework ,UiObject2 Is its improved version .
difference : Reference article ”UiObject And UiObject2 Trigger UiWatcher Code timing “
UI Automator Testing frameworks are most commonly used UiObject And UiObject2, The objects generated by these two classes , All represent the controls that meet the specified conditions , When the control is not found , Will trigger UiDevice All registered UiWatcher object , We can do it in UiWatcher In the implementation class of , Write the processing logic when the control is not found , For example, a control is not found , Maybe the pop-up system dialog box blocks the controls we need to find , At this time, it can be in UiWatcher In the implementation class of , Write close this close system popup 、 Or close the business logic code of a business pop-up window .
UiObject、UiObject2 Trigger UiWatcher The timing of the code is different , Let's spoiler first. They trigger each other UiWatcher The timing of the code
1、UiObject When executing methods that manipulate controls , Will trigger UiWatcher Code for . For example, call UiObject Of click() When the method is used , Can trigger UiWatcher Code for , While using UiDevice Of findObject(UiSelector) When looking for controls , It doesn't lead to UiWatcher Code triggering
2、UiObject2, It is UiDevice Of findObject(BySelector) Method will trigger UiWatcher Code for , That is, when the search control is executed , It will trigger UiWatcher Code for
We can take a look UiObject2 Source code . So let's see first UiObject2 Which dependent classes are used inside the class :
UiDevice: Express UI Operating equipment , It encapsulates the device information 、 Get device controls and device gesture operation functions ;
BySelector: lookup UiDevice The condition collection object of the control on the device , Through it, you can find the target control on the device ;
AccessibilityNodeInfo: Accessible node information , All controls visible on the device screen ( Include View And layout ) Can be abstracted into nodes , And nodes can be nested in nodes to form a control tree ( Node tree ).AccessibilityNodeInfo Contains node information , such as : Control id, text Width and width 、 Parent node 、 Child node information , Can I click , Check it or not , And encapsulates ui Operation method . All in all UiObject2 Some information about the control properties of the object is from AccessibilityNodeInfo Obtained from . You can UiObject2 Comprehend AccessibilityNodeInfo An agent of
Gestures: Gesture object ,ui operation , For example, click on 、 Sliding these operations can be abstracted into a gesture ;
GestureController: Used to perform gestures .
Let's make a concrete analysis UiObject2 Specific source code
1、 Constructors
You can see ,UiObject2 Is a private function in the package , It means that we can't pass directly new The way to create a UiObject2 object . But through UiDevice#findObject(BySelector) The way to create UiObject2 object . namely UiDevice By finder object BySelector To find qualified UI Control .
/** Package-private constructor. Used by {@link UiDevice#findObject(BySelector)}. */
UiObject2(UiDevice device, BySelector selector, AccessibilityNodeInfo cachedNode) {
mDevice = device;
mSelector = selector;
mCachedNode = cachedNode;
mGestures = Gestures.getInstance(device);
mGestureController = GestureController.getInstance(device);
mDisplayMetrics = mDevice.getInstrumentation().getContext().getResources()
.getDisplayMetrics();
}2、getAccessibilityNodeInfo() Get controls Abreast of the times Node information
First ,UiObject2 The control information represented is saved in mCachedNode Attribute , It's a AccessibilityNodeInfo object .
1、 In this function, we first judge mCachedNode Whether the object is null, If null It means that this node has been recycled , Throw an exception ;
2、 And then call getDevice().waitForIdle() Wait for the device to be idle , That is, the current mobile page elements have not changed ;
3、 call mCachedNode.refresh() To refresh the state of the control to get the latest information on the control , such as textview The text on the control may change , call refreash() Get the latest text after ;
4、 If refresh() return false, Represents that the control is obsolete , It indicates that the control is no longer in the control tree , This control should be recycled ;
5、refresh() return false, Then the call is bound in UiObject2 Upper UiWatcher Of checkForCondition() Handle exceptions when the control cannot be found ; etc. checkForCondition() after , Check again mCachedNode Whether there is , If it doesn't exist yet , Direct error reporting ;
6、refresh() return true, Represents that this control is found in the device control tree , And updated the control information ;
7、 Go straight back to mCachedNode.
private AccessibilityNodeInfo mCachedNode;
/**
* Returns an up-to-date {@link AccessibilityNodeInfo} corresponding to the {@link android.view.View} that
* this object represents.
*/
private AccessibilityNodeInfo getAccessibilityNodeInfo() {
if (mCachedNode == null) {
throw new IllegalStateException("This object has already been recycled");
}
getDevice().waitForIdle();
if (!mCachedNode.refresh()) {
getDevice().runWatchers();
if (!mCachedNode.refresh()) {
throw new StaleObjectException();
}
}
return mCachedNode;
}
3、 Get parent control getParent()
Internal calls are too complicated , Do not understand . Regardless of the , Just get the parent control of the current control .
/** Returns this object's parent, or null if it has no parent. */
public UiObject2 getParent() {
AccessibilityNodeInfo parent = getAccessibilityNodeInfo().getParent();
return parent != null ? new UiObject2(getDevice(), mSelector, parent) : null;
}4、 Get the number of child controls
First, get the latest information about updating the current control , That's one of them mChildNodeIds Will update .mChildNodeIds yes AccessibilityNodeInfo An array type object in , Save the child node information of the current node .
/** Returns the number of child elements directly under this object. */
public int getChildCount() {
return getAccessibilityNodeInfo().getChildCount();
}
/**
* Gets the number of children.
*
* @return The child count.
*/
public int getChildCount() {
return mChildNodeIds == null ? 0 : mChildNodeIds.size();
}
5、 Get the current control internal eligible Of One or all Child controls
/**
* Searches all elements under this object and returns the first object to match the criteria,
* or null if no matching objects are found.
*/
public UiObject2 findObject(BySelector selector) {
AccessibilityNodeInfo node =
ByMatcher.findMatch(getDevice(), selector, getAccessibilityNodeInfo());
return node != null ? new UiObject2(getDevice(), selector, node) : null;
}
/** Searches all elements under this object and returns all objects that match the criteria. */
public List<UiObject2> findObjects(BySelector selector) {
List<UiObject2> ret = new ArrayList<UiObject2>();
for (AccessibilityNodeInfo node :
ByMatcher.findMatches(getDevice(), selector, getAccessibilityNodeInfo())) {
ret.add(new UiObject2(getDevice(), selector, node));
}
return ret;
}6、 Get controls Visible area and Including margins The visible area of
/** Returns the visible bounds of this object in screen coordinates. */
public Rect getVisibleBounds() {
return getVisibleBounds(getAccessibilityNodeInfo());
}
/** Returns the visible bounds of this object with the margins removed. */
private Rect getVisibleBoundsForGestures() {
Rect ret = getVisibleBounds();
ret.left = ret.left + mMarginLeft;
ret.top = ret.top + mMarginTop;
ret.right = ret.right - mMarginRight;
ret.bottom = ret.bottom - mMarginBottom;
return ret;
}7、 Get the class name of the control 、 Description information 、 App package name 、 resources id
You can find , In fact, all this information is stored in UiObject2 The object holds AccessibilityNodeInfo Property object .
/**
* Returns the class name of the underlying {@link android.view.View} represented by this
* object.
*/
public String getClassName() {
CharSequence chars = getAccessibilityNodeInfo().getClassName();
return chars != null ? chars.toString() : null;
}
/** Returns the content description for this object. */
public String getContentDescription() {
CharSequence chars = getAccessibilityNodeInfo().getContentDescription();
return chars != null ? chars.toString() : null;
}
/** Returns the package name of the app that this object belongs to. */
public String getApplicationPackage() {
CharSequence chars = getAccessibilityNodeInfo().getPackageName();
return chars != null ? chars.toString() : null;
}
/** Returns the fully qualified resource name for this object's id. */
public String getResourceName() {
CharSequence chars = getAccessibilityNodeInfo().getViewIdResourceName();
return chars != null ? chars.toString() : null;
}8、UI gestures
You can see the execution on the control UI operation , Need to use GestureController Gesture control object . This object is UiObject2 Is initialized in the constructor of . Concrete UI action , For example, click on 、 Sliding is abstract Gestures class .
How to realize the plan? Write a separate article summary .
/** Clicks on this object. */
public void click() {
mGestureController.performGesture(mGestures.click(getVisibleCenter()));
}/** Package-private constructor. Used by {@link UiDevice#findObject(BySelector)}. */
UiObject2(UiDevice device, BySelector selector, AccessibilityNodeInfo cachedNode) {
mDevice = device;
mSelector = selector;
mCachedNode = cachedNode;
mGestures = Gestures.getInstance(device);
mGestureController = GestureController.getInstance(device);
mDisplayMetrics = mDevice.getInstrumentation().getContext().getResources()
.getDisplayMetrics();
}边栏推荐
- What do indicators and labels do
- 通过源码深度分析线程池中Worker线程的执行流程
- UIAutomator2常用类之UiObject2
- Redis introduction
- 安全测试与功能测试、渗透测试你知道有什么区别吗?
- 线性代数第3章向量
- 什么是服务器集群?海外服务器集群的优势?
- Customer cases | focus on process experience to help bank enterprise app iteration
- The passwords are consistent, and the total display is as shown in the figure below
- Network protocol: tcp/ip protocol
猜你喜欢

J3:Redis主从复制

这22个绘图(可视化)方法很重要,值得收藏!

Volatile keyword of JVM memory model

canvas概述
![Design of intelligent weighing system based on Huawei cloud IOT (STM32) [II] there is information at the end](/img/55/ca86fd1a53eb61efc70fead08ff0ad.png)
Design of intelligent weighing system based on Huawei cloud IOT (STM32) [II] there is information at the end

MySQL tutorial: MySQL database learning classic (from getting started to mastering)

DDL,DQL,DML语句

How to write the test case of mobile app? What are the mobile app test points?

Advantages of advanced anti DDoS IP in Hong Kong and which industries are suitable for use

聊聊如何用 Redis 实现分布式锁?
随机推荐
微信小程序插件--wxml-to-canvas(生成图片)
Aof & RDB of J2 redis
客户案例 | 聚焦流程体验,助银行企业APP迭代
C#中关闭窗体的四种方法
Detailed tutorial on installing redis on Linux
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
Without Lin benjian, there would be no TSMC today!
配置服务器环境
Weekend highlights review | establishment of digital RMB industry alliance; China Mobile announced that hefeixin will stop its service
torch.unsqueeze() squeeze() expand() repeat()用法及比较
Chapter 9 practical modeling technology
canvas 图形
JS中的 作用域
Volatile keyword of JVM memory model
Wechat applet plug-in -- wxml to canvas (generate pictures)
PADS画2.54mm排针
Network protocol: tcp/ip protocol
conda+pytorch环境教程
Description of MDM separation of powers and classification and grading authority
基于ABP实现DDD--领域逻辑和应用逻辑