Search in sources :

Example 1 with PixelMatcherResult

use of org.testfx.service.support.PixelMatcherResult in project JavaFXLibrary by eficode.

the class Verifiers method imagesShouldNotMatch.

@RobotKeyword("Fails if images are too similar\n\n" + "``image1`` is an _Object:Image_ for the first comparable image.\n\n" + "``image2`` is an _Object:Image_ for the second comparable image.\n\n" + "``percentage`` the percentage of pixels that should not match, defaults to 100.\n\n" + "This keyword can be coupled with e.g. `Capture Image` -keyword.")
@ArgumentNames({ "image1", "image2", "percentage=100" })
public void imagesShouldNotMatch(Image image1, Image image2, double percentage) {
    robotLog("INFO", "Checking if " + percentage + "% of " + image1 + " differs with " + image2);
    if (image1.getHeight() != image2.getHeight() || image1.getWidth() != image2.getWidth())
        throw new JavaFXLibraryNonFatalException("Images must be same size to compare: Image1 is " + (int) image1.getWidth() + "x" + (int) image1.getHeight() + " and Image2 is " + (int) image2.getWidth() + "x" + (int) image2.getHeight());
    PixelMatcherResult result = robotContext.getCaptureSupport().matchImages(image1, image2, new PixelMatcherRgb());
    int nonSharedPixels = (int) (result.getNonMatchFactor() * 100);
    robotLog("INFO", "Matching pixels: " + nonSharedPixels + "%");
    if (nonSharedPixels < percentage)
        throw new JavaFXLibraryNonFatalException("Images are too similar - Expected at least " + (int) percentage + "% " + "difference, got " + nonSharedPixels + "%");
}
Also used : JavaFXLibraryNonFatalException(javafxlibrary.exceptions.JavaFXLibraryNonFatalException) PixelMatcherResult(org.testfx.service.support.PixelMatcherResult) PixelMatcherRgb(org.testfx.service.support.impl.PixelMatcherRgb)

Example 2 with PixelMatcherResult

use of org.testfx.service.support.PixelMatcherResult in project JavaFXLibrary by eficode.

the class Verifiers method imagesShouldMatch.

@RobotKeyword("Fails if images are not similar enough\n\n" + "``image1`` is an _Object:Image_ for the first comparable image.\n\n" + "``image2`` is an _Object:Image_ for the second comparable image.\n\n" + "``percentage`` the percentage of pixels that should match, defaults to 100.\n\n" + "This keyword can be coupled with e.g. `Capture Image` -keyword.")
@ArgumentNames({ "image1", "image2", "percentage=100" })
public void imagesShouldMatch(Image image1, Image image2, double percentage) {
    robotLog("INFO", "Checking if " + percentage + "% of " + image1 + " matches with " + image2);
    if (image1.getHeight() != image2.getHeight() || image1.getWidth() != image2.getWidth())
        throw new JavaFXLibraryNonFatalException("Images must be same size to compare: Image1 is " + (int) image1.getWidth() + "x" + (int) image1.getHeight() + " and Image2 is " + (int) image2.getWidth() + "x" + (int) image2.getHeight());
    PixelMatcherResult result = robotContext.getCaptureSupport().matchImages(image1, image2, new PixelMatcherRgb());
    int sharedPixels = (int) (result.getMatchFactor() * 100);
    robotLog("INFO", "Matching pixels: " + sharedPixels + "%");
    if (sharedPixels < percentage)
        throw new JavaFXLibraryNonFatalException("Images do not match - Expected at least " + (int) percentage + "% " + "similarity, got " + sharedPixels + "%");
}
Also used : JavaFXLibraryNonFatalException(javafxlibrary.exceptions.JavaFXLibraryNonFatalException) PixelMatcherResult(org.testfx.service.support.PixelMatcherResult) PixelMatcherRgb(org.testfx.service.support.impl.PixelMatcherRgb)

Example 3 with PixelMatcherResult

use of org.testfx.service.support.PixelMatcherResult in project TestFX by TestFX.

the class CaptureSupportImplTest method match_images.

@Test
public void match_images() throws IOException {
    // given:
    Image image0 = capturer.loadImage(resourcePath(getClass(), "res/acme-login-expected.png"));
    Image image1 = capturer.loadImage(resourcePath(getClass(), "res/acme-login-actual.png"));
    // when:
    PixelMatcherRgb matcher = new PixelMatcherRgb();
    PixelMatcherResult result = capturer.matchImages(image0, image1, matcher);
    Path differenceImagePath = testFolder.newFile("acme-login-difference.png").toPath();
    capturer.saveImage(result.getMatchImage(), differenceImagePath);
    // then:
    assertThat(differenceImagePath.toFile().exists(), is(true));
    verifyThat(result.getNonMatchPixels(), equalTo(2191L));
    verifyThat(result.getNonMatchFactor(), closeTo(0.02, /* tolerance */
    0.01));
}
Also used : Path(java.nio.file.Path) PixelMatcherResult(org.testfx.service.support.PixelMatcherResult) Image(javafx.scene.image.Image) Test(org.junit.Test)

Example 4 with PixelMatcherResult

use of org.testfx.service.support.PixelMatcherResult in project TestFX by TestFX.

the class CaptureSupportImplTest method match_images_from_scene.

@Test
public void match_images_from_scene() throws IOException {
    // given:
    interact(() -> primaryStage.getScene().lookup("#loginButton").requestFocus());
    Image image0 = capturer.captureNode(primaryStage.getScene().getRoot());
    // and:
    interact(() -> primaryStage.getScene().lookup("#username").requestFocus());
    Image image1 = capturer.captureNode(primaryStage.getScene().getRoot());
    // when:
    PixelMatcherRgb matcher = new PixelMatcherRgb();
    PixelMatcherResult result = capturer.matchImages(image0, image1, matcher);
    Path differenceImagePath = testFolder.newFile("acme-login-difference.png").toPath();
    capturer.saveImage(result.getMatchImage(), differenceImagePath);
    // then:
    assertThat(differenceImagePath.toFile().exists(), is(true));
    assertThat(result.getNonMatchPixels() > 1900, is(true));
    verifyThat(result.getNonMatchFactor(), closeTo(0.02, /* tolerance */
    0.01));
}
Also used : Path(java.nio.file.Path) PixelMatcherResult(org.testfx.service.support.PixelMatcherResult) Image(javafx.scene.image.Image) Test(org.junit.Test)

Example 5 with PixelMatcherResult

use of org.testfx.service.support.PixelMatcherResult in project TestFX by TestFX.

the class PixelMatcherBase method match.

// ---------------------------------------------------------------------------------------------
// METHODS.
// ---------------------------------------------------------------------------------------------
@Override
public PixelMatcherResult match(Image image0, Image image1) {
    WritableImage matchImage = createEmptyMatchImage(image0, image1);
    int imageWidth = (int) matchImage.getWidth();
    int imageHeight = (int) matchImage.getHeight();
    long matchPixels = 0L;
    long totalPixels = imageWidth * imageHeight;
    for (int imageY = 0; imageY < imageHeight; imageY += 1) {
        for (int imageX = 0; imageX < imageWidth; imageX += 1) {
            Color color0 = readPixel(image0, imageX, imageY);
            Color color1 = readPixel(image1, imageX, imageY);
            boolean areColorsMatching = matchColors(color0, color1);
            if (areColorsMatching) {
                matchPixels += 1;
                Color matchColor = createMatchColor(color0, color1);
                writePixel(matchImage, imageX, imageY, matchColor);
            } else {
                Color nonMatchColor = createNonMatchColor(color0, color1);
                writePixel(matchImage, imageX, imageY, nonMatchColor);
            }
        }
    }
    return new PixelMatcherResult(matchImage, matchPixels, totalPixels);
}
Also used : WritableImage(javafx.scene.image.WritableImage) Color(javafx.scene.paint.Color) PixelMatcherResult(org.testfx.service.support.PixelMatcherResult)

Aggregations

PixelMatcherResult (org.testfx.service.support.PixelMatcherResult)5 Path (java.nio.file.Path)2 Image (javafx.scene.image.Image)2 JavaFXLibraryNonFatalException (javafxlibrary.exceptions.JavaFXLibraryNonFatalException)2 Test (org.junit.Test)2 PixelMatcherRgb (org.testfx.service.support.impl.PixelMatcherRgb)2 WritableImage (javafx.scene.image.WritableImage)1 Color (javafx.scene.paint.Color)1