在Appium Android Uiautomator中,输入方法就是这一段代码。从这一段代码中会发现Appium基本没有做什么事情,最终调用了Uiautomator本身的setText方法。所以,接下来需要确认原生的Uiautomator是否可以支持中文输入。 经过简单的测试发现,Uiautomator不支持中文输入,然后继续Google解决方案。发现了一个uiautomator-unicode-input-helper开源项目。直接拿来尝试了中文,也发现不对。按照作者的说法是可以支持日语,但是貌似想支持中文的话,还要进行一定量的开发。
继续看看Uiautomator为什么不支持中文。然后继续找Android的源码,发现Uiautomator的所有的功能都是通过UiAutomatorBridge来完成的。在UiAutomatorBridge类中有一个成员变量类型是InteractionController。输入是通过InteractionController来实现的。具体的setText方法代码如下:
Java
public boolean More ...sendText(String text) { if (DEBUG) { Log.d(LOG_TAG, "sendText (" + text + ")"); } mUiAutomatorBridge.setOperationTime(); KeyEvent[] events = mKeyCharacterMap.getEvents(text.toCharArray()); if (events != null) { for (KeyEvent event2 : events) { // We have to change the time of an event before injecting it because // all KeyEvents returned by KeyCharacterMap.getEvents() have the same // time stamp and the system rejects too old events. Hence, it is // possible for an event to become stale before it is injected if it // takes too long to inject the preceding ones. KeyEvent event = KeyEvent.changeTimeRepeat(event2, SystemClock.uptimeMillis(), 0); if (!injectEventSync(event)) { return false; } } } return true; } |
通过这段代码,基本上就明白了,Android的Uiautomator完全没有兼容英文以外的输入法的意思。并且也没有提供直接设置属性的方法。
Appium Android Uiautomator 要支持输入中文还有很长的一段路需要走。伤心了。