之前轉過一個教程(http://blog.csdn.net/vivian_ljx/article/details/47613957)說UIAutomator不能通過中文文本查找控件
其實是可以的,問題出在代碼檔案的編碼格式上
解決方法:
選擇UTF-8
例子:
進入“設定”菜單
package com.ljx.test;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class test extends UiAutomatorTestCase{
public void testDemo() throws UiObjectNotFoundException {
getUiDevice().pressHome();
// 進入設定菜單
UiObject settingApp = new UiObject(new UiSelector().text("設定"));
settingApp.click();
//休眠3秒
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
運作結果如下: