1. 程式人生 > >android UiAutomator基本api的二次封裝

android UiAutomator基本api的二次封裝

本人在使用UiAutomator做測試的時候,封裝了很多方法,由於之前的文章並沒有分享這些封裝方法,導致閱讀不暢。本來打算再把影象識別和輔助類寫完在分享,鑑於已經離職,UI這塊很長時間不太會更新程式碼了,就把所有的封裝方法都分享出來了。裡面有些過時的,暫時無用的大家可以忽略。

下面這個是對UiAutomator基本方法的封裝,還有一個在測試報告生成的時候的基本方法封裝,還有些輔助類,改天我整理一下也發出來。

package source;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import com.android.uiautomator.core.Configurator;
import com.android.uiautomator.core.UiDevice;
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;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Bitmap.CompressFormat;
import android.os.RemoteException;
import android.view.KeyEvent;
import jp.jun_nama.test.utf7ime.helper.Utf7ImeHelper;
/**
* @author ··-·塵
* @E-mail:
[email protected]
* @version 建立時間:2017年8月18日 上午10:53:24 * @alter 修改時間:2017年9月12日 09:20:29 * 類說明:基本api封裝 */ @SuppressWarnings("deprecation") public class UiaLibrary extends UiAutomatorTestCase{ public String LINE = "\r\n"; // public static UiaLibrary library = null; // public static UiaLibrary getInstance() { // library = new UiaLibrary(); // return library; // } public void swipeLeft() {//左滑 int y = UiDevice.getInstance().getDisplayHeight(); int x = UiDevice.getInstance().getDisplayWidth(); UiDevice.getInstance().swipe(x-100, y/2, 100, y/2, 20); sleep(150); } public void swipeRight() {//右滑 int y = UiDevice.getInstance().getDisplayHeight(); int x = UiDevice.getInstance().getDisplayWidth(); UiDevice.getInstance().swipe(100, y/2, x-100, y/2, 20); sleep(150); } public void swipeDown() {//下滑 int y = UiDevice.getInstance().getDisplayHeight(); int x = UiDevice.getInstance().getDisplayWidth(); UiDevice.getInstance().swipe(x/2, 200, x/2, y-200, 20); sleep(150); } public void swipeUp() {//上滑 int y = UiDevice.getInstance().getDisplayHeight(); int x = UiDevice.getInstance().getDisplayWidth(); UiDevice.getInstance().swipe(x/2, y-200, x/2, 200, 20); sleep(150); } public void swipUpLittle() {//上滑一點點 int x = UiDevice.getInstance().getDisplayWidth()/2; int y = UiDevice.getInstance().getDisplayHeight()/2; UiDevice.getInstance().swipe(x, y+150, x, y-150, 20); sleep(150); } public void swipDownLittle() {//下拉一點點 int x = UiDevice.getInstance().getDisplayWidth()/2; int y = UiDevice.getInstance().getDisplayHeight()/2; UiDevice.getInstance().swipe(x, y-150, x, y+150, 20); sleep(150); } public String getNow() {//獲取當前時間 Date time = new Date(); SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String c = now.format(time); return c; } public void screenShot() {//截圖name+time String name = getName(); File file = new File("/mnt/sdcard/123/"); if (!file.exists()) { file.mkdirs(); } File files = new File(file.toString()+"/"+getDayHourMinute()+name+".png"); UiDevice.getInstance().takeScreenshot(files); output("預設截圖成功!"); } //壓縮圖片 public void compressPictureToJpeg(String oldPath, File newFile) throws FileNotFoundException { Bitmap bitmap = BitmapFactory.decodeFile(oldPath);//建立並例項化bitmap物件 FileOutputStream out = new FileOutputStream(newFile);//建立檔案輸出流 bitmap.compress(CompressFormat.JPEG, 100, out);//將圖片轉化為jpeg格式輸出 } //擷取某個控制元件的影象 public Bitmap getBitmapByResourceId(String id) throws UiObjectNotFoundException { Rect rect = getUiObjectByResourceId(id).getVisibleBounds();//獲取控制元件的rect物件 String path = screenShot("test");//截圖 Bitmap bitmap = BitmapFactory.decodeFile(path);//建立並例項化bitmap物件 bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());//擷取bitmap例項 return bitmap; } //獲取某一座標點的顏色值 public int getColorPixel(int x, int y) { screenShot("test");//截圖 String path = "/mnt/sdcard/123/test.png"; Bitmap bitmap = BitmapFactory.decodeFile(path);//新建並例項化bitmap物件 int color = bitmap.getPixel(x, y);//獲取座標點畫素顏色 // output(color);//輸出顏色值 return color; } public int getRedPixel(int x, int y) { screenShot("test");//截圖 String path = "/mnt/sdcard/123/test.png"; Bitmap bitmap = BitmapFactory.decodeFile(path);//新建並例項化bitmap物件 int color = bitmap.getPixel(x, y);//獲取座標點畫素顏色 // output(color);//輸出顏色值 int red = Color.red(color); return red; } public int getGreenPixel(int x, int y) { screenShot("test");//截圖 String path = "/mnt/sdcard/123/test.png"; Bitmap bitmap = BitmapFactory.decodeFile(path);//新建並例項化bitmap物件 int color = bitmap.getPixel(x, y);//獲取座標點畫素顏色 // output(color);//輸出顏色值 int green = Color.green(color); return green; } public int getBluePixel(int x, int y) { screenShot("test");//截圖 String path = "/mnt/sdcard/123/test.png"; Bitmap bitmap = BitmapFactory.decodeFile(path);//新建並例項化bitmap物件 int color = bitmap.getPixel(x, y);//獲取座標點畫素顏色 // output(color);//輸出顏色值 int blue = Color.blue(color); return blue; } public int[] getRGBcolorPixel(int x, int y) { screenShot("testDemo"); String path = "/mnt/sdcard/123/testDemo.png"; Bitmap bitmap = BitmapFactory.decodeFile(path); int color = bitmap.getPixel(x, y); int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); int[] rgb = {red, green, blue}; return rgb; } //根據顏色判斷狀態 public boolean isBlue(UiObject uiObject) throws UiObjectNotFoundException { screenShot("test");//截圖 String path = "/mnt/sdcard/123/test.png"; Bitmap bitmap = BitmapFactory.decodeFile(path);//新建並例項化bitmap物件 Rect rect = uiObject.getVisibleBounds(); int x = rect.left; int xx = rect.right; int y = rect.top; int yy = rect.bottom; List<Integer> blueColor = new ArrayList<Integer>(); for (int i = x; i < xx; i++) { for (int k = y;k < yy;k++) { int color = bitmap.getPixel(i, k);//獲取座標點畫素顏色 int red = Color.blue(color); blueColor.add(red); } } int sum = 0; for (int i = 0;i<blueColor.size();i++) { sum += blueColor.get(i); } // output(sum/blueColor.size()); return sum/blueColor.size() > 200?true:false; } /* * 影象對比 * 預設影象寬高一致 */ public boolean comparePicture(String path1, String path2, double limit) { Bitmap bitmap1 = BitmapFactory.decodeFile(path1);//建立並初始化bitmap物件 Bitmap bitmap2 = BitmapFactory.decodeFile(path2);//建立並初始化bitmap物件 int width = bitmap1.getWidth();//獲取寬 int height = bitmap1.getHeight();//獲取高 int total = 0;//統計相同次數 int times = 0;//統計總次數 //遍歷畫素點的顏色值,節省時間每次遞增5個畫素點 for (int x = 0;x < width;x +=3) { for (int y = 0; y < height; y +=3) { int oldPic = bitmap1.getPixel(x, y);//獲取顏色值 int newPic = bitmap2.getPixel(x, y);//獲取顏色值 // int differ = Math.abs(ss - dd);//計算絕對差 times++; if (oldPic == newPic) {//如果相等,則認為相同 total++; } } } double differ = total*1.0/times; output(differ); return differ > 0.99?true:false;//返回統計結果 } //獲取視訊播放進度條 public double getVideoProgress(Bitmap bitmap) { int height = bitmap.getHeight(); int width = bitmap.getWidth(); List<Integer> date = new ArrayList<Integer>(); for (int i = 0;i < width; i++) { int color = bitmap.getPixel(i, height / 2); int red = Color.red(color); // output(red); date.add(red); } int date1 = 0,date2 = 0,date3 = 0,date4 = 0; int status1 = 0,status2 = 0; for (int i = 1;i < date.size() - 1;i++) { if (date.get(i) == 255 && status1 == 0) { status1++; date1 = i; } if (date.get(i) == 238 && status2 == 0) { status2++; date2 = i; } if (date.get(i + 1) < 238 && date.get(i) == 238) { date3 = i; } if (date.get(i + 1) < 165 && date.get(i) == 165) { date4 = i; } } // output(date1, date2, date3, date4); // output((date2 + date3 - date1 * 2.00)/(date4 - date1)/2); return (date2 + date3 - date1 * 2.00)/(date4 - date1)/2; } public String getDayHourMinute() {//獲取日期小時分鐘 Date time = new Date(); SimpleDateFormat format = new SimpleDateFormat("dd-HH-mm"); String name = format.format(time); return name; } public String screenShot(String name) {//截圖並命名 File file = new File("/mnt/sdcard/123/"); if (!file.exists()) { file.mkdirs(); } File files = new File("/mnt/sdcard/123/"+name+".png"); UiDevice.getInstance().takeScreenshot(files); output(name + ".png 截圖成功!"); String path = "/mnt/sdcard/123/" + name + ".png"; return path; } public String screenShot(int num) {//截圖並命名 File file = new File("/mnt/sdcard/123/"); if (!file.exists()) { file.mkdirs(); } File files = new File("/mnt/sdcard/123/"+num+".png"); UiDevice.getInstance().takeScreenshot(files); output(num + ".png 截圖成功!"); String path = "/mnt/sdcard/123/" + num + ".png"; return path; } public void circle(int x, int y, int r) {//畫圓的方法 double d = (double) (Math.PI/30);//角度 double[] xxx = new double[61]; for(int i=0;i<61;i++){ xxx[i]=Math.cos(i*d); } //獲取x座標 double[] yyy = new double[61]; for(int i=1;i<61;i++){ yyy[i]=Math.sin(i*d); } //獲取y座標 int[] xxx1 = new int[61]; for(int i=0;i<61;i++){ xxx1[i]=(int) (xxx[i]*200); } //轉化座標值型別 int[] yyy1 = new int[61]; for(int i=0;i<61;i++){ yyy1[i]=(int) (yyy[i]*200); } //轉化座標值型別 Point[] p = new Point[61]; for(int i=0;i<61;i++){ p[i]=new Point(); p[i].x = xxx1[i]+x; p[i].y = y-yyy1[i]+50; } //建立點陣列 UiDevice.getInstance().swipe(p, 2); } public void heart(int x, int y,int r) {//畫心形的方法 double d = (double) (Math.PI/30); double[] angle = new double[61];//設定角度差 for(int i=0;i<61;i++){ angle[i]=i*d; } //建立一個角度差double陣列 double[] ox = new double[61]; for(int i=0;i<61;i++){ ox[i]= r*(2*Math.cos(angle[i])-Math.cos(2*angle[i])); } //計算x座標 double[] oy = new double[61]; for(int i=0;i<61;i++){ oy[i]=r*(2*Math.sin(angle[i])-Math.sin(2*angle[i])); } //計算y座標 Point[] heart = new Point[61]; for(int i=0;i<61;i++){ heart[i] = new Point(); heart[i].x = (int) oy[i]+x; heart[i].y = -(int) ox[i]+y; } //建立一個點陣列,這裡座標一定要轉化一下,不然是倒著的心形 UiDevice.getInstance().swipe(heart, 2); } public UiObject getUiObjectByText(String text) {//通過文字獲取控制元件 return new UiObject(new UiSelector().text(text)); } public UiObject getUiObjectByTextContains(String text) { return new UiObject(new UiSelector().textContains(text)); } //通過text開始文字查詢控制元件 public UiObject getUiObjectByStartText(String text) { return new UiObject(new UiSelector().textStartsWith(text)); } public UiObject getUiObjectByStartDesc(String desc) { return new UiObject(new UiSelector().descriptionStartsWith(desc)); } public UiObject getUiObjectByTextClassName(String text,String classname) {//通過文字和類名獲取控制元件 return new UiObject(new UiSelector().text(text).className(classname)); } public UiObject getUiObjectByTextResourceId(String text, String id) {//通過文字和id獲取物件 return new UiObject(new UiSelector().text(text).resourceId(id)); } public UiObject getUiObjectByResourceIdClassName(String id, String type) { return new UiObject(new UiSelector().resourceId(id).className(type)); } public UiObject getUiObjectByResourceId(String id) {//通過資源ID獲取控制元件 return new UiObject(new UiSelector().resourceId(id)); } public UiObject getUiObjectByDesc(String desc) {//通過desc獲取控制元件 return new UiObject(new UiSelector().description(desc)); } public UiObject getUiObjectByStartDescContains(String desc) { return new UiObject(new UiSelector().descriptionContains(desc)); } public UiObject getUiObjectByDescContains(String desc) { return new UiObject(new UiSelector().descriptionContains(desc)); } public UiObject getUiObjectByClassName(String type) {//通過classname獲取控制元件 return new UiObject(new UiSelector().className(type)); } public UiObject getUiObjectByResourceIdIntance(String id, int instance) {//通過id和instance獲取控制元件 return new UiObject(new UiSelector().resourceId(id).instance(instance)); } //長按控制元件 public void longclickUiObectByResourceId(String id) throws UiObjectNotFoundException { int x = getUiObjectByResourceId(id).getBounds().centerX(); int y = getUiObjectByResourceId(id).getBounds().centerY(); UiDevice.getInstance().swipe(x, y, x, y, 300);//最後一個引數單位是5ms } public void longclickUiObectByDesc(String desc) throws UiObjectNotFoundException { int x = getUiObjectByDesc(desc).getBounds().centerX(); int y = getUiObjectByDesc(desc).getBounds().centerY(); UiDevice.getInstance().swipe(x, y, x, y, 300);//最後一個引數單位是5ms } public void longclickUiObectByText(String text) throws UiObjectNotFoundException { int x = getUiObjectByText(text).getBounds().centerX(); int y = getUiObjectByText(text).getBounds().centerY(); UiDevice.getInstance().swipe(x, y, x, y, 300);//最後一個引數單位是5ms } //點選中心 public void clickCenter() { int x = UiDevice.getInstance().getDisplayWidth(); int y = UiDevice.getInstance().getDisplayHeight(); clickPiont(x/2, y/2); } public void writeText(String text) throws UiObjectNotFoundException{//輸入文字 getUiObjectByClassName("android.widget.EditText").setText(Utf7ImeHelper.e(text)); } public UiScrollable getUiScrollabe() {//獲取滾動控制元件 return new UiScrollable(new UiSelector().scrollable(true)); } public UiScrollable getUiScrollableByResourceId(String id) {//獲取滾動物件 return new UiScrollable(new UiSelector().scrollable(true).resourceId(id)); } public void getChildByTextOfUiScrollableByClassName(String type, String text) throws UiObjectNotFoundException { getScrollableByClassName(type).getChildByText(new UiSelector().text(text), text).clickAndWaitForNewWindow(); } public UiObject getUiObjectByResourIdIndex(String id, int index) {//通過ID和index獲取控制元件 return new UiObject(new UiSelector().resourceId(id).index(index)); } public void randomClickOpiton() throws UiObjectNotFoundException { int num = getUiObjectByClassName("android.widget.ListView").getChildCount(); int i = new Random().nextInt(num); getUiObjectByResourceIdIntance("com.gaotu100.superclass:id/simpleitemview_left_text", i).clickAndWaitForNewWindow(); } public void outputBegin() {//輸出開始 System.out.println(getNow()+"..-. ...- 開始!"); } public void outputNow() {//輸出當前時間 System.out.println(getNow()); } public void outputOver() {//輸出結束 System.out.println(getNow()+"..-. ...- 結束!"); } // public void output(String text) {//明顯輸出 // System.out.println(text); // } // public void output(double num) {//明顯輸出 // System.out.println(num); // } // public void output(int num) {//方法過載 // System.out.println("===="+num+"===="); // } //明顯輸出 public void output(String text) { System.out.println(text); } public void output(String ...text) {//方法過載 for (int i = 0; i < text.length; i++) { System.out.println("第"+ (i+1) + "個:"+ text[i]); } } public void output(long num) { System.out.println(num); } public void output(long ...num) {//方法過載 for (int i = 0; i < num.length; i++) { System.out.println("第"+ (i+1) + "個:"+ num[i]); } } public void output(double num) { System.out.println(num); } public void output(double ...num) {//方法過載 for (int i = 0; i < num.length; i++) { System.out.println("第"+ (i+1) + "個:"+ num[i]); } } public void output(int num) { System.out.println(num); } public void output(int ...num) {//方法過載 for (int i = 0; i < num.length; i++) { System.out.println("第"+ (i+1) + "個:"+ num[i]); } } public void outpu(Object ...object) { for (int i = 0; i < object.length; i++) { System.out.println("第"+ (i+1) + "個:"+ object[i]); } } public void outpu(Object object) { System.out.println(object.toString()); } public void pressTimes(int keyCode, int times) {//對於一個按鍵按多次 for(int i=0;i<times;i++){ sleep(200); UiDevice.getInstance().pressKeyCode(keyCode); } } public void waitForUiObjectByText(String text) {//等待物件出現 // Date start = new Date(); // boolean key = true; // while(key){ // sleep(200); // UiObject it = new UiObject(new UiSelector().text(text)); // if (it.exists()) { // key = false; // } // Date end = new Date(); // long time = end.getTime() - start.getTime(); // if (time>10000) { // output("超過10s沒有出現!"); // key = false; // } // } getUiObjectByText(text).waitForExists(10000); } public void waitForUiObjectByStartText(String text) { getUiObjectByStartText(text).waitForExists(10000); } //輸出時間差 public void outputTimeDiffer(Date start, Date end) { long time = end.getTime() - start.getTime(); double differ = (double)time/1000; output("總計用時"+differ+"秒!"); } //獲取子控制元件點選 public void getScrollChildByText(String text) throws UiObjectNotFoundException { UiObject child = getUiScrollabe().getChildByText(new UiSelector().text(text), text); child.clickAndWaitForNewWindow(); } //通過classname獲取滾動控制元件 public UiScrollable getScrollableByClassName(String type) { return new UiScrollable(new UiSelector().scrollable(true).className(type)); } public void waitForUiObjectByClassName(String type) throws UiObjectNotFoundException {//等待控制元件出現 getUiObjectByClassName(type).waitForExists(10000); } public void waitForUiObjectByTextContains(String text) {//等待物件出現 // Date start = new Date(); // boolean key = true; // while(key){ // sleep(1000); // UiObject it = new UiObject(new UiSelector().textContains(text)); // if (it.exists()) { // key = false; // } // Date end = new Date(); // long time = end.getTime() - start.getTime(); // if (time>10000) { // output("超過10s沒有出現!"); // key = false; // } // } getUiObjectByText(text).waitForExists(10000); } public void waitForUiObjectByDesc(String desc) {//等待物件出現 // Date start = new Date(); // boolean key = true; // while(key){ // sleep(1000); // UiObject it = new UiObject(new UiSelector().description(desc)); // if (it.exists()) { // key = false; // } // Date end = new Date(); // long time = end.getTime() - start.getTime(); // if (time>10000) { // output("超過10s沒有出現!"); // key = false; // } // } getUiObjectByDesc(desc).waitForExists(10000); } public void waitForUiObjectByResourceId(String id) {//等待物件出現 // Date start = new Date(); // boolean key = true; // while(key){ // sleep(1000); // UiObject it = new UiObject(new UiSelector().resourceId(id)); // if (it.exists()) { // key = false; // } // Date end = new Date(); // long time = end.getTime() - start.getTime(); // if (time>10000) { // output("超過10s沒有出現!"); // key = false; // } // } getUiObjectByResourceId(id).waitForExists(10000); } public void waitForUiObject(UiSelector selector) {//等待物件出現 // Date start = new Date(); // boolean key = true; // while(key){ // sleep(1000); // UiObject it = new UiObject(selector); // if (it.exists()) { // key = false; // } // Date end = new Date(); // long time = end.getTime() - start.getTime(); // if (time>10000) { // output("超過10秒沒有出現!"); // key = false; // } // } new UiObject(selector).waitForExists(10000); } public String getTextByResourceId(String id) throws UiObjectNotFoundException { return getUiObjectByResourceId(id).getText(); } public String getDescByResourceI1d(String id) throws UiObjectNotFoundException { return getUiObjectByResourceId(id).getContentDescription(); } public String getTextByResourceIdClassName(String id,String type) throws UiObjectNotFoundException { return getUiObjectByResourceIdClassName(id, type).getText(); } //獲取兄弟控制元件的文字 public String getTextByBrother(String myid, String brotherid) throws UiObjectNotFoundException { return getUiObjectByResourceId(myid).getFromParent(new UiSelector().resourceId(brotherid)).getText(); } public void writeTextByResourceId(String id, String text) throws UiObjectNotFoundException { getUiObjectByResourceId(id).setText(Utf7ImeHelper.e(text)); } public void clickPiont(int x, int y) {//點選某一個點 UiDevice.getInstance().click(x, y); } public void getUiObjectByResoureIdAndclickRightHalf(String id) throws UiObjectNotFoundException { //獲取控制元件大小 Rect sss = getUiObjectByResourceId(id).getBounds(); //計算中心偏移量 clickPiont(sss.centerX()+sss.width()/4, sss.centerY()); } //點選控制元件左半邊 public void getUiObjectByResoureIdAndclickLeftHalf(String id) throws UiObjectNotFoundException { //獲取控制元件大小 Rect sss = getUiObjectByResourceId(id).getBounds(); //計算中心偏移量 clickPiont(sss.centerX()-sss.width()/4, sss.centerY()); } public void setShort() {//設定短等待 Configurator.getInstance().setActionAcknowledgmentTimeout(500); } public void setFast() {//設定短等待 Configurator.getInstance().setActionAcknowledgmentTimeout(100); } public void setLong() {//設定長等待 Configurator.getInstance().setActionAcknowledgmentTimeout(1500); } //清除中文文字 public void clearTextByResourceId(String id) throws UiObjectNotFoundException { String name = getUiObjectByResourceId(id).getText(); // output(name.length()); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END); // UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_HOME); //如果游標在最後 pressTimes(KeyEvent.KEYCODE_DEL, name.length()); //如果游標在最開始 // pressTimes(KeyEvent.KEYCODE_FORWARD_DEL, name.length()); } //把string型別轉化為int public int changeStringToInt (String text) { // return Integer.parseInt(text); return Integer.valueOf(text); } //等待文字控制元件並點選 public void waitForClassNameAndClick(String type) throws UiObjectNotFoundException { waitForUiObjectByClassName(type); // getUiObjectByText(type).waitForExists(10000); getUiObjectByClassName(type).clickAndWaitForNewWindow(); } public void waitForTextAndClick(String text) throws UiObjectNotFoundException { waitForUiObjectByText(text); // getUiObjectByText(text).waitForExists(10000); getUiObjectByText(text).clickAndWaitForNewWindow(); } //通過開始文字查詢控制元件並點選 public void waitForStartTextAndClick(String text) throws UiObjectNotFoundException { getUiObjectByStartText(text).waitForExists(10000); getUiObjectByStartText(text).clickAndWaitForNewWindow(); } public void waitForTextContainsAndClick(String text) throws UiObjectNotFoundException { getUiObjectByTextContains(text).waitForExists(10000); getUiObjectByTextContains(text).clickAndWaitForNewWindow(); } public void waitForStartDescAndClick(String desc) throws UiObjectNotFoundException { getUiObjectByStartDesc(desc).waitForExists(10000); getUiObjectByStartDesc(desc).clickAndWaitForNewWindow(); } public void waitForDescContainsAndClick(String desc) throws UiObjectNotFoundException { getUiObjectByDescContains(desc).waitForExists(10000); getUiObjectByDescContains(desc).clickAndWaitForNewWindow(); } //等待資源id並點選 public void waitForResourceIdAndClick(String id) throws UiObjectNotFoundException { waitForUiObjectByResourceId(id); // getUiObjectByResourceId(id).waitForExists(10000); getUiObjectByResourceId(id).clickAndWaitForNewWindow(); } //等待desc並點選 public void waitForDescAndClick(String desc) throws UiObjectNotFoundException { waitForUiObjectByDesc(desc); getUiObjectByDesc(desc).clickAndWaitForNewWindow(); } //開啟APP public void startClassApp() throws IOException, InterruptedException { Runtime.getRuntime().exec("am start -n com.gaotu100.superclass/.activity.main.SplashActivity").waitFor(); } public void startWechat() throws IOException, InterruptedException { Runtime.getRuntime().exec("am start -n com.tencent.mm/.ui.LauncherUI").waitFor(); } //關閉APP public void stopClassApp() throws InterruptedException, IOException { sleep(500); Runtime.getRuntime().exec("am force-stop com.gaotu100.superclass").waitFor(); sleep(500); } //開啟alertover public void stopAlertover() throws InterruptedException, IOException { Runtime.getRuntime().exec("am force-stop com.alertover.app").waitFor(); sleep(500); } //關閉alertover public void startAlertover() throws IOException, InterruptedException { sleep(500); Runtime.getRuntime().exec("am start -n com.alertover.app/.activity.LoginActivity").waitFor(); } //開啟或者關閉wifi public void closeOrOpenWifi() throws InterruptedException, IOException { Runtime.getRuntime().exec("am start -n run.wifibutton/.WifiButtonActivity").waitFor(); sleep(1000); } //關閉微信 public void stopWechat() throws InterruptedException, IOException { sleep(500); Runtime.getRuntime().exec("am force-stop com.tencent.mm").waitFor(); Thread.sleep(500); } //關閉支付寶 public void stopAlipay() throws InterruptedException, IOException { Runtime.getRuntime().exec("am force-stop com.eg.android.AlipayGphone").waitFor(); Thread.sleep(500); } //開啟APP public void startWishApp() throws IOException { Runtime.getRuntime().exec("am start -n com.chaojizhiyuan.superwish/.activity.main.SplashActivity"); } //關閉APP public void stopWishApp() throws InterruptedException, IOException { sleep(500); Runtime.getRuntime().exec("am force-stop com.chaojizhiyuan.superwish").waitFor(); sleep(500); } //獲取隨機數 public int getRandomInt(int num) { return new Random().nextInt(num); } //驗證跳轉支付寶 public void verifySkipAlipay() { waitForUiObjectByText("新增銀行卡付款"); String packagename = UiDevice.getInstance().getCurrentPackageName(); assertEquals("跳轉支付寶失敗!", "com.eg.android.AlipayGphone", packagename); } //驗證跳轉支付寶 public void verifySkipWechat() { waitForUiObjectByText("使用零錢支付"); String packagename = UiDevice.getInstance().getCurrentPackageName(); assertEquals("跳轉微信失敗!", "com.tencent.mm", packagename); } //螢幕提醒 public void warningTester() throws RemoteException { UiDevice.getInstance().sleep();//滅屏 sleep(1200);//休眠 if (UiDevice.getInstance().isScreenOn()) {//獲取螢幕狀態 return;//如果亮屏狀態則結束執行 } else { UiDevice.getInstance().wakeUp();//如果的滅屏狀態則重新執行本方法 warningTester();//遞迴 } } //向前滾動 public boolean scrollForward() throws UiObjectNotFoundException { return getUiScrollabe().scrollForward(50); } //向後滾動 public boolean scrollBackward() throws UiObjectNotFoundException { return getUiScrollabe().scrollBackward(50); } public void deleteScreenShot() {//刪除截圖資料夾 File file = new File("/mnt/sdcard/123/"); if (file.exists()) {//如果file存在 File[] files = file.listFiles();//獲取資料夾下檔案列表 for (int i = 0; i < files.length; i++) {//遍歷刪除 files[i].delete(); } file.delete();//最後刪除資料夾,如果不存在直接刪除資料夾 } else { output("資料夾不存在!"); } } }