android UiAutomator基本api的二次封裝

本人在使用UiAutomator作測試的時候,封裝了不少方法,因爲以前的文章並無分享這些封裝方法,致使閱讀不順暢。原本打算再把圖像識別和輔助類寫完在分享,鑑於已經離職,UI這塊很長時間不太會更新代碼了,就把全部的封裝方法都分享出來了。裏面有些過期的,暫時無用的你們能夠忽略。java

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

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:Fhaohaizi@163.com
* @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("文件夾不存在!");
		}
		
	}
 
}

往期文章精選

  1. java一行代碼打印心形
  2. Linux性能監控軟件netdata中文漢化版
  3. 接口測試代碼覆蓋率(jacoco)方案分享
  4. 性能測試框架
  5. 如何在Linux命令行界面愉快進行性能測試
  6. 圖解HTTP腦圖
  7. 寫給全部人的編程思惟
  8. 將json數據格式化輸出到控制檯
  9. 如何測試機率型業務接口
  10. 將swagger文檔自動變成測試代碼
  11. Mac+httpclient高併發配置實例
  12. httpclient處理多用戶同時在線

公衆號地圖 ☢️ 一塊兒來~FunTester

相關文章
相關標籤/搜索