uiautomator 獲取控件,點擊的原理


首先,找控件然后點擊:

new UiObject(By.selector(“test”)).click();

 

第一步:調用getQueryController

this.getQueryController().findAccessibilityNodeInfo(this.mUiSelector);

 

第二步:獲取getRootInActiveWindow,三層調用,最后通過mUiAutomation獲取

AccessibilityNodeInfo rootNode = this.getRootNode();
this.mUiAutomatorBridge.getRootInActiveWindow();
this.mUiAutomation.getRootInActiveWindow();

 

第三步:selector和rootNode算法對比找到所需要的object

this.translateCompoundSelector(uiSelector, rootNode, isCounting);

 

第四步:object click

public boolean click() throws UiObjectNotFoundException {
    Tracer.trace(new Object[0]);
    AccessibilityNodeInfo node = this.findAccessibilityNodeInfo(this.mConfig.getWaitForSelectorTimeout());
    if(node == null) {
        throw new UiObjectNotFoundException(this.mUiSelector.toString());
    } else {
        Rect rect = this.getVisibleBounds(node);
        return this.getInteractionController().clickAndSync(rect.centerX(), rect.centerY(), this.mConfig.getActionAcknowledgmentTimeout());
    }
}

 

第五步:調用InteractionController

private Runnable clickRunnable(final int x, final int y) {
    return new Runnable() {
        public void run() {
            if(InteractionController.this.touchDown(x, y)) {
                SystemClock.sleep(100L);
                InteractionController.this.touchUp(x, y);
            }

        }
    };
}

 

第六步:轉換成event,最后還是uiautomation 注入事件

private boolean touchUp(int x, int y) {
    if(DEBUG) {
        Log.d(LOG_TAG, "touchUp (" + x + ", " + y + ")");
    }

    long eventTime = SystemClock.uptimeMillis();
    MotionEvent event = MotionEvent.obtain(this.mDownTime, eventTime, 1, (float)x, (float)y, 0);
    event.setSource(4098);
    this.mDownTime = 0L;
    return this.injectEventSync(event);
}
private boolean injectEventSync(InputEvent event) {
    return this.mUiAutomatorBridge.injectInputEvent(event, true);
}
public boolean injectInputEvent(InputEvent event, boolean sync) {
    return this.mUiAutomation.injectInputEvent(event, sync);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM