UIAutomator2.0详解(UIDevice篇----触屏操作1)
UIDevice中有20個方法,是關于觸屏操作的,占方法總數的三分之一。數量看似很多,仔細分析一下,也就幾類。
(1)功能鍵型,7個,HOME,RECENT,BACK,DELETE,ENTER,MENU,SEARCH
(2)開啟固定界面型,2個,Notification,Quick Setting
(3)按鍵型,7個,包括方向型(上下左右中),以及KeyCode事件型
(4)單擊、拖拽型,4個
由于不想將博文變得冗長,該部分我們將分為三個章節,本文將講述前兩項,即功能鍵和固定界面。
先列一下,需要調用的接口方法。
功能鍵型(具體按鍵可對應下圖,未給出回車鍵):
(1)public boolean pressSearch() ,點擊查找功能鍵
(2)public boolean pressBack() ,點擊后退鍵
(3)public boolean pressDelete() ,點擊刪除鍵
(4)public boolean pressEnter() ,點擊回車鍵
(5)public boolean pressHome(),點擊Home鍵
(6)public boolean pressMenu(),點擊菜單鍵
(7)public boolean pressRecentApps(),點擊在運行的APP鍵
固定界面型:
(1)public boolean openNotification(),展開通知欄
(2)public boolean openQuickSettings(),展開快速設置欄
需要指出的是,由于各廠家對于功能鍵的顯示方式不同,導致某些功能鍵不存在,或者滿足一定條件才顯示。因此,本文所提供的樣例,在不同手機上的測試效果可能存在差異。
注:關于Search鍵的顯示,需要在EditView中設置兩項屬性
測試案例代碼如下:
package com.breakloop.u2demo.uidevice;import android.os.RemoteException; import android.support.test.InstrumentationRegistry; import android.support.test.uiautomator.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.Until; import android.util.Log;import com.breakloop.u2demo.common.Utils;import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4;/*** Created by user on 2017/11/3.*/public class FingerTest extends UIDeviceTest {@Testpublic void FunctionKeyTest(){String packageName="com.breakloop.test";String activityName=".MainActivity";boolean result;int timeOut=5000;Log.i(TAG, "Start Test");result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(1)Open APPUtils.startAPP(mDevice,packageName,activityName);Log.i(TAG, "open APP");result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(2)Click Edit View1, then Search Button appearUiObject2 mInput1=mDevice.findObject(By.res("com.breakloop.test:id/et_input1"));if(mInput1==null){Log.i(TAG, "Do not find Input1");}else {if (mInput1.isEnabled()) {mInput1.click();result = mDevice.waitForWindowUpdate(null, timeOut);Log.i(TAG, "wait For Window Update, result = " + result);mInput1.setText("SSS");result=mDevice.pressSearch();Log.i(TAG, "Press Search Button, result = "+result);mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);}}//(3)Click Edit View2, then Menu Button appearUiObject2 mInput2=mDevice.findObject(By.res("com.breakloop.test:id/et_input2"));if(mInput2==null){Log.i(TAG, "Do not find Input2");}else {if(mInput2.isEnabled()){mInput2.click();result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);mInput2.setText("AAA");result=mDevice.pressEnter();Log.i(TAG, "Press Enter Button, result = "+result);mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);result=mDevice.pressMenu();Log.i(TAG, "Press Menu Button, result = "+result);mDevice.waitForWindowUpdate(packageName,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);}}//(4)Change Activity and Press Back ButtonUiObject2 button=mDevice.findObject(By.res("com.breakloop.test:id/btn_goto2"));if(button==null){Log.i(TAG, "Do not find Button");}else {if (button.isEnabled()) {button.click();mDevice.waitForWindowUpdate(packageName, timeOut);result=mDevice.pressBack();Log.i(TAG, "Press Back Button, result = "+result);mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);}}//(5)Press Home Buttonresult=mDevice.pressHome();Log.i(TAG, "Press Home Button, result = "+result);mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(6)Press Recent Buttontry {result=mDevice.pressRecentApps();Log.i(TAG, "Press Recet Apps Button, result = "+result);result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);} catch (RemoteException e) {e.printStackTrace();}//(7)Press Deleteresult=mDevice.pressDelete();Log.i(TAG, "Press Delete Button, result = "+result);result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(8)Press Home Buttonresult=mDevice.pressHome();Log.i(TAG, "Press Home Button, result = "+result);result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(9)Open Notificationresult=mDevice.openNotification();Log.i(TAG, "Press Notification Button, result = "+result);result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(10)Press Home Buttonresult=mDevice.pressHome();Log.i(TAG, "Press Home Button, result = "+result);result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(11)Open Quick Settingresult=mDevice.openQuickSettings();Log.i(TAG, "Press Quick Setting Button, result = "+result);result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);//(12)Press Home Buttonresult=mDevice.pressHome();Log.i(TAG, "Press Home Button, result = "+result);result=mDevice.waitForWindowUpdate(null,timeOut);Log.i(TAG, "wait For Window Update, result = "+result);Log.i(TAG, "End Test");} }執行結果如下:
11-03 16:13:24.876 I/com.breakloop.u2demo.uidevice.FingerTest: Start Test 11-03 16:13:25.015 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:25.470 I/com.breakloop.u2demo.uidevice.FingerTest: open APP 11-03 16:13:25.516 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:32.610 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:33.655 I/com.breakloop.u2demo.uidevice.FingerTest: Press Search Button, result = true 11-03 16:13:34.043 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:36.584 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:37.639 I/com.breakloop.u2demo.uidevice.FingerTest: Press Enter Button, result = true 11-03 16:13:37.680 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:39.600 I/com.breakloop.u2demo.uidevice.FingerTest: Press Menu Button, result = true 11-03 16:13:47.575 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:50.646 I/com.breakloop.u2demo.uidevice.FingerTest: Press Back Button, result = true 11-03 16:13:51.095 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:52.650 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true 11-03 16:13:52.698 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:54.603 I/com.breakloop.u2demo.uidevice.FingerTest: Press Recet Apps Button, result = true 11-03 16:13:54.657 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:56.620 I/com.breakloop.u2demo.uidevice.FingerTest: Press Delete Button, result = true 11-03 16:13:56.661 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:13:59.647 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true 11-03 16:13:59.648 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:14:01.619 I/com.breakloop.u2demo.uidevice.FingerTest: Press Notification Button, result = true 11-03 16:14:01.629 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:14:03.666 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true 11-03 16:14:03.670 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:14:05.633 I/com.breakloop.u2demo.uidevice.FingerTest: Press Quick Setting Button, result = true 11-03 16:14:05.642 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:14:07.752 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true 11-03 16:14:07.772 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true 11-03 16:14:07.772 I/com.breakloop.u2demo.uidevice.FingerTest: End Test執行效果如下圖:
比對執行效果和LOG,發現pressMenu觸發成功,但并未顯示。原因未查到。
另,Android7.0基本上導航鍵上已經沒有菜單鍵了。
使用測試手機為華為Mate8,Android版本為7.0。
總結
以上是生活随笔為你收集整理的UIAutomator2.0详解(UIDevice篇----触屏操作1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [附源码]java毕业设计医院药房管理系
- 下一篇: UseCase中include和exte