您好,登錄后才能下訂單哦!
根據(jù)前一篇文章《移植MonkeyRunner的圖片對(duì)比和獲取子圖功能的實(shí)現(xiàn)-Appium篇》 所述,因?yàn)锳ppium和MonkeyRunner有一個(gè)共同點(diǎn)--代碼控制流程都是在客戶(hù)端實(shí)現(xiàn)的。所以要把MonkeyRunner在PC端實(shí)現(xiàn)的圖 片比對(duì)和獲取子圖功能移植到同樣是在PC端運(yùn)行的Appium是很容易的事情,但是對(duì)于在服務(wù)器端運(yùn)行的Robotium和UiAutomator就是另 外一回事了。
因?yàn)樵贏ndroid的sdk中,MonkeyRunner獲取子圖和圖片比對(duì)需要用到的以下兩個(gè)類(lèi)是沒(méi)有支持的,簡(jiǎn)單來(lái)說(shuō)就是java.awt這個(gè)庫(kù)是不支持的:
import java.awt.p_w_picpath.BufferedImage; import javax.p_w_picpathio.ImageIO;
但是在Android的sdk中有Bitmap這個(gè)類(lèi)來(lái)幫助我們完成類(lèi)似的功能,同時(shí)這個(gè)類(lèi)還提供了一個(gè)sameAs的方法來(lái)比對(duì)兩個(gè)Bitmap是否一 致,但是遺憾的是它沒(méi)有像MonkeyRunner一樣提供一個(gè)百分比來(lái)指明兩個(gè)圖片的差異接受程度,所以為了兼容多種情況,我們需要對(duì)sameAs方法 提供多個(gè)重載方法。
當(dāng)然,這只是驗(yàn)證代碼,有bug的話自己調(diào)吧。
注意一下代碼只在UiAutomator上面測(cè)試通過(guò),但是我相信Robotium是一樣的,因?yàn)樗麄兌际沁\(yùn)行在目標(biāo)安卓機(jī)器上面的,大家可以自行驗(yàn)證下。
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 path2, String path3) throws FileNotFoundException { boolean res = false; FileInputStream fis1 = new FileInputStream(path2); Bitmap bitmap1 = BitmapFactory.decodeStream(fis1); FileInputStream fis2 = new FileInputStream(path3); Bitmap bitmap2 = BitmapFactory.decodeStream(fis2); res = sameAs(bitmap1,bitmap2); return res; } public static boolean sameAs (String path2, String path3,double percent) throws FileNotFoundException { FileInputStream fis1 = new FileInputStream(path2); Bitmap bitmap1 = BitmapFactory.decodeStream(fis1); FileInputStream fis2 = new FileInputStream(path3); 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; } }
以下是UiAutomator示例,Robotium的示例請(qǐng)大家自行實(shí)現(xiàn).
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); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。