Class ScreenshotState
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic class -
Constructor Summary
ConstructorsConstructorDescriptionScreenshotState(ComparesImages comparator) ScreenshotState(ComparesImages comparator, Supplier<BufferedImage> stateProvider) The class constructor accepts two arguments. -
Method Summary
Modifier and TypeMethodDescriptiondoublegetOverlapScore(BufferedImage refImage, BufferedImage tplImage) Compares two valid java bitmaps and calculates similarity score between them.remember()Call this method to save the initial screenshot state.remember(BufferedImage customInitialState) This method allows to pass a custom bitmap for further comparison instead of taking one using screenshot provider function.verifyChanged(Duration timeout, double minScore) Verifies whether the state of the screenshot provided by stateProvider lambda function is changed within the given timeout.verifyNotChanged(Duration timeout, double minScore) Verifies whether the state of the screenshot provided by stateProvider lambda function is not changed within the given timeout.
-
Constructor Details
-
ScreenshotState
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 invokeremember()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 comparatorstateProvider- lambda function, which returns a screenshot for further comparison
-
ScreenshotState
-
-
Method Details
-
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
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
Verifies whether the state of the screenshot provided by stateProvider lambda function is changed within the given timeout.- Parameters:
timeout- timeout valueminScore- 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 happensScreenshotState.ScreenshotComparisonError- ifremember()method has not been invoked yet
-
verifyNotChanged
Verifies whether the state of the screenshot provided by stateProvider lambda function is not changed within the given timeout.- Parameters:
timeout- timeout valueminScore- 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 happensScreenshotState.ScreenshotComparisonError- ifremember()method has not been invoked yet
-
getOverlapScore
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 invokesComparesImages.getImagesSimilarity(byte[], byte[]).- Parameters:
refImage- reference imagetplImage- 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
-