移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇
如果你的目标测试app有很多imageview组成的话,这个时候monkeyrunner的截图比较功能就体现出来了。而其他几个流行的框架如Robotium,UIAutomator以及Appium都提供了截图,但少了两个功能:
- 获取子图
- 图片比较
1. 移植代码
public static boolean sameAs(BufferedImage myImage,BufferedImage otherImage, double percent)
{
//BufferedImage otherImage = other.getBufferedImage();
//BufferedImage myImage = getBufferedImage(); if (otherImage.getWidth() != myImage.getWidth()) {
return false;
}
if (otherImage.getHeight() != myImage.getHeight()) {
return false;
} int[] otherPixel = new int[1];
int[] myPixel = new int[1]; int width = myImage.getWidth();
int height = myImage.getHeight(); int numDiffPixels = 0; for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (myImage.getRGB(x, y) != otherImage.getRGB(x, y)) {
numDiffPixels++;
}
}
}
double numberPixels = height * width;
double diffPercent = numDiffPixels / numberPixels;
return percent <= 1.0D - diffPercent;
} public static BufferedImage getSubImage(BufferedImage image,int x, int y, int w, int h)
{
return image.getSubimage(x, y, w, h);
} public static BufferedImage getImageFromFile(File f) { BufferedImage img = null; try {
img = ImageIO.read(f); } catch (IOException e) {
//if failed, then copy it to local path for later check:TBD
//FileUtils.copyFile(f, new File(p1));
e.printStackTrace();
System.exit(1);
}
return img;
}
这里就不多描述了,基本上就是基于MonkeyRunner做轻微的修改,所以叫做移植。而UiAutomator就可能需要大改动,要重现实现了。
2. 客户端调用代码举例
package sample.demo.AppiumDemo; import static org.junit.Assert.*; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL; import javax.imageio.ImageIO; import libs.Util;
import io.appium.java_client.android.AndroidDriver; import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities; public class CompareScreenShots { private AndroidDriver driver; @Before
public void setUp() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Android");
cap.setCapability("appPackage", "com.example.android.notepad");
cap.setCapability("appActivity", ".NotesList"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap);
} @After
public void tearDown() throws Exception {
driver.quit();
} @Test
public void compareScreenAndSubScreen() throws InterruptedException, IOException{
Thread.sleep(2000); WebElement el = driver.findElement(By.className("android.widget.ListView")).findElement(By.name("Note1"));
el.click();
Thread.sleep(1000);
String p1 = "C:/1";
String p2 = "C:/2"; File f2 = new File(p2); File f1 = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f1, new File(p1)); BufferedImage img1 = Util.getImageFromFile(f1); f2 = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f2, new File(p2));
BufferedImage img2 = Util.getImageFromFile(f2); Boolean same = Util.sameAs(img1, img2, 0.9);
assertTrue(same); BufferedImage subImg1 = Util.getSubImage(img1, 6, 39, 474, 38);
BufferedImage subImg2 = Util.getSubImage(img1, 6, 39, 474, 38);
same = Util.sameAs(subImg1, subImg2, 1); File f3 = new File("c:/sub-1.png");
ImageIO.write(subImg1, "PNG", f3); File f4 = new File("c:/sub-2.png");
ImageIO.write(subImg1, "PNG", f4); } }
也不多解析了,没有什么特别的东西。
作者 | 自主博客 | 微信服务号及扫描码 | CSDN |
天地会珠海分舵 | http://techgogogo.com | 服务号:TechGoGoGo扫描码:![]() |
http://blog.csdn.net/zhubaitian |
移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇的更多相关文章
- 移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇
假设你的目标測试app有非常多imageview组成的话,这个时候monkeyrunner的截图比較功能就体现出来了. 而其它几个流行的框架如Robotium,UIAutomator以及Appium都 ...
- 移植MonkeyRunner的图片对比和获取子图功能的实现-UiAutomator/Robotium篇
根据前一篇文章<移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇>所述,因为Appium和MonkeyRunner有一个共同点--代码控制流程都是在客户端实现的. ...
- 移植MonkeyRunner的图片对照和获取子图功能的实现-UiAutomator/Robotium篇
依据前一篇文章<移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇>所述,由于Appium和MonkeyRunner有一个共同点--代码控制流程都是在client实 ...
- monkeyrunner 自动化测试 图片对比的实现
这个功能在网上看了好多人的代码,但是总是在image.writeToFile('D:/tmp/images/black.png','png')这一句出错.查了google的API也感觉没错呀. 后来自 ...
- TwentyTwenty – 使用 jQuery 实现图片对比功能
这是一款非常棒的图片对比工具,能够方便的应用到你的网站中.其基本思路是把两张图片层叠在一起,当你拖动滑竿的时候,利用 CSS clip 裁剪图片,进行形成视觉对比效果. 您可能感兴趣的相关文章 Met ...
- Python 实现图片对比检测
在写测试框架的时候,需要用到图片对比的方法来判断用例执行的情况,问了一下度娘,原来可以用PIL模块处理: from PIL import Image # 先安装Pillow, \>pip in ...
- Java通过图片url地址获取图片base64位字符串的两种方式
工作中遇到通过图片的url获取图片base64位的需求.一开始是用网上的方法,通过工具类Toolkit,虽然实现的代码比较简短,不过偶尔会遇到图片转成base64位不正确的情况,至今不知道为啥. 之后 ...
- 使用Python的PIL模块来进行图片对比
使用Python的PIL模块来进行图片对比 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, ...
- 用函数式的 Swift 实现图片转字符画的功能
今天整理 Pocket 中待看的文章,看到这篇<Creating ASCII art in functional Swift>,讲解如何用 Swift 将图片转成 ASCII 字符.具体原 ...
随机推荐
- LINQ之路(3):LINQ扩展
本篇文章将从三个方面来进行LINQ扩展的阐述:扩展查询操作符.自定义查询操作符和简单模拟LINQ to SQL. 1.扩展查询操作符 在实际的使用过程中,Enumerable或Queryable中的扩 ...
- Ubuntu14.04设备JDK
1.设备JDK 打开命令直插式工具.输入以下三个命令: sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo ap ...
- Android studio 中国的垃圾问题解决
为了获得良好的刚安装Android studio, 实例importproject时刻,你会发现很多中国的文件夹显示异常.例如下面的附图: 为什么会出现这个问题呢,事实上原因非常easy,由于Andr ...
- 提升Mac os x 10.10+xcode6.1之后,Cocoapods发生故障的解决方案
提升Mac OS X 10.10+Xcode 6.1之后.Cocoapods图书馆管理也依赖于相应升级.现在最新的Release版本号是 0.34.在之前的版本号.当数据库更新和管理,你会遇到一个错误 ...
- curl 要么 file_get_contents 获得授权页面的方法的必要性
今天,需要工作,需要使用 curl / file_get_contents 获得授权的必要性(Authorization)的页面内容.解决后写了这篇文章分享给大家. php curl 扩展,可以在se ...
- 使用log4j日志-配置载入问题
1.在eclipse中,把log4j.properties放在类路径下,在项目启动时就会自己主动载入. 2.在idea中.把log4j.properties放在类路径下,可是项目启动时不能直接载入(原 ...
- Benchmark与Profiler---性能调优得力助手
转载请注明出处:http://blog.csdn.net/gaoyanjie55/article/details/34981077 性能优化.它是一种诊断性能瓶颈,能问题点进行优化的过程.前两天听完s ...
- requirejs实现模块化
使用requirejs实现模块化编程 > 序言 - -# 公司大了,业务多了,前端代码量也逐渐增大,我们渐渐的依赖js实现的交互越来越多,长期以来会导致我们的代码维护越来越困难,所以依赖的插件也 ...
- linux tar.gz zip 减压 压缩命令
http://apps.hi.baidu.com/share/detail/37384818 download ADT link http://dl.google.com/android/ADT-0. ...
- OCP读书笔记(23) - 题库(ExamC)
200.Which operation requires that you create an auxiliary instance manually before executing the ope ...