Best io.appium code snippet using io.appium.java_client.imagecomparison.FeaturesMatchingOptions.withMatchFunc
OpenCvImageComparison.java
Source:OpenCvImageComparison.java
...108 FeaturesMatchingResult result = Drivers.getMobileDriver()109 .matchImagesFeatures(screenshot, new File(originalImg), new FeaturesMatchingOptions()110 .withDetectorName(FeatureDetector.ORB)111 .withGoodMatchesFactor(40)112 .withMatchFunc(MatchingFunction.BRUTE_FORCE_HAMMING)113 .withEnabledVisualization());114 Date date = new Date();115 long getMilis = date.getTime();116 GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));117 calendar.setTimeInMillis(getMilis);118 File snapshotDir = new File("target/storedVisualization");119 if (!snapshotDir.exists()) {120 FileUtils.forceMkdir(snapshotDir);121 }122 MyLogger.log.info("Creating local image to display matchings between " + imageName + " and current screen");123 result.storeVisualization(new File("target/storedVisualization/" + calendar.getTime() + "_MatchingBetween_" + imageName + "_&_currentScreen.png"));124 }125 /**126 * Display similarities between two give images127 *128 * @param firstImageName129 * @param secondImageName130 * @throws IOException131 */132 public void displaySimilaritiesBetweenTwoGiveImages(String firstImageName, String secondImageName) throws IOException {133 String firstImage = String.format("target/test-classes/" + firstImageName + ".png");134 String secondImage = String.format("target/test-classes/" + secondImageName + ".png");135 FeaturesMatchingResult result = Drivers.getMobileDriver()136 .matchImagesFeatures(new File(firstImage), new File(secondImage), new FeaturesMatchingOptions()137 .withDetectorName(FeatureDetector.ORB)138 .withGoodMatchesFactor(40)139 .withMatchFunc(MatchingFunction.BRUTE_FORCE_HAMMING)140 .withEnabledVisualization());141 Date date = new Date();142 long getMilis = date.getTime();143 GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));144 calendar.setTimeInMillis(getMilis);145 File snapshotDir = new File("target/storedVisualization");146 if (!snapshotDir.exists()) {147 FileUtils.forceMkdir(snapshotDir);148 }149 MyLogger.log.info("Creating local image to display similarities between " + firstImageName + "_&_" + secondImageName);150 result.storeVisualization(new File("target/storedVisualization/" + calendar.getTime() + "_SimilaritiesBetween_" + firstImageName + "_&_" + secondImageName + ".png"));151 }152}...
ImagesComparisonTest.java
Source:ImagesComparisonTest.java
...37 FeaturesMatchingResult result = driver38 .matchImagesFeatures(screenshot, screenshot, new FeaturesMatchingOptions()39 .withDetectorName(FeatureDetector.ORB)40 .withGoodMatchesFactor(40)41 .withMatchFunc(MatchingFunction.BRUTE_FORCE_HAMMING)42 .withEnabledVisualization());43 assertThat(result.getVisualization().length, is(greaterThan(0)));44 assertThat(result.getCount(), is(greaterThan(0)));45 assertThat(result.getTotalCount(), is(greaterThan(0)));46 assertFalse(result.getPoints1().isEmpty());47 assertNotNull(result.getRect1());48 assertFalse(result.getPoints2().isEmpty());49 assertNotNull(result.getRect2());50 }51 @Test52 public void verifyOccurrencesSearch() {53 byte[] screenshot = Base64.encodeBase64(driver.getScreenshotAs(OutputType.BYTES));54 OccurrenceMatchingResult result = driver55 .findImageOccurrence(screenshot, screenshot, new OccurrenceMatchingOptions()...
HomePage.java
Source:HomePage.java
...55 FeaturesMatchingResult result = driver56 .matchImagesFeatures(screenShot, fullpath.getBytes(), new FeaturesMatchingOptions()57 .withDetectorName(FeatureDetector.ORB)58 .withGoodMatchesFactor(40)59 .withMatchFunc(MatchingFunction.BRUTE_FORCE_HAMMING)60 .withEnabledVisualization());61 Assert.assertEquals(result.getVisualization().length, result.getVisualization().length>0);62 Assert.assertEquals(result.getCount(), result.getCount()>0);63 Assert.assertEquals(result.getTotalCount(), result.getTotalCount()>0);64 Assert.assertFalse(result.getPoints1().isEmpty());65 Assert.assertNotNull(result.getRect1());66 Assert.assertFalse(result.getPoints2().isEmpty());67 Assert.assertNotNull(result.getRect2());68 69 LoginPage loginPage = new LoginPage(driver); 70 loginPage.InValidLogin(); 71 72 73
...
training.java
Source:training.java
...45//// FeaturesMatchingResult result = driver46//// .matchImagesFeatures(screenshot, originalImg, new FeaturesMatchingOptions()47//// .withDetectorName(FeatureDetector.ORB)48//// .withGoodMatchesFactor(40)49//// .withMatchFunc(MatchingFunction.BRUTE_FORCE_HAMMING)50//// .withEnabledVisualization());51////52//// assertFalse(result.getPoints1().isEmpty());53//// assertNotNull(result.getRect1());54//// assertFalse(result.getPoints2().isEmpty());55//// assertNotNull(result.getRect2());56// }57//58// public byte[] extractBytes (String ImageName) throws IOException {59// // open image60// File imgPath = new File("C:\\Users\\OSN20933\\Pictures\\stb setup images\\1.png");61// BufferedImage bufferedImage = ImageIO.read(imgPath);62//63// // get DataBufferBytes from Raster...
FeaturesMatchingOptions.java
Source:FeaturesMatchingOptions.java
...41 *42 * @param name the name of the matching function.43 * @return self instance for chaining.44 */45 public FeaturesMatchingOptions withMatchFunc(MatchingFunction name) {46 this.matchFunc = name.toString();47 return this;48 }49 /**50 * Sets the maximum count of "good" matches (e. g. with minimal distances).51 *52 * @param factor the "good" matches factor53 * @return self instance for chaining.54 */55 public FeaturesMatchingOptions withGoodMatchesFactor(int factor) {56 checkArgument(factor > 1);57 this.goodMatchesFactor = factor;58 return this;59 }...
withMatchFunc
Using AI Code Generation
1import io.appium.java_client.imagecomparison.FeaturesMatchingOptions;2import io.appium.java_client.imagecomparison.FeaturesMatchingResult;3import io.appium.java_client.imagecomparison.MatchAlgorithm;4import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions;5import io.appium.java_client.imagecomparison.OccurrenceMatchingResult;6import io.appium.java_client.imagecomparison.OccurrenceOrder;7import io.appium.java_client.imagecomparison.OccurrencePreference;8import java.io.File;9import java.io.IOException;10import java.util.ArrayList;11import java.util.List;12import java.util.Map;13import javax.imageio.ImageIO;14import org.openqa.selenium.By;15import org.openqa.selenium.Dimension;16import org.openqa.selenium.Point;17import org.openqa.selenium.WebElement;18import org.testng.Assert;19import org.testng.annotations.Test;20public class AppiumImageComparison extends BaseTest {21 public void testImageComparison() throws IOException {22 WebElement imageElement = driver.findElement(By.id("io.appium.android.apis:id/image"));23 File screenshot = driver.getScreenshotAs(org.openqa.selenium.OutputType.FILE);24 Point point = imageElement.getLocation();25 int eleWidth = imageElement.getSize().getWidth();26 int eleHeight = imageElement.getSize().getHeight();27 BufferedImage eleScreenshot = ImageIO.read(screenshot).getSubimage(point.getX(), point.getY(), eleWidth,28 eleHeight);29 ImageIO.write(eleScreenshot, "png", screenshot);30 File screenshotLocation = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\element.png");31 FileUtils.copyFile(screenshot, screenshotLocation);32 List<Point> features = driver.findImageFeatures(screenshotLocation);33 List<Feature> featuresList = new ArrayList<Feature>(features.size());34 for (Point p : features) {35 featuresList.add(new Feature(p.x, p.y));36 }
withMatchFunc
Using AI Code Generation
1import java.io.File;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.List;5import org.openqa.selenium.By;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.remote.DesiredCapabilities;8import io.appium.java_client.android.AndroidDriver;9import io.appium.java_client.imagecomparison.FeaturesMatchingOptions;10import io.appium.java_client.imagecomparison.MatchResult;11public class MatchFeatures {12 public static void main(String[] args) throws MalformedURLException {13 DesiredCapabilities caps = new DesiredCapabilities();14 caps.setCapability("platformName", "Android");15 caps.setCapability("deviceName", "emulator-5554");16 caps.setCapability("app", "C:\\Users\\User\\Downloads\\ApiDemos-debug.apk");17 WebElement views = driver.findElementByAccessibilityId("Views");18 views.click();19 WebElement dateWidgets = driver.findElementByAccessibilityId("Date Widgets");20 dateWidgets.click();21 WebElement inline = driver.findElementByAccessibilityId("2. Inline");22 inline.click();23 WebElement first = driver.findElement(By.id("android:id/first"));24 first.click();25 File firstImage = new File("C:\\Users\\User\\Desktop\\Appium\\firstImage.png");26 File secondImage = new File("C:\\Users\\User\\Desktop\\Appium\\secondImage.png");27 FeaturesMatchingOptions options = new FeaturesMatchingOptions().withEnabledVisualization();28 List<MatchResult> result = driver.findImageOccurrence(firstImage, secondImage, options);29 System.out.println(result);30 }31}
withMatchFunc
Using AI Code Generation
1import io.appium.java_client.imagecomparison.FeaturesMatchingOptions;2import io.appium.java_client.imagecomparison.ImageComparisonFeature;3import io.appium.java_client.imagecomparison.ImageComparisonMatchingResult;4import org.openqa.selenium.WebElement;5import org.testng.Assert;6import org.testng.annotations.Test;7import java.util.ArrayList;8import java.util.List;9public class ImageComparisonTest extends BaseTest {10 public void imageComparisonTest() {11 WebElement element = driver.findElementByAccessibilityId("Images");12 int x = element.getLocation().getX();13 int y = element.getLocation().getY();14 int imageWidth = element.getSize().getWidth();15 int imageHeight = element.getSize().getHeight();16 BufferedImage imgWholePage = ImageIO.read(new File("C:\\Users\\Downloads\\image1.png"));17 BufferedImage imgElement = imgWholePage.getSubimage(x, y, imageWidth, imageHeight);18 ImageIO.write(imgElement, "png", new File("C:\\Users\\Downloads\\image2.png"));19 List<ImageComparisonFeature> features = new ArrayList<>();20 features.add(ImageComparisonFeature.FEATURES_MATCH);21 features.add(ImageComparisonFeature.FEATURES_ARE_IN_SAME_PLACE);22 FeaturesMatchingOptions options = new FeaturesMatchingOptions();23 options.withEnabledComparisonFeatures(features);24 options.withMatchFunc((ImageComparisonMatchingResult result) -> {25 return result.getMatchPercentage() > 0.9;26 });27 boolean matchResult = driver.compareImages(imgElement, imgElement, options);28 Assert.assertTrue(matchResult);29 }30}31from appium import webdriver32from appium.webdriver.common.touch_action import TouchAction33from selenium.webdriver.common.by import By34from appium.webdriver.common.mobileby import MobileBy35from selenium.webdriver.support.ui import WebDriverWait36from selenium.webdriver.support import expected_conditions as EC37from appium.webdriver.common.image_comparison import FeaturesMatchingOptions, ImageComparisonFeature38from PIL import Image39import time40desired_caps = {}
withMatchFunc
Using AI Code Generation
1import io.appium.java_client.imagecomparison.*;2import java.util.*;3import java.io.*;4import java.nio.file.*;5import java.nio.file.Files;6import java.nio.file.Paths;7import java.nio.file.Path;8import java.nio.file.StandardOpenOption;9import org.openqa.selenium.*;10import org.openqa.selenium.remote.*;11import org.openqa.selenium.support.ui.*;12import org.testng.*;13import org.testng.annotations.*;14public class Appium {15 public static void main(String[] args) throws Exception {16 DesiredCapabilities caps = new DesiredCapabilities();17 caps.setCapability("platformName", "Android");18 caps.setCapability("deviceName", "emulator-5554");19 caps.setCapability("app", "C:\\Users\\appium\\Downloads\\ApiDemos-debug.apk");
withMatchFunc
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.chrome.ChromeDriver;3import org.openqa.selenium.chrome.ChromeOptions;4import org.openqa.selenium.remote.RemoteWebDriver;5import org.openqa.selenium.remote.CapabilityType;6import org.openqa.selenium.remote.DesiredCapabilities;7import org.openqa.selenium.By;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.support.ui.ExpectedConditions;10import org.openqa.selenium.support.ui.WebDriverWait;11import org.openqa.selenium.support.ui.FluentWait;12import org.openqa.selenium.support.ui.Wait;13import org.openqa.selenium.TakesScreenshot;14import org.openqa.selenium.OutputType;15import org.openqa.selenium.JavascriptExecutor;16import org.openqa.selenium.interactions.Actions;17import org.openqa.selenium.support.ui.Select;18import org.openqa.selenium.NoSuchElementException;19import org.openqa.selenium.TimeoutException;20import org.openqa.selenium.StaleElementReferenceException;21import org.openqa.selenium.WebDriverException;22import org.openqa.selenium.UnhandledAlertException;23import org.openqa.selenium.Alert;24import org.openqa.selenium.support.ui.Select;25import org.openqa.selenium.support.ui.ExpectedConditions;26import org.openqa.selenium.support.ui.WebDriverWait;27import org.openqa.selenium.support.ui.FluentWait;28import org.openqa.selenium.support.ui.Wait;29import org.openqa.selenium.TakesScreenshot;30import org.openqa.selenium.OutputType;31import org.openqa.selenium.JavascriptExecutor;32import org.openqa.selenium.interactions.Actions;33import org.openqa.selenium.support.ui.Select;34import org.openqa.selenium.NoSuchElementException;35import org.openqa.selenium.TimeoutException;36import org.openqa.selenium.StaleElementReferenceException;37import org.openqa.selenium.WebDriverException;38import org.openqa.selenium.UnhandledAlertException;39import org.openqa.selenium.Alert;40import org.openqa.selenium.support.ui.Select;41import org.openqa.selenium.support.ui.ExpectedConditions;42import org.openqa.selenium.support.ui.WebDriverWait;43import org.openqa.selenium.support.ui.FluentWait;44import org.openqa.selenium.support.ui.Wait;45import org.openqa.selenium.TakesScreenshot;46import org.openqa.selenium.OutputType;47import org.openqa.selenium.JavascriptExecutor;48import org.openqa.selenium.interactions.Actions;49import org.openqa.selenium.support.ui.Select;50import java.util.concurrent.TimeUnit;51import java.util.concurrent.TimeoutException;52import java.util.concurrent.Callable;53import java.util.concurrent.Executors;54import java.util.concurrent.ScheduledExecutorService;55import java.util.concurrent.ScheduledFuture;56import java.util.concurrent.ScheduledThreadPoolExecutor;57import java.util.concurrent.TimeUnit;58import java.util.concurrent.TimeoutException;59import java
withMatchFunc
Using AI Code Generation
1package com.appium;2import java.io.File;3import java.net.MalformedURLException;4import java.net.URL;5import java.util.concurrent.TimeUnit;6import org.openqa.selenium.By;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.remote.DesiredCapabilities;9import io.appium.java_client.android.AndroidDriver;10import io.appium.java_client.imagecomparison.FeaturesMatchingOptions;11import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;12import io.appium.java_client.remote.MobileCapabilityType;13public class ImageComparison {14public static void main(String[] args) throws MalformedURLException, InterruptedException {15DesiredCapabilities cap = new DesiredCapabilities();16cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");17cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");18cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.0.0");19cap.setCapability("appPackage", "com.android.calculator2");20cap.setCapability("appActivity", "com.android.calculator2.Calculator");21cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");22cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 100);23cap.setCapability(MobileCapabilityType.UDID, "emulator-5554");24cap.setCapability("autoGrantPermissions", true);25cap.setCapability("noReset", true);26cap.setCapability("fullReset", false);27cap.setCapability("skipUnlock", true);28cap.setCapability("app", "C:\\Users\\Raj\\Desktop\\appium\\ApiDemos-debug.apk");
withMatchFunc
Using AI Code Generation
1import io.appium.java_client.AppiumDriver;2import io.appium.java_client.MobileElement;3import io.appium.java_client.TouchAction;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.imagecomparison.FeaturesMatchingOptions;6import io.appium.java_client.imagecomparison.FeaturesMatchingResult;7import io.appium.java_client.touch.offset.PointOption;8import org.openqa.selenium.By;9import org.openqa.selenium.OutputType;10import org.openqa.selenium.Rectangle;11import org.openqa.selenium.remote.DesiredCapabilities;12import org.openqa.selenium.support.ui.ExpectedConditions;13import org.openqa.selenium.support.ui.WebDriverWait;14import java.io.File;15import java.net.MalformedURLException;16import java.net.URL;17import java.util.List;18import java.util.concurrent.TimeUnit;19public class Appium {20 public static void main(String[] args) throws MalformedURLException, InterruptedException {21 DesiredCapabilities dc = new DesiredCapabilities();22 dc.setCapability("platformName", "Android");23 dc.setCapability("deviceName", "emulator-5554");24 dc.setCapability("appPackage", "com.android.chrome");25 dc.setCapability("appActivity", "com.google.android.apps.chrome.Main");26 dc.setCapability("autoGrantPermissions", "true");27 dc.setCapability("noReset", "true");28 dc.setCapability("fullReset", "false");29 dc.setCapability("skipUnlock", "true");30 dc.setCapability("unicodeKeyboard", "true");31 dc.setCapability("resetKeyboard", "true");32 dc.setCapability("disableAndroidWatchers", "true");33 dc.setCapability("skipDeviceInitialization", "true");34 dc.setCapability("skipLogcatCapture", "true");35 dc.setCapability("skipServerInstallation", "true");36 dc.setCapability("skipUnlock", "true");37 dc.setCapability("skipDeviceInitialization", "true");38 dc.setCapability("skipLogcatCapture", "true");39 dc.setCapability("skipServerInstallation", "true");40 dc.setCapability("skipUnlock", "true");41 dc.setCapability("skipDeviceInitialization", "true");42 dc.setCapability("skipLogcatCapture", "true");43 dc.setCapability("skipServerInstallation", "true");
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!