How to use remember method of io.appium.java_client.ScreenshotState class

Best io.appium code snippet using io.appium.java_client.ScreenshotState.remember

ScreenshotState.java

Source:ScreenshotState.java Github

copy

Full Screen

...21 * The class constructor accepts two arguments. The first one is image comparator, the second22 * parameter is lambda function, that provides the screenshot of the necessary23 * screen area to be verified for similarity.24 * This lambda method is NOT called upon class creation.25 * One has to invoke {@link #remember()} method in order to call it.26 *27 * <p>Examples of provider function with Appium driver:28 * <code>29 * () -&gt; {30 * final byte[] srcImage = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);31 * return ImageIO.read(new ByteArrayInputStream(srcImage));32 * }33 * </code>34 * or35 * <code>36 * () -&gt; {37 * final byte[] srcImage = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);38 * final BufferedImage screenshot = ImageIO.read(new ByteArrayInputStream(srcImage));39 * final WebElement element = driver.findElement(locator);40 * // Can be simplified in Selenium 3.0+ by using getRect method of WebElement interface41 * final Point elementLocation = element.getLocation();42 * final Dimension elementSize = element.getSize();43 * return screenshot.getSubimage(44 * new Rectangle(elementLocation.x, elementLocation.y, elementSize.width, elementSize.height);45 * }46 * </code>47 *48 * @param comparator image comparator49 * @param stateProvider lambda function, which returns a screenshot for further comparison50 */51 public ScreenshotState(ComparesImages comparator, Supplier<BufferedImage> stateProvider) {52 this.comparator = checkNotNull(comparator);53 this.stateProvider = stateProvider;54 }55 public ScreenshotState(ComparesImages comparator) {56 this(comparator, null);57 }58 /**59 * Gets the interval value in ms between similarity verification rounds in <em>verify*</em> methods.60 *61 * @return current interval value in ms62 */63 public Duration getComparisonInterval() {64 return comparisonInterval;65 }66 /**67 * Sets the interval between similarity verification rounds in <em>verify*</em> methods.68 *69 * @param comparisonInterval interval value. 500 ms by default70 * @return self instance for chaining71 */72 public ScreenshotState setComparisonInterval(Duration comparisonInterval) {73 this.comparisonInterval = comparisonInterval;74 return this;75 }76 /**77 * Call this method to save the initial screenshot state.78 * It is mandatory to call before any <em>verify*</em> method is invoked.79 *80 * @return self instance for chaining81 */82 public ScreenshotState remember() {83 this.previousScreenshot = stateProvider.get();84 return this;85 }86 /**87 * This method allows to pass a custom bitmap for further comparison88 * instead of taking one using screenshot provider function. This might89 * be useful in some advanced cases.90 *91 * @param customInitialState valid bitmap92 * @return self instance for chaining93 */94 public ScreenshotState remember(BufferedImage customInitialState) {95 this.previousScreenshot = checkNotNull(customInitialState);96 return this;97 }98 public static class ScreenshotComparisonError extends RuntimeException {99 private static final long serialVersionUID = -7011854909939194466L;100 ScreenshotComparisonError(Throwable reason) {101 super(reason);102 }103 ScreenshotComparisonError(String message) {104 super(message);105 }106 }107 public static class ScreenshotComparisonTimeout extends RuntimeException {108 private static final long serialVersionUID = 6336247721154252476L;109 private final double currentScore;110 ScreenshotComparisonTimeout(String message, double currentScore) {111 super(message);112 this.currentScore = currentScore;113 }114 public double getCurrentScore() {115 return currentScore;116 }117 }118 private ScreenshotState checkState(Function<Double, Boolean> checkerFunc, Duration timeout) {119 final LocalDateTime started = LocalDateTime.now();120 double score;121 do {122 final BufferedImage currentState = stateProvider.get();123 score = getOverlapScore(ofNullable(this.previousScreenshot)124 .orElseThrow(() -> new ScreenshotComparisonError("Initial screenshot state is not set. "125 + "Nothing to compare")), currentState);126 if (checkerFunc.apply(score)) {127 return this;128 }129 try {130 Thread.sleep(comparisonInterval.toMillis());131 } catch (InterruptedException e) {132 throw new ScreenshotComparisonError(e);133 }134 }135 while (Duration.between(started, LocalDateTime.now()).compareTo(timeout) <= 0);136 throw new ScreenshotComparisonTimeout(137 String.format("Screenshot comparison timed out after %s ms. Actual similarity score: %.5f",138 timeout.toMillis(), score), score);139 }140 /**141 * Verifies whether the state of the screenshot provided by stateProvider lambda function142 * is changed within the given timeout.143 *144 * @param timeout timeout value145 * @param minScore the value in range (0.0, 1.0)146 * @return self instance for chaining147 * @throws ScreenshotComparisonTimeout if the calculated score is still148 * greater or equal to the given score after timeout happens149 * @throws ScreenshotComparisonError if {@link #remember()} method has not been invoked yet150 */151 public ScreenshotState verifyChanged(Duration timeout, double minScore) {152 return checkState((x) -> x < minScore, timeout);153 }154 /**155 * Verifies whether the state of the screenshot provided by stateProvider lambda function156 * is not changed within the given timeout.157 *158 * @param timeout timeout value159 * @param minScore the value in range (0.0, 1.0)160 * @return self instance for chaining161 * @throws ScreenshotComparisonTimeout if the calculated score is still162 * less than the given score after timeout happens163 * @throws ScreenshotComparisonError if {@link #remember()} method has not been invoked yet164 */165 public ScreenshotState verifyNotChanged(Duration timeout, double minScore) {166 return checkState((x) -> x >= minScore, timeout);167 }168 /**169 * Compares two valid java bitmaps and calculates similarity score between them.170 * Both images are expected to be of the same size/resolution. The method171 * implicitly invokes {@link ComparesImages#getImagesSimilarity(byte[], byte[])}.172 *173 * @param refImage reference image174 * @param tplImage template175 * @return similarity score value in range (-1.0, 1.0]. 1.0 is returned if the images are equal176 * @throws ScreenshotComparisonError if provided images are not valid or have177 * different resolution...

Full Screen

Full Screen

ScreenshotStateTests.java

Source:ScreenshotStateTests.java Github

copy

Full Screen

...57 //region Positive Tests58 @Test59 public void testBasicComparisonScenario() {60 final ScreenshotState currentState = new ScreenshotState(staticImage::generate)61 .remember();62 assertThat(currentState.verifyNotChanged(ONE_SECOND, MAX_SCORE), is(notNullValue()));63 }64 @Test65 public void testChangedImageVerification() {66 final ScreenshotState currentState = new ScreenshotState(randomImageOfStaticSize::generate)67 .remember();68 assertThat(currentState.verifyChanged(ONE_SECOND, MAX_SCORE), is(notNullValue()));69 }70 @Test71 public void testChangedImageVerificationWithDifferentSize() {72 final ScreenshotState currentState = new ScreenshotState(randomImageOfRandomSize::generate)73 .remember();74 assertThat(currentState.verifyChanged(ONE_SECOND, MAX_SCORE,75 ScreenshotState.ResizeMode.REFERENCE_TO_TEMPLATE_RESOLUTION), is(notNullValue()));76 }77 @Test78 public void testChangedImageVerificationWithCustomRememberedImage() {79 final ScreenshotState currentState = new ScreenshotState(randomImageOfRandomSize::generate)80 .remember(randomImageOfRandomSize.generate());81 assertThat(currentState.verifyChanged(ONE_SECOND, MAX_SCORE,82 ScreenshotState.ResizeMode.REFERENCE_TO_TEMPLATE_RESOLUTION), is(notNullValue()));83 }84 @Test85 public void testChangedImageVerificationWithCustomInterval() {86 final ScreenshotState currentState = new ScreenshotState(randomImageOfRandomSize::generate)87 .setComparisonInterval(Duration.ofMillis(100)).remember();88 assertThat(currentState.verifyChanged(ONE_SECOND, MAX_SCORE,89 ScreenshotState.ResizeMode.REFERENCE_TO_TEMPLATE_RESOLUTION), is(notNullValue()));90 }91 @Test92 public void testDirectOverlapScoreCalculation() {93 final BufferedImage anImage = staticImage.generate();94 final double score = ScreenshotState.getOverlapScore(anImage, anImage);95 assertThat(score, is(greaterThanOrEqualTo(MAX_SCORE)));96 }97 @Test98 public void testScreenshotComparisonUsingStaticMethod() {99 BufferedImage img1 = randomImageOfStaticSize.generate();100 // ImageIO.write(img1, "png", new File("img1.png"));101 BufferedImage img2 = randomImageOfStaticSize.generate();102 // ImageIO.write(img2, "png", new File("img2.png"));103 assertThat(ScreenshotState.getOverlapScore(img1, img2), is(lessThan(MAX_SCORE)));104 }105 //endregion106 //region Negative Tests107 @Test(expected = ScreenshotState.ScreenshotComparisonError.class)108 public void testDifferentSizeOfTemplates() {109 new ScreenshotState(randomImageOfRandomSize::generate).remember().verifyChanged(ONE_SECOND, MAX_SCORE);110 }111 @Test(expected = NullPointerException.class)112 public void testInvalidProvider() {113 new ScreenshotState(() -> null).remember();114 }115 @Test(expected = ScreenshotState.ScreenshotComparisonTimeout.class)116 public void testImagesComparisonExpectationFailed() {117 new ScreenshotState(randomImageOfStaticSize::generate).remember().verifyNotChanged(ONE_SECOND, MAX_SCORE);118 }119 @Test(expected = ScreenshotState.ScreenshotComparisonTimeout.class)120 public void testImagesComparisonExpectationFailed2() {121 new ScreenshotState(staticImage::generate).remember().verifyChanged(ONE_SECOND, MAX_SCORE);122 }123 @Test(expected = ScreenshotState.ScreenshotComparisonError.class)124 public void testScreenshotInitialStateHasNotBeenRemembered() {125 new ScreenshotState(staticImage::generate).verifyNotChanged(ONE_SECOND, MAX_SCORE);126 }127 //endregion128}...

Full Screen

Full Screen

remember

Using AI Code Generation

copy

Full Screen

1ScreenshotState state = new ScreenshotState();2state.remember();3ScreenshotState state = new ScreenshotState();4state.forget();5var state = new ScreenshotState();6state.remember();7var state = new ScreenshotState();8state.forget();9state = ScreenshotState()10state.remember()11state = ScreenshotState()12state.forget()13$state = new ScreenshotState();14$state->remember();15$state = new ScreenshotState();16$state->forget();17state := new ScreenshotState()18state.remember()19state := new ScreenshotState()20state.forget()21ScreenshotState state = new ScreenshotState();22state.remember();23ScreenshotState state = new ScreenshotState();24state.forget();25let state = ScreenshotState()26state.remember()27let state = ScreenshotState()28state.forget()

Full Screen

Full Screen

remember

Using AI Code Generation

copy

Full Screen

1{2 private static final List<WeakReference> REMEMBERED = new ArrayList<WeakReference>();3 public static void remember(WebElement element)4 {5 REMEMBERED.add(new WeakReference(element));6 }7 public static void forget(WebElement element)8 {9 for (WeakReference ref : REMEMBERED)10 {11 if (ref.get() == element)12 {13 REMEMBERED.remove(ref);14 break;15 }16 }17 }18 public static boolean isRemembered(WebElement element)19 {20 for (WeakReference ref : REMEMBERED)21 {22 if (ref.get() == element)23 {24 return true;25 }26 }27 return false;28 }29}30public void perform()31{32 if (this.actions.isEmpty())33 {34 throw new IllegalStateException("No actions to perform");35 }36 if (this.driver instanceof AppiumDriver)37 {38 ((AppiumDriver)this.driver).performTouchAction(this);39 }40 {41 if (this.driver instanceof AndroidDriver)42 {43 ((AndroidDriver)this.driver).performTouchAction(this);44 }45 {46 if (this.driver instanceof IOSDriver)47 {48 ((IOSDriver)this.driver).performTouchAction(this);49 }50 {51 throw new IllegalStateException("Driver does not support touch actions");52 }53 }54 }55}56public void performTouchAction(TouchAction action)57{58 this.execute(DriverCommand.PERFORM_TOUCH_ACTION, ImmutableMap.of("actions", action.getActions()));59}60public void performMultiAction(MultiTouchAction action)61{62 this.execute(DriverCommand.PERFORM_MULTI_ACTION, ImmutableMap.of("actions", action.getActions()));63}64public void perform()65{66 if (this.actions.isEmpty())67 {68 throw new IllegalStateException("No actions to perform");69 }70 if (this.driver instanceof AppiumDriver)71 {72 ((Appium

Full Screen

Full Screen

remember

Using AI Code Generation

copy

Full Screen

1public class AppiumTest {2 public void rememberScreenshot() {3 ScreenshotState.remember(driver, "screenshot");4 }5}6class AppiumTest(unittest.TestCase):7 def test_remember_screenshot(self):8 ScreenshotState.remember(driver, "screenshot")9 ScreenshotState.remember(driver, "screenshot")10describe('AppiumTest', function() {11 it('should remember screenshot', function() {12 ScreenshotState.remember(driver, "screenshot");13 });14});15public class AppiumTest {16 public void rememberScreenshot() {17 ScreenshotState.remember(driver, "screenshot");18 }19}20func AppiumTest(t *testing.T) {21 t.Run("rememberScreenshot", func(t *testing.T) {22 ScreenshotState.remember(driver, "screenshot");23 })24}25class AppiumTest {26 fun rememberScreenshot() {27 ScreenshotState.remember(driver,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in ScreenshotState

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful