Class SingltonBrowserImage
- java.lang.Object
-
- com.github.loyada.jdollarx.singlebrowser.SingltonBrowserImage
-
public class SingltonBrowserImage extends Object
Capturing and asserting the image (screenshot) of a Path element. It supports both canvas and a standard element image. It supports both accurate and fuzzy assertion. It provides utility functions to display an element in a separate window.
-
-
Constructor Summary
Constructors Constructor Description SingltonBrowserImage(Path el)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
assertCanvasImageIsEqualToExpected(InputStream expectedImageInput)
Verify that the canvas image is pixel-perfectvoid
assertImageIsEqualToExpected(InputStream expectedImageInput)
Verify that the element's image is pixel-perfectstatic void
assertImageIsEqualToExpected(InputStream actualImage, InputStream expectedImageInput)
Verify that the element's image is pixel-perfectvoid
assertImageIsEqualToExpectedNoScaling(InputStream expectedImageInput)
Verify that the element's image is pixel-perfectvoid
assertImageIsEqualToExpectedWithShiftAndCrop(InputStream expectedImageInput, int maxShift)
Verify that the element's image is pixel-perfect, but allowing one to be a cropped/shifted version of the other.void
assertImageIsSimilarToExpected(InputStream expectedImageInput, int maxBadPixelsRatio)
Verify the picture is "similar" to the reference image.void
assertImageIsSimilarToExpectedWithFilter(InputStream filterImageInput, InputStream expectedImageInput, int maxBadPixelsRatio)
Verify the picture is "similar" to the reference image.void
assertImageIsSimilarToExpectedWithShift(InputStream expectedImageInput, int maxBadPixelsRatio, int maxShift)
Verify the picture is "similar" to the reference image.void
assertImgSourceIsEqualToExpected(InputStream expectedImageInput)
Verify that the HTML img source is pixel-perfectstatic void
assertWindowIsSimilarToExpected(InputStream expectedImageInput, int maxBadPixelsRatio)
Similar to assertImageIsSimilarToExpected(), but compares the entire visible window.static void
assertWindowIsSimilarToExpectedWithFilter(InputStream filterImageInput, InputStream expectedImageInput, int maxBadPixelsRatio)
Similar to assertImageIsSimilarToExpectedWithFilter(), but compares the entire visible window.void
captureCanvasToFile(File outputFile)
Capture the image of an HTML5 canvas as a png, and save it to the given file.void
captureImgSourceToFile(File outputFile)
Capture the source of an img element as a png, and save it to the given filevoid
captureToFile(File outputFile)
Capture the image of an element as a png, and save it to the given filevoid
captureToFileNoScaling(File outputFile)
Capture the image of an element as a png, and save it to the given fileOptional<BufferedImage>
getErrorImage(InputStream expectedImageInput)
compare captured image to a reference image and return an image that highlights the differences.Optional<BufferedImage>
getFuzzyErrorImage(InputStream expectedImageInput)
compare captured image to a reference image and return an image that highlights the differences.void
show()
Display the element image in a separate window.void
showCanvas()
Similar to show(), but optimized for an HTML5 canvas element
-
-
-
Constructor Detail
-
SingltonBrowserImage
public SingltonBrowserImage(Path el)
- Parameters:
el
- The element the represents the image we are interested in
-
-
Method Detail
-
captureToFile
public void captureToFile(File outputFile)
Capture the image of an element as a png, and save it to the given file- Parameters:
outputFile
- - output file
-
captureToFileNoScaling
public void captureToFileNoScaling(File outputFile)
Capture the image of an element as a png, and save it to the given file- Parameters:
outputFile
- - output file
-
captureImgSourceToFile
public void captureImgSourceToFile(File outputFile)
Capture the source of an img element as a png, and save it to the given file- Parameters:
outputFile
- - output file
-
captureCanvasToFile
public void captureCanvasToFile(File outputFile)
Capture the image of an HTML5 canvas as a png, and save it to the given file. If the element given is not a canvas, this will fail. Note that it is more optimized - it downloads only the section of the canvas as an image.- Parameters:
outputFile
- - output file
-
show
public void show()
Display the element image in a separate window. This is useful for troubleshooting/development. Note that this will not work well if you do it inside a debugger evaluation.
-
showCanvas
public void showCanvas()
Similar to show(), but optimized for an HTML5 canvas element
-
assertImageIsEqualToExpected
public void assertImageIsEqualToExpected(InputStream expectedImageInput) throws IOException
Verify that the element's image is pixel-perfect- Parameters:
expectedImageInput
- - reference png image- Throws:
IOException
- - file could not be read
-
assertImageIsEqualToExpected
public static void assertImageIsEqualToExpected(InputStream actualImage, InputStream expectedImageInput) throws IOException
Verify that the element's image is pixel-perfect- Parameters:
actualImage
- - actual imageexpectedImageInput
- - reference png image- Throws:
IOException
- - file could not be read
-
assertImageIsEqualToExpectedNoScaling
public void assertImageIsEqualToExpectedNoScaling(InputStream expectedImageInput) throws IOException
Verify that the element's image is pixel-perfect- Parameters:
expectedImageInput
- - reference png image- Throws:
IOException
- - file could not be read
-
assertCanvasImageIsEqualToExpected
public void assertCanvasImageIsEqualToExpected(InputStream expectedImageInput) throws IOException
Verify that the canvas image is pixel-perfect- Parameters:
expectedImageInput
- - reference png image- Throws:
IOException
- - file could not be read
-
assertImgSourceIsEqualToExpected
public void assertImgSourceIsEqualToExpected(InputStream expectedImageInput) throws IOException
Verify that the HTML img source is pixel-perfect- Parameters:
expectedImageInput
- - reference png image- Throws:
IOException
- - file could not be read
-
getErrorImage
public Optional<BufferedImage> getErrorImage(InputStream expectedImageInput) throws IOException
compare captured image to a reference image and return an image that highlights the differences. Both images are expected to have the same dimensions, otherwise it throws in AssertionError.- Parameters:
expectedImageInput
- - reference png image- Returns:
- an image that highlights the different pixels. If the images are equal, returns an empty optional.
- Throws:
IOException
- - file could not be readAssertionError
- - images are not the same size
-
getFuzzyErrorImage
public Optional<BufferedImage> getFuzzyErrorImage(InputStream expectedImageInput) throws IOException
compare captured image to a reference image and return an image that highlights the differences. Both images are expected to have the same dimensions, otherwise it throws in AssertionError.- Parameters:
expectedImageInput
- - reference png image- Returns:
- an image that highlights the different pixels. If the images are equal, returns an empty optional.
- Throws:
IOException
- - file could not be readAssertionError
- - images are not the same size
-
assertImageIsEqualToExpectedWithShiftAndCrop
public void assertImageIsEqualToExpectedWithShiftAndCrop(InputStream expectedImageInput, int maxShift) throws IOException
Verify that the element's image is pixel-perfect, but allowing one to be a cropped/shifted version of the other.- Parameters:
expectedImageInput
- - reference png imagemaxShift
- maximum pixels the images are shifted/cropped compared to each other- Throws:
IOException
- - file could not be read
-
assertImageIsSimilarToExpected
public void assertImageIsSimilarToExpected(InputStream expectedImageInput, int maxBadPixelsRatio) throws IOException
Verify the picture is "similar" to the reference image. Ignores minor differences between the pixels. Does not identify offsets and rotation. It uses a VERY simplistic approach (no wavelets or any other transform).- Parameters:
expectedImageInput
- - reference png imagemaxBadPixelsRatio
- - a positive number. For example: If it's 100, then 1% of the pixels can have major differences compared to the reference.- Throws:
IOException
- - file could not be read
-
assertImageIsSimilarToExpectedWithShift
public void assertImageIsSimilarToExpectedWithShift(InputStream expectedImageInput, int maxBadPixelsRatio, int maxShift) throws IOException
Verify the picture is "similar" to the reference image. Ignores minor differences between the pixels. Does not identify offsets and rotation. It uses a VERY simplistic approach (no wavelets or any other transform).- Parameters:
expectedImageInput
- - reference png imagemaxBadPixelsRatio
- - a positive number. For example: If it's 100, then 1% of the pixels can have major differences compared to the reference.maxShift
- - max shift allowed- Throws:
IOException
- - file could not be read
-
assertImageIsSimilarToExpectedWithFilter
public void assertImageIsSimilarToExpectedWithFilter(InputStream filterImageInput, InputStream expectedImageInput, int maxBadPixelsRatio) throws IOException
Verify the picture is "similar" to the reference image. Ignores minor differences between the pixels. Does not identify offsets and rotation. It uses a VERY simplistic approach (no wavelets or any other transform).- Parameters:
filterImageInput
- - filter imageexpectedImageInput
- - reference png imagemaxBadPixelsRatio
- - a positive number. For example: If it's 100, then 1% of the pixels can have major differences compared to the reference.- Throws:
IOException
- - file could not be read
-
assertWindowIsSimilarToExpected
public static void assertWindowIsSimilarToExpected(InputStream expectedImageInput, int maxBadPixelsRatio) throws IOException
Similar to assertImageIsSimilarToExpected(), but compares the entire visible window.- Parameters:
expectedImageInput
- - reference png imagemaxBadPixelsRatio
- - threshold for max error rate- Throws:
IOException
-
assertWindowIsSimilarToExpectedWithFilter
public static void assertWindowIsSimilarToExpectedWithFilter(InputStream filterImageInput, InputStream expectedImageInput, int maxBadPixelsRatio) throws IOException
Similar to assertImageIsSimilarToExpectedWithFilter(), but compares the entire visible window.- Parameters:
filterImageInput
- - filter imageexpectedImageInput
- - reference imagemaxBadPixelsRatio
- threshold for max error rate- Throws:
IOException
-
-