您的位置:首页 > 产品设计 > UI/UE

java +UIAutomator 通过中文文本查找控件 报错问题

2015-11-23 15:52 549 查看
<span style="font-family: SimSun; font-size: 14px; background-color: rgb(255, 255, 255);">在使用eclipse来编译java+uiautomator程序时,使用text属性来查找控件报错了,提示找不到控件UiOjbectNotFoundException</span>




百度搜索到的其他示例程序都是英文属性,难道uiautomator不支持中文text控件属性吗。再次把系统语言切换成英文后,再更改text name,运行居然可以了。

但这个UIAutomator是google弄的,不可能对unicode字符支持这么基础的东西都有问题啊,可怜网上UIAutomator与此相关的资源太少,几经艰辛才知道这个跟eclipse项目的Text file encoding选项有关系。原来问题还是出在自身上。

解决方法:把项目默认的Text file encoding从GBK改成UTF-8,重新打包运行

这次真的成功了。

附上实验代码:

import android.os.RemoteException;

import com.android.uiautomator.core.UiDevice;
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 MessageTestCase extends UiAutomatorTestCase{
public void testDemo() throws UiObjectNotFoundException,RemoteException{
UiDevice device=getUiDevice();
device.pressHome();
sleep(2000);
UiObject messageIcon=new UiObject(new UiSelector().text("信息"));
assertTrue(" messageIcon not found",  messageIcon.exists());
messageIcon.click();
sleep(3000);
UiObject newMessage=new UiObject(new UiSelector().text("写短信"));
assertTrue(" newMessage not found",  newMessage.exists());
newMessage.click();
sleep(1000);
UiObject addContact=new UiObject(new UiSelector().text("收件人"));
assertTrue(" addContact not found",  addContact.exists());
addContact.click();
addContact.setText("10086");
UiObject editMessage=new UiObject(new UiSelector().text("输入文本信息"));
assertTrue(" editMessage not found", editMessage.exists());
editMessage.click();
editMessage.setText("11");
UiObject sendButton=new UiObject(new UiSelector().description("发送"));
assertTrue(" sendButton not found", sendButton.exists());
sendButton.click();
UiObject sendCard=new UiObject(new UiSelector().text("中国移动"));
sendCard.click();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: