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

时间:2019-01-18
本文章向大家介绍java +UIAutomator 通过中文文本查找控件 报错问题,主要包括java +UIAutomator 通过中文文本查找控件 报错问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 

<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();

		
	}

}