本文通过实例来分析uiautomator的具体操作
<span style="font-size:10px;">//2015年1月15日,编写用于优物自动化的程序;用于练习和确认Uiautomator的学习情况 package test; import jp.jun_nama.test.utf7ime.helper.Utf7ImeHelper; import android.os.RemoteException; import com.android.uiautomator.core.UiObject; import com.android.uiautomator.core.UiObjectNotFoundException; import com.android.uiautomator.core.UiScrollable; import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.testrunner.UiAutomatorTestCase; public class Runner extends UiAutomatorTestCase{ public void test() throws UiObjectNotFoundException , RemoteException{ getUiDevice().wakeUp();//唤醒APP findAndRunApp(); // try{ //进首页后延时3s // Thread.sleep(3000); // }catch(InterruptedException e1){ // e1.printStackTrace(); // } loginApp("************","*******"); exitToMainWindow(); loginoutApp(); loginApp("**********","*******"); modifyPassword( "*******","*********" ); exitToMainWindow(); loginApp("*********","*******"); feedback("元气少女鹿都不回我的。。。鹿是一种温顺可爱的动物,今人形容女孩子温顺可爱,会说这女孩像一只温顺的小鹿;"); selectcity("广州"); selectbrand("唯品会"); attention(); cancelattention(); loginoutApp(); exitToMainWindow(); } //寻找和打开APP private void findAndRunApp() throws UiObjectNotFoundException{ getUiDevice().pressHome(); UiScrollable appview = new UiScrollable (new UiSelector().scrollable(true)); appview.setAsHorizontalList(); UiObject openapp = appview.getChildByText(new UiSelector().<span style="color:#3333FF;">className(android.widget.TextView.class.getName()) , "优物")</span>; openapp.clickAndWaitForNewWindow(); System.out.println("find youwu_App"); sleep(5000);//睡眠5s // System.out.println("sleep success"); } //登陆APP private void loginApp(String userid , String userkey) throws UiObjectNotFoundException{ <span style="color:#3333FF;">//根据</span></span><span style="font-size:10px;"><span style="color:#3333FF;">resourceId来获取对象</span> </span><span style="font-size:10px;"> UiObject mybutton = new UiObject(new UiSelector().<span style="color:#3333FF;">resourceId</span>("com.stunner.vipshop:id/menu_3"));//点击首页-我 mybutton.clickAndWaitForNewWindow(); UiObject loginbutton =new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/login"));//点击我的尤物页-登陆按钮 loginbutton.clickAndWaitForNewWindow(); UiObject clearbutton =new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/btn_clean"));//点击输入账号的清除叉叉 clearbutton.clickAndWaitForNewWindow(); UiObject username = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/username"));//点击输入用户名 username.setText(userid); UiObject password = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/password"));//点击输入密码 password.setText(userkey); UiObject login = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/login_button"));//点击登陆按钮 login.clickAndWaitForNewWindow(); UiObject FirstPage = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_1"));//回到首页 FirstPage.clickAndWaitForNewWindow(); System.out.println("login success"); sleep(5000);//休眠5s // UiObject phonetxt = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/txt_phone")); // if(phonetxt.exists()) <span style="color:#3366FF;">//判断对象是否存在,然后进行相关操作。</span> // System.out.println("login success"); } //退出登陆 private void loginoutApp() throws UiObjectNotFoundException{ UiObject mybutton = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_3"));//点击首页-我 mybutton.clickAndWaitForNewWindow(); UiObject setting = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/layout_main_setting"));//点击设置 setting.clickAndWaitForNewWindow(); UiObject exit = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/txt_logout"));//点击-退出登录 exit.clickAndWaitForNewWindow(); sleep(2000); getUiDevice().pressBack();//点击-返回 UiObject FirstPage = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_1"));//回到首页 FirstPage.clickAndWaitForNewWindow(); System.out.println("exit success"); } //修改密码 private void modifyPassword(String oldkey , String newkey ) throws UiObjectNotFoundException{ UiObject mybutton = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_3"));//点击首页-我 mybutton.clickAndWaitForNewWindow(); UiObject setting = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/layout_main_setting"));//点击设置 setting.clickAndWaitForNewWindow(); UiObject modify = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/layout_ps")) ;//点击-修改密码 modify.clickAndWaitForNewWindow(); UiObject in_oldkey = new UiObject (new UiSelector().resourceId("com.stunner.vipshop:id/password"));//读参写入密码 in_oldkey.setText(oldkey); UiObject in_newkey = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/password_change"));//输入新密码 in_newkey.setText(newkey); UiObject confirm = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/confir_button"));//点击确认 confirm.clickAndWaitForNewWindow(); sleep(3000); } //意见反馈 private void feedback(String tucao) throws UiObjectNotFoundException{ UiObject mybutton = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_3"));//点击首页-我 mybutton.clickAndWaitForNewWindow(); UiObject feedback_button = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/layout_feedback"));//点击吐槽 feedback_button.clickAndWaitForNewWindow(); UiObject fd_content =new UiObject(new UiSelector().className("android.widget.LinearLayout").index(2).className("android.widget.EditText").index(0));//输入中文文字 fd_content.setText(Utf7ImeHelper.e(tucao)); sleep(6000); UiObject tijiao =new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/title_btn_right"));//点击提交标签 tijiao.clickAndWaitForNewWindow(); UiObject FirstPage = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_1"));//回到首页 FirstPage.clickAndWaitForNewWindow(); sleep(2000); } //选择城市 private void selectcity(String city) throws UiObjectNotFoundException{ UiObject city_button = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/txt_city"));//点击首页-选择城市 city_button.clickAndWaitForNewWindow(); UiObject go_search = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/go_search"));//点击-搜索框 go_search.clickAndWaitForNewWindow(); UiObject edt_search = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/edt_search"));//输入-城市 edt_search.setText(Utf7ImeHelper.e(city)); sleep(4000); UiObject text = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/text"));//点击搜索出来的城市,若无此城市,testcase直接不运行,fail text.clickAndWaitForNewWindow(); sleep(2000); System.out.println("select city success!"); } //选择品牌 private void selectbrand(String brand) throws UiObjectNotFoundException{ UiObject btn_search = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/btn_search"));//点击选择-品牌标签 btn_search.clickAndWaitForNewWindow(); UiObject go_search = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/go_search"));//点击-品牌搜索框 go_search.clickAndWaitForNewWindow(); UiObject edt_search = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/edt_search"));//输入-搜索的品牌 edt_search.setText(Utf7ImeHelper.e(brand)); sleep(4000); UiObject info_layout = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/info_layout"));//选择你搜索到品牌的第一个 info_layout.clickAndWaitForNewWindow(); sleep(2000); System.out.println("select brand success!"); } //关注品牌 private void attention() throws UiObjectNotFoundException, RemoteException{ UiObject attention_button = new UiObject(new UiSelector().className("android.view.View").index(1)); attention_button.click(); sleep(2000); System.out.println("attention brand success!"); exitToMainWindow(); } //取消关注 private void cancelattention() throws UiObjectNotFoundException{ UiObject mybutton = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_3"));//点击首页-我 mybutton.clickAndWaitForNewWindow(); UiObject brandnuma = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/layout_brandnuma"));//点击-我关注的品牌 brandnuma.clickAndWaitForNewWindow(); UiObject titlebar = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/titlebar"));//向左滑动,出现-取消关注的标签 titlebar.swipeLeft(5); UiObject button1 = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/button1"));//点击-取消关注 button1.click(); getUiDevice().pressBack(); UiObject FirstPage = new UiObject(new UiSelector().resourceId("com.stunner.vipshop:id/menu_1"));//回到首页 FirstPage.clickAndWaitForNewWindow(); System.out.println("cancel attention success!"); } //退出到手机第一屏 private void exitToMainWindow() throws UiObjectNotFoundException, RemoteException{ // android.os.Process.killProcess(android.os.Process.myPid()) ; //退出uiautomator的进程 // System.out.println("do exit"); getUiDevice().pressRecentApps(); sleep(5000); UiObject CloseApp = new UiObject(new UiSelector().className("android.widget.ScrollView").index(1)); CloseApp.swipeRight(20); System.out.println("Close App success!!!"); getUiDevice().pressBack(); findAndRunApp(); } }</span>