UiAutomator執行自動化測試中,爲了對某個應用執行相關功能點的測試工作。那麼在 protected void setUp() throws Exception {}函數中,進行一些測試前的準備工作;在 protected void tearDown() throws Exception {}函數中,進行釋放一些資源。
例如:
setUp() throws Exception {}函數
@Override
protected void setUp() throws Exception {
super.setUp();
keyBoard =new KeyBoard(device);
pubfun= new PublicAppFunc(device);
//初始化ToastListener
assistant.initToastListener();
if(!device.isScreenOn()) device.wakeUp();//true if the screen is ON **測試前需要點亮屏幕**
if(Config.Model.equals("E700")){
pubfun.clickAppslist();
Utils.delayMs(2000);
}
if(!new UiObject(new UiSelector().text(Config.testAppName)).waitForExists(1000))
{
assistant.exitApp();
assistant.longPressHome(1);
}
//**測試前啓動指定的測試應用**
assistant.getUiObjectByInfo(InfoType.TEXT,Config.testAppName).clickAndWaitForNewWindow();
}
很顯然在測試前,啓動測試應用方法:
assistant.getUiObjectByInfo(InfoType.TEXT,Config.testAppName).clickAndWaitForNewWindow();
例如:
tearDown() throws Exception {}函數
@Override
protected void tearDown() throws Exception {
super.tearDown();
assistant.exitApp();
}
在執行完一個測試用例後,退出指定測試應用方法:
assistant.exitApp();函數
public void exitApp(){
exitApp(Config.testAppName);
}
具體實現:
//完成退出測試APP功能
public void exitApp(String appName){
try {
mdevice.pressRecentApps();
Utils.delayMs(2000);
UiObject appLaunch = mdevice.findObject(new UiSelector().text(appName));
do{
appLaunch.waitForExists(2000);
if(appLaunch.exists()){
appLaunch.swipeLeft(5);
}
}while(appLaunch.exists());
Utils.delayMs(2000);
mdevice.pressHome();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
Log.e("test", "UiObjectNotFoundException");
} catch (RemoteException e) {
e.printStackTrace();
Log.e("test", "RemoteException");
}
}