根据前一篇文章《移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇》所述,因为Appium和MonkeyRunner有一个共同点--代码控制流程都是在客户端实现的。所以要把MonkeyRunner在PC端实现的图片比对和获取子图功能移植到同样是在PC端运行的Appium是很容易的事情,但是对于在服务器端运行的Robotium和UiAutomator就是另外一回事了。

因为在Android的sdk中,MonkeyRunner获取子图和图片比对需要用到的以下两个类是没有支持的,简单来说就是java.awt这个库是不支持的:

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

但是在Android的sdk中有Bitmap这个类来帮助我们完成类似的功能,同时这个类还提供了一个sameAs的方法来比对两个Bitmap是否一致,但是遗憾的是它没有像MonkeyRunner一样提供一个百分比来指明两个图片的差异接受程度,所以为了兼容多种情况,我们需要对sameAs方法提供多个重载方法。 当然,这只是验证代码,有bug的话自己调吧。

1. 移植代码

注意一下代码只在UiAutomator上面测试通过,但是我相信Robotium是一样的,因为他们都是运行在目标安卓机器上面的,大家可以自行验证下。

package libs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class Util { public static boolean sameAs (String path1, String path2) throws FileNotFoundException {
boolean res = false;
FileInputStream fis1 = new FileInputStream(path1);
Bitmap bitmap1 = BitmapFactory.decodeStream(fis1); FileInputStream fis2 = new FileInputStream(path2);
Bitmap bitmap2 = BitmapFactory.decodeStream(fis2); res = sameAs(bitmap1,bitmap2); return res; } public static boolean sameAs (String path1, String path2,double percent) throws FileNotFoundException {
FileInputStream fis1 = new FileInputStream(path1);
Bitmap bitmap1 = BitmapFactory.decodeStream(fis1); FileInputStream fis2 = new FileInputStream(path2);
Bitmap bitmap2 = BitmapFactory.decodeStream(fis2); return sameAs(bitmap1,bitmap2,percent); } public static boolean sameAs (Bitmap bitmap1, Bitmap bitmap2, double percent) {
if(bitmap1.getHeight() != bitmap2.getHeight())
return false; if(bitmap1.getWidth() != bitmap2.getWidth())
return false; if(bitmap1.getConfig() != bitmap2.getConfig())
return false; int width = bitmap1.getWidth();
int height = bitmap2.getHeight(); int numDiffPixels = 0; for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitmap1.getPixel(x, y) != bitmap2.getPixel(x, y)) {
numDiffPixels++;
}
}
}
double numberPixels = height * width;
double diffPercent = numDiffPixels / numberPixels;
return percent <= 1.0D - diffPercent;
} public static boolean sameAs (Bitmap bmp1, Bitmap bmp2) throws FileNotFoundException {
boolean res = false; res = bmp1.sameAs(bmp2); return res;
} public static Bitmap getSubImage(String path,int x,int y,int width,int height) throws FileNotFoundException { FileInputStream fis = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(fis); Bitmap res = Bitmap.createBitmap(bitmap, x, y, width, height); return res; }
}

2. 调用代码示例

以下是UiAutomator示例,Robotium的示例请大家自行实现.
package sample.demo;

import java.io.File;
import java.io.IOException;
import libs.Util;
import android.graphics.Bitmap; import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase; public class CompareScreenshots extends UiAutomatorTestCase { public void testCompareScreenshotsNSubScrenshots() throws UiObjectNotFoundException, IOException, InterruptedException {
UiDevice device = getUiDevice();
//device.pressHome();
UiObject appNotes = new UiObject(new UiSelector().text("Notes"));
appNotes.click();
Thread.sleep(3000); String p1 = "/data/local/tmp/1.bmp";
String p2 = "/data/local/tmp/2.bmp";
File f1 = new File(p1);
if(f1.exists())
f1.delete(); File f2 = new File(p2);
if(f2.exists())
f2.delete(); device.takeScreenshot(f1);
device.takeScreenshot(f2); Bitmap sub1 = Util.getSubImage(p1, 6, 39, 474, 38);
Bitmap sub2 = Util.getSubImage(p2, 6, 39, 474, 38); boolean same = Util.sameAs(sub1, sub2, 1.0);
assertTrue(same); same = Util.sameAs(p1, p2, 0.9);
assertTrue(same); }
}
作者 自主博客 微信服务号及扫描码 CSDN
天地会珠海分舵 http://techgogogo.com 服务号:TechGoGoGo扫描码: http://blog.csdn.net/zhubaitian

移植MonkeyRunner的图片对比和获取子图功能的实现-UiAutomator/Robotium篇的更多相关文章

  1. 移植MonkeyRunner的图片对照和获取子图功能的实现-UiAutomator/Robotium篇

    依据前一篇文章<移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇>所述,由于Appium和MonkeyRunner有一个共同点--代码控制流程都是在client实 ...

  2. 移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇

    如果你的目标测试app有很多imageview组成的话,这个时候monkeyrunner的截图比较功能就体现出来了.而其他几个流行的框架如Robotium,UIAutomator以及Appium都提供 ...

  3. 移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇

    假设你的目标測试app有非常多imageview组成的话,这个时候monkeyrunner的截图比較功能就体现出来了. 而其它几个流行的框架如Robotium,UIAutomator以及Appium都 ...

  4. monkeyrunner 自动化测试 图片对比的实现

    这个功能在网上看了好多人的代码,但是总是在image.writeToFile('D:/tmp/images/black.png','png')这一句出错.查了google的API也感觉没错呀. 后来自 ...

  5. TwentyTwenty – 使用 jQuery 实现图片对比功能

    这是一款非常棒的图片对比工具,能够方便的应用到你的网站中.其基本思路是把两张图片层叠在一起,当你拖动滑竿的时候,利用 CSS clip 裁剪图片,进行形成视觉对比效果. 您可能感兴趣的相关文章 Met ...

  6. Python 实现图片对比检测

    在写测试框架的时候,需要用到图片对比的方法来判断用例执行的情况,问了一下度娘,原来可以用PIL模块处理: from PIL import Image  # 先安装Pillow, \>pip in ...

  7. Java通过图片url地址获取图片base64位字符串的两种方式

    工作中遇到通过图片的url获取图片base64位的需求.一开始是用网上的方法,通过工具类Toolkit,虽然实现的代码比较简短,不过偶尔会遇到图片转成base64位不正确的情况,至今不知道为啥. 之后 ...

  8. 使用Python的PIL模块来进行图片对比

    使用Python的PIL模块来进行图片对比 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, ...

  9. 用函数式的 Swift 实现图片转字符画的功能

    今天整理 Pocket 中待看的文章,看到这篇<Creating ASCII art in functional Swift>,讲解如何用 Swift 将图片转成 ASCII 字符.具体原 ...

随机推荐

  1. hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq

    http://acm.hdu.edu.cn/showproblem.php? pid=4691 去年夏天,更多的学校的种族称号.当时,没有后缀数组 今天将是,事实上,自己的后缀阵列组合rmq或到,但是 ...

  2. Codeforces Round #107 (Div. 2)---A. Soft Drinking

    Soft Drinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Windows PHone 8 获取硬件信息

    /// <summary> /// 获取系统信息 /// </summary> private string GetDeviceInfo() { StringBuilder s ...

  4. ASP.NET MVC常见问题解决方法

    1.页面报错: The following errors occurred while attempting to load the app. - No assembly found containi ...

  5. ACM字符串处理算法经典:字符串搜索

    语法:result=strfind(char str[],char key[]); 参数: str[]:在这个源字符串查找操作 key[]:搜索字符串.不能为空字符串 回报值:     假设查找成功. ...

  6. NSIS:应用软件自动升级功能的探索与实践

    原文 NSIS:应用软件自动升级功能的探索与实践 记得以前轻狂曾分享过使用第三方软件实现应用软件自动升级功能 (详细http://www.flighty.cn/html/soft/20110106_1 ...

  7. php学习之道:mysql SELECT FOUND_ROWS()与COUNT(*)使用方法差别

    在mysql中 FOUND_ROWS()与COUNT(*)都能够统计记录.假设都一样为什么会有两个这种函数呢.以下我来介绍SELECT FOUND_ROWS()与COUNT(*)使用方法差别 SELE ...

  8. myeclipse解决JSP文件script调整背景颜色

    1进口MyEclipse主题后,打开jsp要么html文件,jsvascript部分原因遭遇了一层白色的.闪避这个时候.症状,如下面: watermark/2/text/aHR0cDovL2Jsb2c ...

  9. CocoaChina 第四个测试

    1. iOS同意近期本地通知数量最大为多少? A.64 B.32 C.128 D.16 2. int x = 1; int y = 2; int z = x^y*y; NSLog(@"%d& ...

  10. 每天进步一点点--&gt;功能fseek() 使用方法

    在阅读代码时,遇到了非常早之前用过的fseek(),非常久没实用了.有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头.须要包括头文件stdio.h fseek   函数名: fsee ...