本文通过实例来分析uiautomator的具体操作
<span style="font-size:10px;"><span style="color:#3366FF;">//所有UI自动测试都要继承UiAutomatorTestCase </span>
public class MessageTestCase extends UiAutomatorTestCase {
public void testDemo() throws UiObjectNotFoundException{
<span style="color:#3366FF;">//获取UiDevice对象 </span>
UiDevice device = getUiDevice();
<span style="color:#3366FF;">//点击home键操作 </span>
device.pressHome();
<span style="color:#3333FF;">//如截图中提到的,我们使用content-desc属性Apps选择所有应用列表 </span>
UiObject appsTab = new UiObject(new UiSelector().description("Apps"));
appsTab.click(); <span style="color:#3333FF;">//此处使用clickAndWaitForNewWindow();也可以</span>
<span style="color:#3333FF;">//因为短信界面在第二个列表页面,所以我们可以先滑动一下</span>
<span style="color:#3333FF;">//通过scrollable属性来选定滑动view </span>
UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
appViews.setAsHorizontalList();
appViews.scrollForward();
<span style="color:#3333FF;">//通过类名和Text找到短信应用图标,Text获取通过uiautomatorviewer </span>
UiObject messageApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Messaging");
<span style="color:#3366FF;">//点击并等待打开短信应用 </span>
messageApp.clickAndWaitForNewWindow();
<span style="color:#3366FF;">//选择新建短信 </span>
UiObject newMessage = new UiObject(new UiSelector().description("New message"));
newMessage.click();
<span style="color:#3366FF;">//选择编辑信息 </span>
UiObject editMessage = new UiObject(new UiSelector().text("Type text message"));
editMessage.click();
editMessage.setText("hello world!");
}
} </span>