Transplantation of MonkeyRunner's image comparison and implementation of Sub-graph acquisition functions-UiAutomator/Robotium,

Source: Internet
Author: User
Tags appium

Transplantation of MonkeyRunner's image comparison and implementation of Sub-graph acquisition functions-UiAutomator/Robotium,

According to the previous article titled image comparison for porting MonkeyRunner and implementation for acquiring subgraphs-Appium, because Appium and MonkeyRunner have one thing in common-the code control process is implemented on the client. Therefore, it is very easy to transplant the image comparison and subgraph acquisition functions implemented by MonkeyRunner on the PC end to the Appium that runs on the PC end, however, it is another thing for Robotium and UiAutomator running on the server.

In the Android sdk, the following two classes are not supported for MonkeyRunner to obtain subgraph and image comparison. in simple words, the java. awt library is not supported:

import java.awt.image.BufferedImage;import javax.imageio.ImageIO;
However, the Android sdk has the Bitmap class to help us complete similar functions. At the same time, this class also provides a sameAs method to compare whether the two bitmaps are consistent, unfortunately, it does not provide a percentage like MonkeyRunner to indicate the differences between two images. Therefore, to be compatible with multiple situations, we need to provide multiple overload methods for the sameAs method.

Of course, this is just the verification code. If there is a bug, call it yourself.

1. Port the code

Note that the Code passes the test only on UiAutomator, but I believe that robotify is the same, because they all run on the target Android machine and can be verified by yourself.

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. The following is an example of the Call code: UiAutomator. You can implement this example by yourself.

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); }}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.