Class ScreenshotState


  • public class ScreenshotState
    extends java.lang.Object
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double getOverlapScore​(java.awt.image.BufferedImage refImage, java.awt.image.BufferedImage tplImage)
      Compares two valid java bitmaps and calculates similarity score between them.
      ScreenshotState remember()
      Call this method to save the initial screenshot state.
      ScreenshotState remember​(java.awt.image.BufferedImage customInitialState)
      This method allows to pass a custom bitmap for further comparison instead of taking one using screenshot provider function.
      ScreenshotState verifyChanged​(java.time.Duration timeout, double minScore)
      Verifies whether the state of the screenshot provided by stateProvider lambda function is changed within the given timeout.
      ScreenshotState verifyNotChanged​(java.time.Duration timeout, double minScore)
      Verifies whether the state of the screenshot provided by stateProvider lambda function is not changed within the given timeout.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ScreenshotState

        public ScreenshotState​(ComparesImages comparator,
                               java.util.function.Supplier<java.awt.image.BufferedImage> stateProvider)
        The class constructor accepts two arguments. The first one is image comparator, the second parameter is lambda function, that provides the screenshot of the necessary screen area to be verified for similarity. This lambda method is NOT called upon class creation. One has to invoke remember() method in order to call it.

        Examples of provider function with Appium driver: () -> { final byte[] srcImage = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); return ImageIO.read(new ByteArrayInputStream(srcImage)); } or () -> { final byte[] srcImage = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); final BufferedImage screenshot = ImageIO.read(new ByteArrayInputStream(srcImage)); final WebElement element = driver.findElement(locator); // Can be simplified in Selenium 3.0+ by using getRect method of WebElement interface final Point elementLocation = element.getLocation(); final Dimension elementSize = element.getSize(); return screenshot.getSubimage( new Rectangle(elementLocation.x, elementLocation.y, elementSize.width, elementSize.height); }

        Parameters:
        comparator - image comparator
        stateProvider - lambda function, which returns a screenshot for further comparison
      • ScreenshotState

        public ScreenshotState​(ComparesImages comparator)
    • Method Detail

      • remember

        public ScreenshotState remember()
        Call this method to save the initial screenshot state. It is mandatory to call before any verify* method is invoked.
        Returns:
        self instance for chaining
      • remember

        public ScreenshotState remember​(java.awt.image.BufferedImage customInitialState)
        This method allows to pass a custom bitmap for further comparison instead of taking one using screenshot provider function. This might be useful in some advanced cases.
        Parameters:
        customInitialState - valid bitmap
        Returns:
        self instance for chaining
      • verifyChanged

        public ScreenshotState verifyChanged​(java.time.Duration timeout,
                                             double minScore)
        Verifies whether the state of the screenshot provided by stateProvider lambda function is changed within the given timeout.
        Parameters:
        timeout - timeout value
        minScore - the value in range (0.0, 1.0)
        Returns:
        self instance for chaining
        Throws:
        ScreenshotState.ScreenshotComparisonTimeout - if the calculated score is still greater or equal to the given score after timeout happens
        ScreenshotState.ScreenshotComparisonError - if remember() method has not been invoked yet
      • verifyNotChanged

        public ScreenshotState verifyNotChanged​(java.time.Duration timeout,
                                                double minScore)
        Verifies whether the state of the screenshot provided by stateProvider lambda function is not changed within the given timeout.
        Parameters:
        timeout - timeout value
        minScore - the value in range (0.0, 1.0)
        Returns:
        self instance for chaining
        Throws:
        ScreenshotState.ScreenshotComparisonTimeout - if the calculated score is still less than the given score after timeout happens
        ScreenshotState.ScreenshotComparisonError - if remember() method has not been invoked yet
      • getOverlapScore

        public double getOverlapScore​(java.awt.image.BufferedImage refImage,
                                      java.awt.image.BufferedImage tplImage)
        Compares two valid java bitmaps and calculates similarity score between them. Both images are expected to be of the same size/resolution. The method implicitly invokes ComparesImages.getImagesSimilarity(byte[], byte[]).
        Parameters:
        refImage - reference image
        tplImage - template
        Returns:
        similarity score value in range (-1.0, 1.0]. 1.0 is returned if the images are equal
        Throws:
        ScreenshotState.ScreenshotComparisonError - if provided images are not valid or have different resolution