移植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 字符.具体原 ...
随机推荐
- KVO
其原理探究
什么是KVO ? KVO这是Key-Value Observing,它提供了一种机制,指定的对象的属性被改动后,则对象就会接受到通知. 简单的说就是每次指定的被观察的对象的属性被改动后.KVO就会自己 ...
- PHP实现协同程序
于server其中编程.为了实现异步.通常情况下,需要回调.比例如下面的代码 function send($value) { $data = process($value); onReceive($d ...
- Visual Studio Team Services使用教程--Readers tfs组成员添加
- WPF学习(3)布局
今天我们来说说WPF的布局.我们知道WinForm的布局主要是采用基于坐标的方式,当窗口内容发生变化时,里面的控件不会随之动态调整,这就造成了一个很不好的用户体验.而WPF为了避免这个缺点,采用了基于 ...
- ios save image to album
- (void)savePhotoToAlbum { ZoomScrollView *zoomScrollView = (ZoomScrollView*)[self.scrollView viewWi ...
- curl 要么 file_get_contents 获得授权页面的方法的必要性
今天,需要工作,需要使用 curl / file_get_contents 获得授权的必要性(Authorization)的页面内容.解决后写了这篇文章分享给大家. php curl 扩展,可以在se ...
- BZOJ 2947 Poi2000 促销 set
标题效果:特定n天,首先插入一些每天.然后去掉最高值和最低值,要付出最大的值-至少值价格.乞讨n总天数支付的价格 堆/段树/平衡树光秃秃的标题 #include <set> #includ ...
- mysql函数二
四.条件推断函数 1.if(expr,v1,v2)函数:成立返回结果v1,否则结果v2 例:select id,if(grade>=60,'pass','fail') from t; 2.IFN ...
- 【C语言】reverse_string(char * string)(递归)
递归reverse_string(char * string)性能. 逆转 原始字符串 更改 相反,打印出的. /* 编写一个函数reverse_string(char * string)(递归实现) ...
- 【IOS 开发】Object-C 入门 Xcode 环境具体解释
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/38424965 一. Xcode 环境安装 与 project ...
