14.Android UiAutomator 图像处理
一、BitMap介绍
1.图像使用场景
1)效果类截图
2)不可见的组件图像对比
3)失败与异常截图
4)利用图像判断组件
2.部分API简单说明
API |
说明 |
compress | 压缩图片 |
copy | 复制图片 |
createBitmap | 创建图片 |
getHeight | 获取图片高度 |
getWidth | 获取图片宽度 |
getPixel | 获取某个点颜色值 |
setPixel | 设置某个点颜色值 |
3.创建bitmap实例
//方法体代码
public class ImageTestCase extends UiAutomatorTestCase{
public void saveBitMapToSdcard(Bitmap bitmap,String newName){
FileOutputStream out=null;
try {
out=new FileOutputStream("/mnt/sdcard/"+newName+".jpg");
if(out!=null){
//三个参数分别为格式、保存的文件质量、文件流
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} //用例部分代码
public class test1 extends ImageTestCase{
//快速调试
public static void main(String [] args){
new UiAutomatorHelper("test","testDemo1.test1","testDemo1","2");
}
//创建一个Bitmap
public void testDemo1(){
//a.截取一张图片
String Path="/mnt/sdcard/testBitMap.png";
File storePath=new File(Path);
UiDevice.getInstance().takeScreenshot(storePath);
sleep(1000);
//b.将图片重命名并保存
//从文件中创建bitmap
Bitmap bitmap=BitmapFactory.decodeFile(Path);
//调用方法体
saveBitMapToSdcard(bitmap,"new-Image-88");
}
}
二、获取像素值与图像截取
像素值:每一个像素点的颜色值
1.获取某点像素值实例
//方法体代码
public class ImageTestCase extends UiAutomatorTestCase{
//根据描述获取组件
public UiObject obj(String text){
return new UiObject(new UiSelector().description(text));
}
//截取一张图片后另存
public void cutBitmap(Rect rect,String path){
Bitmap m=BitmapFactory.decodeFile(path);
m=m.createBitmap(m,rect.left,rect.top,rect.width(),rect.height());
//调用上面例子中的那个方法,实际调试过程中如果需要就把那个方法体加上
saveBitMapToSdcard(m, "cutImg_88");
}
//获取某点的颜色值
public int getColorPicel(int x,int y){
String path="/mnt/sdcard/testcolor.png";
File file=new File(path);
UiDevice.getInstance().takeScreenshot(file);
Bitmap m=BitmapFactory.decodeFile(path);
int color=m.getPixel(x, y);
return color;
}
} //用例代码
public class test1 extends ImageTestCase{
//快速调试
public static void main(String [] args){
new UiAutomatorHelper("test","testDemo1.test1","testDemo2","2");
}
//用例
public void testDemo2() throws UiObjectNotFoundException{
//截取图片
Rect rect = obj("城市").getBounds();
String path="/mnt/sdcard/testcolor.png";
File file=new File(path);
UiDevice.getInstance().takeScreenshot(file);
//调用方法体
cutBitmap(rect,path);
//调用方法体获取某个点的颜色值
int color=getColorPicel(rect.centerX(),rect.centerY());
System.out.println("COLOR:"+color);
}
}
三、图像嵌入文字
截图的时候希望把用例场景用文字写在图像上,便于快速查看
1.图像嵌入文字实例:
//方法体
public class ImageTestCase extends UiAutomatorTestCase{
//截图方法
public void screenshotAndDrawRext(String path,String imageName,String text){
File file=new File(path);
UiDevice.getInstance().takeScreenshot(file);
Bitmap bitmap=BitmapFactory.decodeFile(path);
Bitmap drawBitmap=drawTextBitmap(bitmap,text);
saveBitMapToSdcard(drawBitmap, imageName);//调用前面第一个例子中的方法
}
//嵌入文字方法
public Bitmap drawTextBitmap(Bitmap bitmap,String text){
int x=bitmap.getWidth();
int y=bitmap.getHeight();
//创建一个更大的位图
Bitmap newBitmap=Bitmap.createBitmap(x,y+80,Bitmap.Config.ARGB_8888);
Canvas canvans=new Canvas(newBitmap);
Paint paint=new Paint();
//在原图位置(0,0)叠加一张图片
canvans.drawBitmap(bitmap, 0, 0,paint);
//画笔眼色
paint.setColor(Color.parseColor("#FF0000"));
paint.setTextSize(80);//设置文字大小
canvans.drawText(text, 300, y+55, paint);//写字
canvans.save(Canvas.ALL_SAVE_FLAG);//保存
canvans.restore();
return newBitmap;
}
} //用例
public class test1 extends ImageTestCase{
//快速调试
public static void main(String [] args){
new UiAutomatorHelper("test","testDemo1.test1","testDemo3","2");
}
public void testDemo3(){
String path="/mnt/sdcard/testDrawText.png";
String imageName="testDrawText_888";
String text="测试输入";
//调用方法体
screenshotAndDrawRext(path, imageName, text);
}
}
四、图像对比
某些特殊组件无法获取到组件信息,无法判断状态
1.图像对比实例
//方法体
public class ImageTestCase extends UiAutomatorTestCase{
//图像对比的方法
public boolean imageSameAs(String targetImagePath,String comPath,double percent){
try {
//创建两个bitmap
Bitmap m1=BitmapFactory.decodeFile(targetImagePath);
Bitmap m2=BitmapFactory.decodeFile(comPath);
//声明变量
int width=m2.getWidth();
int height=m2.getHeight();
int numDiffPixels=0;
//横纵对比,涉及到两个循环
for(int y=0;y<height;y++){
for(int x=0;x<width;x++){
//取不相等的像素值
if(m2.getPixel(x, y)!=m1.getPixel(x, y)){
numDiffPixels++;
}
}
}
double totalPices=height*width;//总像素值
double diffPercent=numDiffPixels/totalPices;//不相等的百分比
return percent<=1.0-diffPercent;//返回相似度
} catch (Exception e) {
}
return false;
}
} //图片对比用例部分
public class test1 extends ImageTestCase{
//快速调试
public static void main(String [] args){
new UiAutomatorHelper("test","testDemo1.test1","testDemo4","2");
}
//图片对比
public void testDemo4(){
//截取两张对比图
String targetImagePath="/mnt/sdcard/c1.png";
String comPath="/mnt/sdcard/c2.png";
File f1=new File(targetImagePath);
File f2=new File(comPath);
UiDevice.getInstance().takeScreenshot(f1);
sleep(1000);
UiDevice.getInstance().pressHome();//换个场景
sleep(1000);
UiDevice.getInstance().takeScreenshot(f2);
//调用图像对比方法
boolean b=imageSameAs(targetImagePath,comPath,1.0d);
//输出对比结果
System.out.println("图像比对结果:"+b);
}
}
14.Android UiAutomator 图像处理的更多相关文章
- Android uiautomator gradle build system
This will guide you through the steps to write your first uiautomator test using gradle as it build ...
- Android UiAutomator 自动化测试编译运行---新手2
1.首先打开eclipse创建java项目
- Appium python自动化测试系列之Android UIAutomator终极定位(七)
android uiautomator text定位 可能有人不知道为什么说android uiautomator是终极定位,而且android uiautomator和appium有什么关系呢?如果 ...
- Android UiAutomator - CTS Frame
使用UiAutomator进行UI自动化测试后,生成的测试结果并不是很美观.为了生成一份好看的测试结果(报告),本文将使用CTS框架,当然也可以自己编写一份测试报告框架(如:生成html,excel报 ...
- Android UiAutomator 快速调试
背景:在Eclipse中不能直接运行Uiautomator工程,所以每次编写一份用例都要进行手动输入命令,很烦.调试起来不仅繁琐还浪费时间.网上找到一份快速调试的代码UiAutomatorHelper ...
- Android UiAutomator
UiAutomator是一个做UI测试的自动化框架.<Android自动化测试框架>中已有详细介绍,这里就不再累赘了. 一.首先了解自动化测试流程 自动化需求分析 测试用例设计 自动化框架 ...
- python+Android+uiautomator的环境
Python+Android+uiautomator的环境搭建 Python 下载适合系统的版本并安装,安装时勾选把路径加入path 验证:windows下打开cmd输入python 出现以下界面说明 ...
- Appium+python自动化(十二)- Android UIAutomator终极定位凶“胸”器(七)(超详解)
简介 乍眼一看,小伙伴们觉得这部分其实在异性兄弟那里就做过介绍和分享了,其实不然,上次介绍和分享的大哥是uiautomatorviewer,是一款定位工具.今天介绍的是一个java库,提供执行自动化测 ...
- Android uiautomator实例使用
转载自:http://blog.csdn.net/huiguixian/article/details/22398193 Android测试工具中,Monkey Runner只要简单几个指令即可,但他 ...
随机推荐
- django的htpp请求之WSGIRequest
WSGIRequest对象 Django在接收到http请求之后,会根据http请求携带的参数以及报文信息创建一个WSGIRequest对象,并且作为视图函数第一个参数传给视图函数.这个参数就是dja ...
- Yii2 UploadedFile上传文件
通过 UploadFile::getInstance($model, $attribute); UploadFile::getInstances($model, $attribute); Upload ...
- js操作对象属性值为字符串
今天在项目开发中遇到一个没遇到过的问题,这个问题是需要对比两个对象a和b,a是一个只有一个属性的对象,b是一个含有多个属性对象,如果b中包含和a一模一样的属性名和值,则把这个一样的属性和值从b中删除了 ...
- Manacher算法——求最长回文子串
首先,得先了解什么是回文串.回文串就是正反读起来就是一样的,如“abcdcba”.我们要是直接采用暴力方法来查找最长回文子串,时间复杂度为O(n^3),好一点的方法是枚举每一个字符,比较较它左右距离相 ...
- 石家庄铁道大学网站首页UI分析
今天的软件工程王老师讲了UI的设计,以前狭隘的认为只有移动设备上的界面叫UI,百度一下才发现UI其实有这么多含义:UI即User Interface的简称.泛指用户的操作界面,UI设计主要指界面的样式 ...
- “我爱淘”第二冲刺阶段Scrum站立会议3
完成任务: 完成了注册界面的设计,以及部分代码,但是还没有完成服务器端的添加功能. 计划任务: 将注册功能实现了它,可以对数据库进行添加,在客户端实现分类功能,通过学院的分类查看书籍. 遇到问题: 分 ...
- springMVC 流程
springMVC流程控制 SpringMVC流程 web.xml 中配置 org.springframework.web.servlet.DispatcherServlet 这一步其实和spring ...
- WCF RestFull提交数据超出限额解决方法
最近在使用wcf restfull时出现了超大数据提交出错的问题. 服务端会返回错误:服务器处理请求时遇到错误.有关构造有效服务请求的内容,请参阅服务帮助页.异常消息为“反序列化对象 属于类型 Yes ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺-8
一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 服务器的完善,后端配置的修改. 明天计划完成的工作: 完善各个功能以及修改bug. 工作中遇到的困难: 服务器的语言编程困难,后端调试中不断 ...
- 【第五周】alpha发布之小组评论
对于昨天的阿尔法发布,有那么几点需要说一下: 1,对这次阿尔法发布的过程,我们组还是基本顺利的,由于之前吃过亏,这次我提前试用了一下投影仪,做了些调试,之后的发布过程起码设备上是正常的. 2,我们的项 ...