How to use SimilarityMatchingOptions class of io.appium.java_client.imagecomparison package

Best io.appium code snippet using io.appium.java_client.imagecomparison.SimilarityMatchingOptions

Run io.appium automation tests on LambdaTest cloud grid

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

VehiclePage.java

Source: VehiclePage.java Github

copy
1package pages;
2
3import io.appium.java_client.MobileElement;
4import io.appium.java_client.TouchAction;
5import io.appium.java_client.android.AndroidDriver;
6import io.appium.java_client.android.AndroidElement;
7import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;
8import io.appium.java_client.imagecomparison.SimilarityMatchingResult;
9import io.appium.java_client.pagefactory.AndroidFindBy;
10import io.appium.java_client.touch.offset.PointOption;
11import org.apache.commons.io.FileUtils;
12import org.openqa.selenium.By;
13import org.openqa.selenium.OutputType;
14
15import java.io.File;
16import java.util.List;
17
18public class VehiclePage extends CommonPage {
19
20
21    public @AndroidFindBy(id = "com.codecool.holabusz:id/centerTextView")
22    MobileElement noStopsText;
23    public String noStopsSentence = "There are no stops available in 0 meters";
24    public @AndroidFindBy(xpath = "//android.widget.Toast[@text='Permission Denied']")
25    MobileElement deniedToast;
26    public @AndroidFindBy(id = "com.codecool.holabusz:id/testText")
27    MobileElement meterText;
28    public @AndroidFindBy(id = "com.codecool.holabusz:id/seekBar")
29    MobileElement meterSlider;
30    public @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/" +
31            "android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/" +
32            "android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/" +
33            "androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]/android.widget.TextView[1]")
34    MobileElement firstVehicleNumber;
35
36
37
38    public VehiclePage(AndroidDriver<AndroidElement> androidDriver) {
39        super(androidDriver);
40    }
41
42    public int getNumberOfVehicles() {
43        List<AndroidElement> vehicles = androidDriver.findElements(By.xpath("/hierarchy/android.widget.FrameLayout/" +
44                "android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/" +
45                "android.view.ViewGroup/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup"));
46        if (vehicles.size() < 1) {
47            return 0;
48        }
49        return vehicles.size();
50    }
51
52
53    public void setMeterSlider(double percent) {
54        int startX = meterSlider.getLocation().getX();
55        int startY = meterSlider.getLocation().getY();
56        int endX = meterSlider.getSize().getWidth();
57        int moveToXDirectionAt = (int)(endX * percent);
58        TouchAction act = new TouchAction(androidDriver);
59        act.press(PointOption.point(startX, startY)).moveTo(PointOption.point(moveToXDirectionAt, startY)).release().perform();
60
61    }
62
63    public boolean doVisualCheck(String checkName) throws Exception {
64    String baselineFileName = "C:\\Users\\ilove\\IdeaProjects\\holabusTest\\src\\test\\resources\\validationPictures\\"+checkName+".png";
65    File baselineImg = new File(baselineFileName);
66        System.out.println(baselineFileName);
67
68    if (!baselineImg.exists()){
69        System.out.printf("No baseline found for '%s' check capturing baseline!%n",checkName);
70        File newBaseline = androidDriver.getScreenshotAs(OutputType.FILE);
71        FileUtils.copyFile(newBaseline,new File(baselineFileName));
72        return false;
73    }
74    SimilarityMatchingOptions opts = new SimilarityMatchingOptions();
75    opts.withEnabledVisualization();
76    SimilarityMatchingResult res = androidDriver.getImagesSimilarity(baselineImg, androidDriver.getScreenshotAs(OutputType.FILE), opts);
77
78    if(res.getScore()<0.99){
79        File failViz = new File("C:\\Users\\ilove\\IdeaProjects\\holabusTest\\src\\test\\resources\\failedValidations\\"+checkName+".png");
80        res.storeVisualization(failViz);
81        return false;
82
83
84    }
85        System.out.println("Visual is passed!");
86    return true;
87    }
88}
89
90
Full Screen

ImagesComparisonTest.java

Source: ImagesComparisonTest.java Github

copy
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * See the NOTICE file distributed with this work for additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package io.appium.java_client.ios;
18
19import static org.hamcrest.Matchers.greaterThan;
20import static org.hamcrest.core.Is.is;
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertThat;
24
25import io.appium.java_client.imagecomparison.FeatureDetector;
26import io.appium.java_client.imagecomparison.FeaturesMatchingOptions;
27import io.appium.java_client.imagecomparison.FeaturesMatchingResult;
28import io.appium.java_client.imagecomparison.MatchingFunction;
29import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions;
30import io.appium.java_client.imagecomparison.OccurrenceMatchingResult;
31import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;
32import io.appium.java_client.imagecomparison.SimilarityMatchingResult;
33import org.apache.commons.codec.binary.Base64;
34import org.junit.Test;
35import org.openqa.selenium.OutputType;
36
37public class ImagesComparisonTest extends AppIOSTest {
38
39    @Test
40    public void verifyFeaturesMatching() {
41        byte[] screenshot = Base64.encodeBase64(driver.getScreenshotAs(OutputType.BYTES));
42        FeaturesMatchingResult result = driver
43                .matchImagesFeatures(screenshot, screenshot, new FeaturesMatchingOptions()
44                        .withDetectorName(FeatureDetector.ORB)
45                        .withGoodMatchesFactor(40)
46                        .withMatchFunc(MatchingFunction.BRUTE_FORCE_HAMMING)
47                        .withEnabledVisualization());
48        assertThat(result.getVisualization().length, is(greaterThan(0)));
49        assertThat(result.getCount(), is(greaterThan(0)));
50        assertThat(result.getTotalCount(), is(greaterThan(0)));
51        assertFalse(result.getPoints1().isEmpty());
52        assertNotNull(result.getRect1());
53        assertFalse(result.getPoints2().isEmpty());
54        assertNotNull(result.getRect2());
55    }
56
57    @Test
58    public void verifyOccurrencesSearch() {
59        byte[] screenshot = Base64.encodeBase64(driver.getScreenshotAs(OutputType.BYTES));
60        OccurrenceMatchingResult result = driver
61                .findImageOccurrence(screenshot, screenshot, new OccurrenceMatchingOptions()
62                        .withEnabledVisualization());
63        assertThat(result.getVisualization().length, is(greaterThan(0)));
64        assertNotNull(result.getRect());
65    }
66
67    @Test
68    public void verifySimilarityCalculation() {
69        byte[] screenshot = Base64.encodeBase64(driver.getScreenshotAs(OutputType.BYTES));
70        SimilarityMatchingResult result = driver
71                .getImagesSimilarity(screenshot, screenshot, new SimilarityMatchingOptions()
72                        .withEnabledVisualization());
73        assertThat(result.getVisualization().length, is(greaterThan(0)));
74        assertThat(result.getScore(), is(greaterThan(0.0)));
75    }
76}
77
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in SimilarityMatchingOptions

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)