SingltonBrowserImage

public class SingltonBrowserImage

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.

Constructors

SingltonBrowserImage

public SingltonBrowserImage(Path el)
Parameters:
  • el – The element the represents the image we are interested in

Methods

assertCanvasImageIsEqualToExpected

public void assertCanvasImageIsEqualToExpected(InputStream expectedImageInput)

Verify that the canvas image is pixel-perfect

Parameters:
  • expectedImageInput
    • reference png image
Throws:
  • IOException
    • file could not be read

assertImageIsEqualToExpected

public void assertImageIsEqualToExpected(InputStream expectedImageInput)

Verify that the element’s image is pixel-perfect

Parameters:
  • expectedImageInput
    • reference png image
Throws:
  • IOException
    • file could not be read

assertImageIsEqualToExpectedWithShiftAndCrop

public void 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.

Parameters:
  • expectedImageInput
    • reference png image
  • maxShift – 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)

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 image
  • maxBadPixelsRatio
    • 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

assertImgSourceIsEqualToExpected

public void assertImgSourceIsEqualToExpected(InputStream expectedImageInput)

Verify that the HTML img source is pixel-perfect

Parameters:
  • expectedImageInput
    • reference png image
Throws:
  • IOException
    • file could not be read

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

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

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

getErrorImage

public Optional<BufferedImage> getErrorImage(InputStream expectedImageInput)

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
Throws:
  • IOException
    • file could not be read
  • AssertionError
    • images are not the same size
Returns:

an image that highlights the different pixels. If the images are equal, returns an empty optional.

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