[uiautomator篇] UiWatcher的使用

  1 //package com.softwinner.pad.mark3d;
  2 package com.softwinner.performance.benchmark.mark3d;
  3 
  4 import android.content.Context;
  5 import android.content.Intent;
  6 import android.os.RemoteException;
  7 import android.support.test.InstrumentationRegistry;
  8 import android.support.test.uiautomator.By;
  9 import android.support.test.uiautomator.UiDevice;
 10 import android.support.test.uiautomator.UiObject;
 11 import android.support.test.uiautomator.UiSelector;
 12 import android.support.test.uiautomator.UiWatcher;
 13 import android.support.test.uiautomator.Until;
 14 import android.util.Log;
 15 
 16 import com.softwinner.performance.filecopyspeed.TestBase;
 17 
 18 import org.junit.After;
 19 import org.junit.Before;
 20 import org.junit.Test;
 21 
 22 import static com.softwinner.performance.benchmark.mark3d.UiAssistant.LAUNCH_TIMEOUT;
 23 import static junit.framework.Assert.assertTrue;
 24 import static org.hamcrest.core.IsNull.notNullValue;
 25 import static org.junit.Assert.assertThat;
 26 
 27 /**
 28 * mobilexprt2015 performance test
 29 * @author liuzhipeng
 30 */
 31 
 32 public class MobileXPRTTest {
 33 
 34 private static final String packageName = "com.mobilexprt2015";
 35 private String[] path = {
 36 "/sdcard/performance/mobilexprt/screen/",
 37 "/sdcard/performance/mobilexprt/log/"};
 38 
 39 private String logTag = "mobilexprttest";
 40 private UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
 41 private UiAssistant cAssistant = new UiAssistant(mDevice,path);
 42 
 43 private String startButton = "com.mobilexprt2015:id/customize_screen_start_tests";
 44 private String testingObject = "com.mobilexprt2015:id/tv_next_scenario";
 45 private String testendObject = "com.mobilexprt2015:id/group_name";
 46 
 47 
 48 @Before
 49 public void setUp() throws Exception {
 50 cAssistant.setup(path,logTag);
 51 this.watcherEndTheCall();
 52 }
 53 
 54 /**
 55 * test mobilexprt performance
 56 * @author liuzhipeng
 57 * @return null
 58 * @throws Exception
 59 */
 60 @Test
 61 public void testAI030004MobileXPRT() throws Exception
 62 {
 63 int TestNumber = 0;
 64 while(TestNumber < 1){
 65 String assisStr = "[TestNumber_"+TestNumber+"]";
 66 openApplication(packageName);
 67 Log.i(logTag,assisStr+"open mobilexprt ");
 68 Thread.sleep(5000);
 69 // UiObject startTest = mDevice.findObject(new UiSelector().
 70 // resourceId(startButton));
 71 // assertTrue("not found start test button",startTest.exists());
 72 // startTest.clickAndWaitForNewWindow();
 73 // Log.i(logTag,assisStr+"click start test button");
 74 // assertTrue("step[1] fail",cAssistant.WaitforUnitObjectExist(0,testingObject,5,100));
 75 // Log.i(logTag,assisStr+"testing page["+testingObject+ "]exist");
 76 // Thread.sleep(1000*60*1);
 77 // assertTrue("step[2] fail",cAssistant.WaitforUnitObjectExist(0,testendObject,17*60,500));
 78 // Log.i(logTag,assisStr+"test end page["+testendObject+"]exist");
 79 // cAssistant.takescreen("mobilexprt[TestNumber_"+TestNumber +"]End",
 80 // "[TestNumber_"+TestNumber +"]mobilexprtScore");
 81 // Log.i(logTag,assisStr+"test end,and takescreen");
 82 cAssistant.QuitApplication(packageName);
 83 Log.i(logTag,assisStr+"quit mobilexprt");
 84 Thread.sleep(1000*10);
 85 TestNumber++;
 86 
 87 }
 88 
 89 }
 90 public void openApplication(String packName) throws RemoteException {
 91 mDevice.wakeUp();
 92 /* Start from the home screen*/
 93 mDevice.pressHome();
 94 
 95 final String launcherPackage = mDevice.getLauncherPackageName();
 96 assertThat(launcherPackage,notNullValue());
 97 mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
 98 LAUNCH_TIMEOUT);
 99 
100 // launch the app
101 Context context = InstrumentationRegistry.getContext();
102 final Intent intent = context.getPackageManager()
103 .getLaunchIntentForPackage(packName);
104 // Clear out any previous instances
105 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
106 context.startActivity(intent);
107 
108 // Wait for the app to appear
109 mDevice.wait(Until.hasObject(By.pkg(packName).depth(0)),
110 LAUNCH_TIMEOUT);
111 }
112 private void watcherEndTheCall(){
113 // UiDevice 动作不会触发监听器
114 // runWatcher是在查找控件失败时会调用,当然你也可以自己调用
115 // findAccessibilityNodeInfo里面会调用runWatcher
116 // UiObject.findAccessibilityNodeInfo,查找控件会用到
117 mDevice.registerWatcher("endCallWatcher", new UiWatcher() {
118 UiObject incomingCall = mDevice.findObject(new UiSelector()
119 .resourceId(""));
120 @Override
121 public boolean checkForCondition() {
122 System.out.println("endCallWatcher running");
123 if(incomingCall.exists()){
124 try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
125 System.out.println("endCall success");
126 return true;
127 }
128 System.out.println("endCall fail");
129 return false;
130 }
131 });
132 }
133 @After
134 public void tearDown() throws Exception {
135 Log.i(logTag,"is triggered :"+ mDevice.hasWatcherTriggered("endCallWatcher"));
136 mDevice.removeWatcher("endCallWatcher");
137 
138 }
139 }

 

posted @ 2017-07-03 15:57  liuzhipenglove  阅读(825)  评论(0编辑  收藏  举报