Uiautomator ---(1) 封装代码
http://www.cnblogs.com/by-dream/p/4996000.html 上面是别人的写法
我自己的写法:
package qq.test; import android.content.Context; import android.content.Intent; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SdkSuppress; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.Until; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.Assert.assertThat; /** * Instrumentation test, which will execute on an Android device. * * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> * http://blog.sina.com.cn/s/blog_51335a0001017x8v.html see LargeTest * http://blog.csdn.net/zr940326/article/details/51586789 */ @RunWith(AndroidJUnit4.class) @SdkSuppress(minSdkVersion = 18) public class ChangeTextBehaviorTest { private static final String BASIC_SAMPLE_PACKAGE = "com.android.music"; private static final int LAUNCH_TIMEOUT = 5000; private static final String STRING_TO_BE_TYPED = "UiAutomator"; private UiDevice mDevice; UiautomatorAssistant uiautomatorAssistant ; @BeforeClass public static void testBeforeClass() { System.out.println("Public Static void testBeforeClass"); } @Before public void startMainActivityFromHomeScreen() throws Exception { // Intialize UiDevice instance your android device mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // start from the home screen // press the home key mDevice.pressHome(); // wait for launcher final String launcherPackage = mDevice.getLauncherPackageName(); assertThat(launcherPackage,notNullValue()); // wait for the music can see in the screen mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT); // launch the app Context context = InstrumentationRegistry.getContext(); final Intent intent = context.getPackageManager() .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE); // Clear out any previous instances intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); // Wait for the app to appear mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT); } @Test public void PictureTest() throws Exception { /* UiObject musicTabButton = mDevice.findObject(new UiSelector() .resourceId("com.android.music:id/songtab")); if(!musicTabButton.isSelected() && musicTabButton.exists() && musicTabButton.isEnabled()) musicTabButton.click(); UiCollection musicList = new UiCollection(new UiSelector() .className("android.widget.RelativeLayout")); UiObject music = musicList.getChild(new UiSelector() .index(3)); if(!music.isSelected()) music.click(); */ uiautomatorAssistant = new UiautomatorAssistant(mDevice); uiautomatorAssistant.ClickById("com.android.music:id/songtab"); uiautomatorAssistant.ClickByCollCLASS("android.widget.RelativeLayout","com.android.music:id/line1","3"); } @After public void tearDown() throws Exception { } } /* @RunWith(AndroidJUnit4.class) @SdkSuppress(minSdkVersion = 18) public class ChangeTextBehaviorTest { private static final String BASIC_SAMPLE_PACKAGE = "com.android.music"; private static final int LAUNCH_TIMEOUT = 5000; private static final String STRING_TO_BE_TYPED = "UiAutomator"; private UiDevice mDevice; public void startMainActivityFromHomeScreen() { // Initialize UiDevice instance mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // Start from the home screen mDevice.pressHome(); // Wait for launcher final String launcherPackage = mDevice.getLauncherPackageName(); assertThat(launcherPackage, notNullValue()); mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT); // Launch the app Context context = InstrumentationRegistry.getContext(); final Intent intent = context.getPackageManager() .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE); // Clear out any previous instances intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); // Wait for the app to appear mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT); } @Test public void testMusic(){ startMainActivityFromHomeScreen(); } } */
封装代码类:
package qq.test; import android.support.test.uiautomator.UiCollection; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; /** * Created by Administrator on 2017/5/11. */ public class UiautomatorAssistant { UiDevice mdevice; public String m_logpathString = "/mnt/sdcard/PerformanceLog.txt"; final static int CLICK_ID = 1000; final static int CLICK_TEXT = 1001; final static int CLICK_CLASS = 1002; UiautomatorAssistant(UiDevice device) { mdevice = device; }// struct function public boolean ClickById(String id) { return ClickByInfo(CLICK_ID, id); } public boolean ClickByText(String text) { return ClickByInfo(CLICK_TEXT, text); } public boolean ClickByCollCLASS(String classname,String id,String text) { return ClickByCollInfo(CLICK_CLASS,classname,id,text); } private boolean ClickByCollInfo(int CLICK, String classname, String id, String text) { UiSelector uiselector = null; UiCollection musicList = null; switch (CLICK) { case CLICK_CLASS: musicList = new UiCollection(new UiSelector().className(classname)); uiselector = new UiSelector().resourceId(id).text(text); break; default: return false; } UiObject uiobject = null; try { uiobject = musicList.getChild(uiselector); } catch (UiObjectNotFoundException e) { e.printStackTrace(); } int i = 0; if(!uiobject.exists()) return false; try { uiobject.click(); } catch (UiObjectNotFoundException e) { e.printStackTrace(); } return true; } private boolean ClickByInfo(int CLICK, String str) { UiSelector uiselector = null; switch (CLICK) { case CLICK_ID: uiselector = new UiSelector().resourceId(str); break; case CLICK_TEXT: uiselector = new UiSelector().text(str); break; default: return false; } // UiObject uiobject = new UiObject(uiselector); UiObject uiobject = mdevice.findObject(uiselector); int i = 0; while (!uiobject.exists() && i < 5) { //SolveProblems(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } if (i == 4) { //TakeScreen(str + "-not-find"); return false; } i++; } try { //UiAutomationLog("Click type:" + CLICK + "content:" + str); uiobject.click(); } catch (UiObjectNotFoundException e) { e.printStackTrace(); } return true; } }