[android]uiautomator中文輸入解決方案

大搜車-自娛發表於2014-02-25
UiAutomator不支援中文輸入。

通過設定中文輸入法為預設,UiObject.setText("pinyin ") 的方式,可以實現中文輸入,但是隻能輸入一些固定的片語。

github上發現了一個 utf7ime 的好東西,可以實現中文輸入,英文輸入,中英文混合輸入。簡單來說,支援輸入任何unicode編碼的字元。
原理是:UiObject.setText( String) 只能接受ASCII碼,整個過程是輸入的unicode編碼的字串decode成ASCIl碼,setText接受這些ASCll碼再通過utf7ime這個輸入法encode成unicode編碼的字串輸出。
前置條件:手機裝入此輸入法並將之設為預設輸入法

簡單說一說整個過程,

打包下載 https://github.com/sumio/uiautomator-unicode-input-helper
匯入其中的Utf7Ime , 生成apk並安裝設定成預設輸入法
把 helper-library 裡面的Utf7ImeHelper.java匯入自己的公用方法庫,用於把字串decode成ASCII碼
最後生成指令碼是這樣的:

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.sn.test.testUtil.AppAPI;
import com.sn.test.testUtil.Utf7ImeHelper;

public class TestInput extends UiAutomatorTestCase {
private String caseName = this.getClass().getSimpleName();
private String caseDetail = "";
private String description = "";
private boolean result = false;

protected void setUp() throws Exception {
super.setUp();
AppAPI.setUp(caseName);
}

public void testDemo() throws UiObjectNotFoundException, RemoteException {

new UiObject(new UiSelector().className("android.widget.TextView")
.text("資訊")).clickAndWaitForNewWindow();
sleep(1000);

new UiObject(new UiSelector().className("android.widget.RadioButton")
.text("新資訊")).clickAndWaitForNewWindow();
sleep(1000);

new UiObject(new UiSelector().className("android.widget.EditText"))
.setText(Utf7ImeHelper.e("test test yingwen 中文一起輸入"));
sleep(1000);

下一步考慮如何將這個功能整合到指令碼錄製工具中去。

文章轉載於:http://testerhome.com/topics/408
有空驗證的同學,驗證完後記得在部落格上留言驗證結果。

相關文章