Jacobvu84 / serenity-pageobject-junit-webdriver

4 stars 1 forks source link

Bài #46: So sánh hình ảnh #50

Open Jacobvu84 opened 5 years ago

Jacobvu84 commented 5 years ago

Nhiều trường hợp chúng ta muốn kiểm tra hình ảnh sau khi được upload lên có đúng không như avatar chẳng hạn.

Trong video này hướng dẫn bạn cách so sánh hình ảnh để kiểm tra độ chính xác.

Jacobvu84 commented 5 years ago

CompareGraph

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class CompareGraph {

    public enum Result {
        Matched, SizeMismatch, PixelMismatch
    };

    public static Result CompareImage(String baseFile, String actualFile) {

        Result compareResult = Result.PixelMismatch;
        Image baseImage = Toolkit.getDefaultToolkit().getImage(baseFile);
        Image actualImage = Toolkit.getDefaultToolkit().getImage(actualFile);
        try {
            PixelGrabber baseImageGrab = new PixelGrabber(baseImage, 0, 0, -1, -1, false);
            PixelGrabber actualImageGrab = new PixelGrabber(actualImage, 0, 0, -1, -1, false);

            int[] baseImageData = null;
            int[] actualImageData = null;

            if (baseImageGrab.grabPixels()) {
                int width = baseImageGrab.getWidth();
                int height = baseImageGrab.getHeight();
                baseImageData = new int[width * height];
                baseImageData = (int[]) baseImageGrab.getPixels();
            }

            if (actualImageGrab.grabPixels()) {
                int width = actualImageGrab.getWidth();
                int height = actualImageGrab.getHeight();
                actualImageData = new int[width * height];
                actualImageData = (int[]) actualImageGrab.getPixels();
            }

            System.out.println(baseImageGrab.getHeight() + "<>" + actualImageGrab.getHeight());
            System.out.println(baseImageGrab.getWidth() + "<>" + actualImageGrab.getWidth());

            if ((baseImageGrab.getHeight() != actualImageGrab.getHeight())
                    || (baseImageGrab.getWidth() != actualImageGrab.getWidth()))
                compareResult = Result.SizeMismatch;
            else if (java.util.Arrays.equals(baseImageData, actualImageData))
                compareResult = Result.Matched;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return compareResult;
    }
}
Jacobvu84 commented 5 years ago

Cách dùng. Trong phần Steps

@Step
public void capture_and_save_it(String element, String pathStorge) {
    try {
            FileUtils.copyFile(captureElementBitmap(element), new File(pathStorge));
    } catch (IOException e) {e.printStackTrace();
    } catch (Exception e) {e.printStackTrace();
    }
}
Jacobvu84 commented 5 years ago
    @Step
    public void actual_image_and_expected_image_should_be_similar(String expectImg, String actualImge) {
        assertThat(CompareGraph.Result.Matched, equalTo(CompareGraph.CompareImage(expectImg, actualImge)));
    }