湿货分享:日志记录和图片对比

基于移动端自动化测试

Posted by WS on August 28, 2019

由于用的iframe嵌入ppt文档, 网速不佳的话可能初始化有一点点慢, 喝杯卡布奇诺等等☕️

项目代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * Compare Two Images
 * 图片相似度对比
 * @author Graham.Liu
 * @return res = 1.0:图片一致,res != 1.0:图片不一致
*/
@SuppressWarnings("static-access")
public Double compareImage(String imagePath1, String imagePath2) {
  OpenCV openCV = new OpenCV();
  openCV.loadLocally();
  Mat image1 = Imgcodecs.imread(imagePath1,Imgcodecs.IMREAD_GRAYSCALE);
  Mat image2 = Imgcodecs.imread(imagePath2,Imgcodecs.IMREAD_GRAYSCALE);
  Mat imageHist1 = new Mat();
  Mat imageHist2 = new Mat();

  // 颜色范围
  MatOfFloat ranges = new MatOfFloat(0f, 256f);
  // 直方图大小,越大匹配越精准,速度也会越慢
  MatOfInt histSize = new MatOfInt(1000);

  // 转换直方图
  Imgproc.calcHist(Arrays.asList(image1), new MatOfInt(0), new Mat(), imageHist1, histSize, ranges);
  Imgproc.calcHist(Arrays.asList(image2), new MatOfInt(0), new Mat(), imageHist2, histSize, ranges);

  // 对比,通过CORREL相关系数
  double res = Imgproc.compareHist(imageHist1, imageHist2, Imgproc.CV_COMP_CORREL);
  return res;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 * 像素点颜色提取, 判断背景/前景颜色, 是否符合预期输入颜色值
 * @param isBackgroundColor 是否判断背景色--true,判断前景文字颜色--false
 * @param ExpectColorImage 期望颜色值,一般是期望颜色图片路径
 * @param ActualImage 实际图片路径
 * @author Graham.liu
*/ 
 public boolean verifyExpectColorExist(String isBackgroundColor,String ExpectColorName,String ActualImage) {
      BufferedImage biAC = null;
      File fileImageAC  = new File(ActualImage);
      double colorProportion;
      int ExpectColorCount = 0;
      int[] rgb = new int[3];
      int[] colorRange = new int[6];
     
      try {
          biAC = ImageIO.read(fileImageAC);
      } catch (Exception e) {
          System.out.println(e);
      }
      int widthAC = biAC.getWidth();
      int heightAC = biAC.getHeight();
      int minxAC = biAC.getMinX();
      int minyAC = biAC.getMinY();   
     
      //获取期望图片RGB的范围值
      colorRange = GetRGBRangeEX(ExpectColorName);

      //计算期望颜色的数量                   
      for (int i = minxAC; i < widthAC; i++) {
          for (int j = minyAC; j < heightAC; j++) {
              int pixeAC = biAC.getRGB(i, j); 
              rgb[0] = (pixeAC & 0xff0000) >> 16; //Red
              rgb[1] = (pixeAC & 0xff00) >> 8; //Green
              rgb[2] = (pixeAC & 0xff); //Blue
              if((rgb[0]>=colorRange[1] && rgb[0]<=colorRange[0]) && (rgb[1]>=colorRange[3] && rgb[1]<=colorRange[2]) && (rgb[2]>=colorRange[5] && rgb[2]<=colorRange[4])){
                  ExpectColorCount++;
             }
          }
      }
      System.out.println("ExpectColorCount = " + ExpectColorCount);
  
     
      //判断计算预期颜色值占总像素的比例
      if(!ExpectColorName.equals(null)) {
          if(ExpectColorCount > 0) {
              colorProportion = (double)(ExpectColorCount)/(double)(widthAC*heightAC);
              System.out.println("AllColorCount = " + widthAC*heightAC);
              System.out.println("colorProportion = " + colorProportion);
                         
          }else {
              report.updateTestLog("Verify expect color diaplay","Expect color is not diaplayed", Status.FAIL);
              return false;
          }
      }else {
          report.updateTestLog("Please check expect color value","Expect color is null", Status.FAIL);
          return false;
      }
  
      //根据是否背景色判断
      if(isBackgroundColor.toLowerCase().contentEquals("true")){
          if(colorProportion >= 0.5 && colorProportion <= 1) {
              report.updateTestLog("Verify background color diaplay","Background color is diaplayed as " + ExpectColorName, Status.PASS);
              return true;
          }else {
              report.updateTestLog("Verify background color diaplay","Background color is not diaplayed as " + ExpectColorName, Status.FAIL);
              return false;
          }
      }else {
          if(colorProportion < 0.5 && colorProportion >= 0) {
              report.updateTestLog("Verify words color diaplay","Words color is diaplayed as " + ExpectColorName, Status.PASS);
              return true;
          }else {
              report.updateTestLog("Verify words color diaplay","Words color is not diaplayed as " + ExpectColorName, Status.FAIL);
              return false;
          }
      }      
 }

%