Тестирование верстки путем сравнение скриншотов в aShot

Добрый день.

Делаю сравнение заранее сохраненного фрагмента файла и текущего скриншота страницы:

 Class<? extends DiffMarkupPolicy> diffStorageClass;

        ImageDiffer imageDiffer = new ImageDiffer()
                .withColorDistortion(10)
                .withDiffMarkupPolicy(diffStorageClass.newInstance() // тут NullPointerException 
                        .withDiffColor(Color.RED));

        ImageDiff diff = imageDiffer.makeDiff(
                new AShot().takeScreenshot(ManageBrowser.getDriver()).getImage(),
                loadImage("./src/main/resources/images/table_th.png")
        );

        String path = screenPath + "/test.png";
        File outputFile = new File(path);
        try {
//            ImageIO.write(shot.getImage(), "png", outputFile);
            ImageIO.write(diff.getDiffImage(), "png", outputFile);
        }
        catch (IOException e){e.printStackTrace();}

пример брал отсюда https://github.com/yandex-qatools/ashot/blob/master/src/test/java/ru/yandex/qatools/elementscompare/tests/DifferTest.java

Возникает java.lang.NullPointerException в строке

.withDiffMarkupPolicy(diffStorageClass.newInstance()

не ясно, как инициализировать переменную в строке

Class<? extends DiffMarkupPolicy> diffStorageClass;

Конечно возникает. Вы его объявили, но не инициализировали. Посмотрите внимательнее пример, как там передаётся параметр.

В итоге получил предварительно рабочую функцию:

    @Test
    public void testDiffImg() throws Exception {
        WebElement myWebElement = findElement("select_y", byId);

        ImageDiffer imageDiffer = new ImageDiffer()
//                .withColorDistortion(11119017)
                .withDiffMarkupPolicy(new PointsMarkupPolicy()
                        .withDiffColor(Color.RED));

        ImageDiff diff = imageDiffer.makeDiff(
                loadImage("./src/main/resources/images/select_y.png"),
                new AShot()
                        .takeScreenshot(driver, myWebElement).getImage()
        );

        String path = screenPath + "/test2.png";
        File outputFile = new File(path);
        try {
            ImageIO.write(diff.getMarkedImage(), "png", new File(screenPath + "/getMarkedImage2.png"));
        }
        catch (IOException e){e.printStackTrace();}

        assertThat(diff.hasDiff(), is(false));
    }

может кому-то пригодится

1 лайк