|
本人在使用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: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);
- }
复制代码
|
|