您好,登錄后才能下訂單哦!
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就可能需要大改動,要重現實現了。
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 掃描碼:
| 向AI問一下細節 免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。 猜你喜歡最新資訊相關推薦相關標簽AI
助 手
垣曲县|
中阳县|
彰化县|
青神县|
揭阳市|
铜鼓县|
浠水县|
邹平县|
徐水县|
庆城县|
栾川县|
安平县|
湟中县|
利川市|
抚松县|
隆昌县|
安国市|
桓台县|
海口市|
久治县|
高阳县|
宝应县|
金溪县|
昌乐县|
拉孜县|
桐梓县|
三台县|
中江县|
天柱县|
思南县|
安溪县|
明光市|
阜新|
施甸县|
阳东县|
九龙坡区|
安溪县|
宁城县|
贺州市|
綦江县|
高阳县|
|