UI自动化图像对比方法

最近在做移动端的UI自动化,需要用截图对比来验证当前状态,具体是以下的场景:

  1. 验证页面是否已经完全加载完毕,再进行下一步操作
  2. 验证结果页面是否符合预期

过程中遇到了两个问题:

  1. 头部工具条有时间,每次截图都不一样,需要预先截掉。
    解决方法:用例初始化的时候,获取顶部工具条的坐标尺寸,macaca截图后自己再进行二次裁剪
public int top_toolbar_height;
public int device_width;
public int device_height;
JSONObject toolbar = JSON.parseObject(driver.getRect(HomePageUI.TOP_TOOLBAR).toString());
top_toolbar_height = toolbar.getInteger("height");
JSONObject windowSize = driver.getWindowSize();
device_width = windowSize.getInteger("width");
device_height = windowSize.getInteger("height");
public void cut_top_toolbar(String srcpath, String subpath) throws IOException {//裁剪方法
        FileInputStream is = null;
        ImageInputStream iis = null;
        try {
            is = new FileInputStream(srcpath); //读取原始图片
            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("png"); //ImageReader声称能够解码指定格式
            ImageReader reader = it.next();
            iis = ImageIO.createImageInputStream(is); //获取图片流
            reader.setInput(iis, true); //将iis标记为true(只向前搜索)意味着包含在输入源中的图像将只按顺序读取
            ImageReadParam param = reader.getDefaultReadParam(); //指定如何在输入时从 Java Image I/O框架的上下文中的流转换一幅图像或一组图像
            Rectangle rect = new Rectangle(0, top_toolbar_height, device_width, device_height - top_toolbar_height); //定义空间中的一个区域
            param.setSourceRegion(rect); //提供一个 BufferedImage,将其用作解码像素数据的目标。
            BufferedImage bi = reader.read(0, param); //读取索引imageIndex指定的对象
            ImageIO.write(bi, "png", new File(subpath)); //保存新图片
        } finally {
            if (is != null)
                is.close();
            if (iis != null)
                iis.close();
        }
    }
  1. 对比原本是打算直接使用MD5的方法,但发现同一个session下截的两张图MD5值是一致的,但不同session下截的不一致,就算把手机亮度固定也不行。
    解决方法:需要先把图片进行灰度处理,避免了色彩影响,md5就一致了
public void grayImage(String fileName) throws IOException{
        File file = new File(Config.SCREEN_SHOT_PATH + java.io.File.separator + fileName);
        BufferedImage image = ImageIO.read(file);

        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_GRAY
        for(int i= 0 ; i < width ; i++){
            for(int j = 0 ; j < height; j++){
                int rgb = image.getRGB(i, j);
                grayImage.setRGB(i, j, rgb);
            }
        }

        File newFile = new File(Config.SCREEN_SHOT_PATH + java.io.File.separator + fileName);
        ImageIO.write(grayImage, "png", newFile);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 当前一切UI自动化都是建立在selenium2的API基础上的,最底层都是调用的模式。 UI自动化主要的体现应该在...
    我为峰2014阅读 11,064评论 0 7
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,793评论 1 32
  • 1.abstract class 和interface 有什么区别? 抽象类可以有构造方法 接口不行 抽象类可以有...
    sunnysans阅读 4,340评论 0 1
  • 本文整理自MIN飞翔博客 [1] 1. 概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或...
    HoyaWhite阅读 7,545评论 2 20
  • 最近兴起的各种波浪,挺吓人。禁不住要问:世上像样点的男人中,到底有没有能管得住下半身的? 女人也不得了。进了婚姻,...
    3e05348ffdd4阅读 1,651评论 0 4