缘由

如题,对于开发人员使用 windowmanager 自定义的 toast 消息,无法获取

解决思路

在小伙伴@erickyang 的提示下,我又去看了一下 appium 下 ui2.0 服务获取 toast 的源码

private class Listener extends Thread{

    private long previousTime = currentTimeMillis();

    public void run() {

        while (true) {
            AccessibilityEvent accessibilityEvent = null;
            toastMessages = init();

            //return true if the AccessibilityEvent type is NOTIFICATION type
            UiAutomation.AccessibilityEventFilter eventFilter = new UiAutomation.AccessibilityEventFilter() {
                @Override
                public boolean accept(AccessibilityEvent event) {
                    return event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
                }
            };
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    // Not performing any event.
                }
            };

            try {
                //wait for AccessibilityEvent filter
                accessibilityEvent = UiAutomatorBridge.getInstance().getUiAutomation()
                        .executeAndWaitForEvent(runnable /*executable event*/, eventFilter /* event to filter*/, 500 /*time out in ms*/);
            } catch (Exception ignore) {}

            if (accessibilityEvent != null) {
                toastMessages = accessibilityEvent.getText();
                previousTime = currentTimeMillis();
                Logger.info("toastMessages getText:" + toastMessages);
                GetToastMessage.toastMessage = toastMessages.toString();// add by chenlg
            }
            if (stopLooping) {
                break;
            }
        }
    }

    public List<CharSequence> init(){
        if (currentTimeMillis() - previousTime  > TOAST_CLEAR_TIMEOUT) {
            return new ArrayList<CharSequence>();
        }
        return toastMessages;
    }
}

从这边可以看到是捕获的 AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED 事件,
然后和开发的同学沟通了一下,windowmanager 在弹窗的时候,发送的是什么样的事件?是否可以捕获?
开发的同学说他们可以主动发这个事件 AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED

主动发送事件的方法

AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
event.getText().add("hellowindowManager");
view.onPopulateAccessibilityEvent(event);
view.sendAccessibilityEventUnchecked(event);

拓展

以后对于不能捕获的 UI 无素,可以试试这种方法,捕获相关的信息,方便做自动化测试


↙↙↙阅读原文可查看相关链接,并与作者交流