Best io.appium code snippet using io.appium.java_client.imagecomparison.SimilarityMatchingResult
ComparesImages.java
Source:ComparesImages.java
...20import io.appium.java_client.imagecomparison.FeaturesMatchingResult;21import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions;22import io.appium.java_client.imagecomparison.OccurrenceMatchingResult;23import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;24import io.appium.java_client.imagecomparison.SimilarityMatchingResult;25import org.apache.commons.codec.binary.Base64;26import org.apache.commons.io.FileUtils;27import java.io.File;28import java.io.IOException;29import java.util.Map;30import javax.annotation.Nullable;31public interface ComparesImages extends ExecutesMethod {32 /**33 * Performs images matching by features with default options. Read34 * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html35 * for more details on this topic.36 *37 * @param base64image1 base64-encoded representation of the first image38 * @param base64Image2 base64-encoded representation of the second image39 * @return The matching result.40 */41 default FeaturesMatchingResult matchImagesFeatures(byte[] base64image1, byte[] base64Image2) {42 return matchImagesFeatures(base64image1, base64Image2, null);43 }44 /**45 * Performs images matching by features. Read46 * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html47 * for more details on this topic.48 *49 * @param base64image1 base64-encoded representation of the first image50 * @param base64Image2 base64-encoded representation of the second image51 * @param options comparison options52 * @return The matching result. The configuration of fields in the result depends on comparison options.53 */54 default FeaturesMatchingResult matchImagesFeatures(byte[] base64image1, byte[] base64Image2,55 @Nullable FeaturesMatchingOptions options) {56 Object response = CommandExecutionHelper.execute(this,57 compareImagesCommand(ComparisonMode.MATCH_FEATURES, base64image1, base64Image2, options));58 //noinspection unchecked59 return new FeaturesMatchingResult((Map<String, Object>) response);60 }61 /**62 * Performs images matching by features with default options. Read63 * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html64 * for more details on this topic.65 *66 * @param image1 The location of the first image67 * @param image2 The location of the second image68 * @return The matching result.69 */70 default FeaturesMatchingResult matchImagesFeatures(File image1, File image2) throws IOException {71 return matchImagesFeatures(image1, image2, null);72 }73 /**74 * Performs images matching by features. Read75 * https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html76 * for more details on this topic.77 *78 * @param image1 The location of the first image79 * @param image2 The location of the second image80 * @param options comparison options81 * @return The matching result. The configuration of fields in the result depends on comparison options.82 */83 default FeaturesMatchingResult matchImagesFeatures(File image1, File image2,84 @Nullable FeaturesMatchingOptions options) throws IOException {85 return matchImagesFeatures(Base64.encodeBase64(FileUtils.readFileToByteArray(image1)),86 Base64.encodeBase64(FileUtils.readFileToByteArray(image2)), options);87 }88 /**89 * Performs images matching by template to find possible occurrence of the partial image90 * in the full image with default options. Read91 * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html92 * for more details on this topic.93 *94 * @param fullImage base64-encoded representation of the full image95 * @param partialImage base64-encoded representation of the partial image96 * @return The matching result.97 */98 default OccurrenceMatchingResult findImageOccurrence(byte[] fullImage, byte[] partialImage) {99 return findImageOccurrence(fullImage, partialImage, null);100 }101 /**102 * Performs images matching by template to find possible occurrence of the partial image103 * in the full image. Read104 * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html105 * for more details on this topic.106 *107 * @param fullImage base64-encoded representation of the full image108 * @param partialImage base64-encoded representation of the partial image109 * @param options comparison options110 * @return The matching result. The configuration of fields in the result depends on comparison options.111 */112 default OccurrenceMatchingResult findImageOccurrence(byte[] fullImage, byte[] partialImage,113 @Nullable OccurrenceMatchingOptions options) {114 Object response = CommandExecutionHelper.execute(this,115 compareImagesCommand(ComparisonMode.MATCH_TEMPLATE, fullImage, partialImage, options));116 //noinspection unchecked117 return new OccurrenceMatchingResult((Map<String, Object>) response);118 }119 /**120 * Performs images matching by template to find possible occurrence of the partial image121 * in the full image with default options. Read122 * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html123 * for more details on this topic.124 *125 * @param fullImage The location of the full image126 * @param partialImage The location of the partial image127 * @return The matching result. The configuration of fields in the result depends on comparison options.128 */129 default OccurrenceMatchingResult findImageOccurrence(File fullImage, File partialImage) throws IOException {130 return findImageOccurrence(fullImage, partialImage, null);131 }132 /**133 * Performs images matching by template to find possible occurrence of the partial image134 * in the full image. Read135 * https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html136 * for more details on this topic.137 *138 * @param fullImage The location of the full image139 * @param partialImage The location of the partial image140 * @param options comparison options141 * @return The matching result. The configuration of fields in the result depends on comparison options.142 */143 default OccurrenceMatchingResult findImageOccurrence(File fullImage, File partialImage,144 @Nullable OccurrenceMatchingOptions options)145 throws IOException {146 return findImageOccurrence(Base64.encodeBase64(FileUtils.readFileToByteArray(fullImage)),147 Base64.encodeBase64(FileUtils.readFileToByteArray(partialImage)), options);148 }149 /**150 * Performs images matching to calculate the similarity score between them151 * with default options. The flow there is similar to the one used in152 * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},153 * but it is mandatory that both images are of equal size.154 *155 * @param base64image1 base64-encoded representation of the first image156 * @param base64Image2 base64-encoded representation of the second image157 * @return Matching result. The configuration of fields in the result depends on comparison options.158 */159 default SimilarityMatchingResult getImagesSimilarity(byte[] base64image1, byte[] base64Image2) {160 return getImagesSimilarity(base64image1, base64Image2, null);161 }162 /**163 * Performs images matching to calculate the similarity score between them.164 * The flow there is similar to the one used in165 * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},166 * but it is mandatory that both images are of equal size.167 *168 * @param base64image1 base64-encoded representation of the first image169 * @param base64Image2 base64-encoded representation of the second image170 * @param options comparison options171 * @return Matching result. The configuration of fields in the result depends on comparison options.172 */173 default SimilarityMatchingResult getImagesSimilarity(byte[] base64image1, byte[] base64Image2,174 @Nullable SimilarityMatchingOptions options) {175 Object response = CommandExecutionHelper.execute(this,176 compareImagesCommand(ComparisonMode.GET_SIMILARITY, base64image1, base64Image2, options));177 //noinspection unchecked178 return new SimilarityMatchingResult((Map<String, Object>) response);179 }180 /**181 * Performs images matching to calculate the similarity score between them182 * with default options. The flow there is similar to the one used in183 * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},184 * but it is mandatory that both images are of equal size.185 *186 * @param image1 The location of the full image187 * @param image2 The location of the partial image188 * @return Matching result. The configuration of fields in the result depends on comparison options.189 */190 default SimilarityMatchingResult getImagesSimilarity(File image1, File image2) throws IOException {191 return getImagesSimilarity(image1, image2, null);192 }193 /**194 * Performs images matching to calculate the similarity score between them.195 * The flow there is similar to the one used in196 * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)},197 * but it is mandatory that both images are of equal size.198 *199 * @param image1 The location of the full image200 * @param image2 The location of the partial image201 * @param options comparison options202 * @return Matching result. The configuration of fields in the result depends on comparison options.203 */204 default SimilarityMatchingResult getImagesSimilarity(File image1, File image2,205 @Nullable SimilarityMatchingOptions options)206 throws IOException {207 return getImagesSimilarity(Base64.encodeBase64(FileUtils.readFileToByteArray(image1)),208 Base64.encodeBase64(FileUtils.readFileToByteArray(image2)), options);209 }210}...
Edition098_Visual_Testing_1.java
Source:Edition098_Visual_Testing_1.java
1import io.appium.java_client.MobileBy;2import io.appium.java_client.android.AndroidDriver;3import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;4import io.appium.java_client.imagecomparison.SimilarityMatchingResult;5import java.io.File;6import java.net.URL;7import org.apache.commons.io.FileUtils;8import org.junit.After;9import org.junit.Before;10import org.junit.Test;11import org.openqa.selenium.By;12import org.openqa.selenium.OutputType;13import org.openqa.selenium.WebElement;14import org.openqa.selenium.remote.DesiredCapabilities;15import org.openqa.selenium.support.ui.ExpectedConditions;16import org.openqa.selenium.support.ui.WebDriverWait;17public class Edition098_Visual_Testing_1 {18 private final static String APP = "https://github.com/cloudgrey-io/the-app/releases/download/v1.10.0/TheApp-v1.10.0.apk";19 // need somewhere to store match files; change for a path suitable for your system20 private final static String VALIDATION_PATH = "/Users/jlipps/Desktop/validations";21 private final static String CHECK_HOME = "home_screen";22 private final static String CHECK_LOGIN = "login_screen";23 private final static String BASELINE = "BASELINE_";24 private final static double MATCH_THRESHOLD = 0.99;25 private final static By LOGIN_SCREEN = MobileBy.AccessibilityId("Login Screen");26 private final static By USERNAME_FIELD = MobileBy.AccessibilityId("username");27 private AndroidDriver<WebElement> driver;28 @Before29 public void setUp() throws Exception {30 DesiredCapabilities capabilities = new DesiredCapabilities();31 capabilities.setCapability("platformName", "Android");32 capabilities.setCapability("deviceName", "Android Emulator");33 capabilities.setCapability("automationName", "UiAutomator2");34 capabilities.setCapability("app", APP);35 // make sure we uninstall the app before each test regardless of version36 capabilities.setCapability("uninstallOtherPackages", "io.cloudgrey.the_app");37 URL server = new URL("http://localhost:4723/wd/hub");38 driver = new AndroidDriver<>(server, capabilities);39 }40 @After41 public void tearDown() {42 if (driver != null) {43 driver.quit();44 }45 }46 private WebElement waitForElement(WebDriverWait wait, By selector) {47 WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(selector));48 try { Thread.sleep(750); } catch (InterruptedException ign) {}49 return el;50 }51 @Test52 public void testAppDesign() throws Exception {53 WebDriverWait wait = new WebDriverWait(driver, 5);54 // wait for an element that's on the home screen55 WebElement loginScreen = waitForElement(wait, LOGIN_SCREEN);56 // now we know the home screen is loaded, so do a visual check57 doVisualCheck(CHECK_HOME);58 // nav to the login screen, and wait for an element that's on the login screen59 loginScreen.click();60 waitForElement(wait, USERNAME_FIELD);61 // perform our second visual check, this time of the login screen62 doVisualCheck(CHECK_LOGIN);63 }64 private void doVisualCheck(String checkName) throws Exception {65 String baselineFilename = VALIDATION_PATH + "/" + BASELINE + checkName + ".png";66 File baselineImg = new File(baselineFilename);67 // If no baseline image exists for this check, we should create a baseline image68 if (!baselineImg.exists()) {69 System.out.println(String.format("No baseline found for '%s' check; capturing baseline instead of checking", checkName));70 File newBaseline = driver.getScreenshotAs(OutputType.FILE);71 FileUtils.copyFile(newBaseline, new File(baselineFilename));72 return;73 }74 // Otherwise, if we found a baseline, get the image similarity from Appium. In getting the similarity,75 // we also turn on visualization so we can see what went wrong if something did.76 SimilarityMatchingOptions opts = new SimilarityMatchingOptions();77 opts.withEnabledVisualization();78 SimilarityMatchingResult res = driver.getImagesSimilarity(baselineImg, driver.getScreenshotAs(OutputType.FILE), opts);79 // If the similarity is not high enough, consider the check to have failed80 if (res.getScore() < MATCH_THRESHOLD) {81 File failViz = new File(VALIDATION_PATH + "/FAIL_" + checkName + ".png");82 res.storeVisualization(failViz);83 throw new Exception(84 String.format("Visual check of '%s' failed; similarity match was only %f, and below the threshold of %f. Visualization written to %s.",85 checkName, res.getScore(), MATCH_THRESHOLD, failViz.getAbsolutePath()));86 }87 // Otherwise, it passed!88 System.out.println(String.format("Visual check of '%s' passed; similarity match was %f",89 checkName, res.getScore()));90 }91}...
OpenCV.java
Source:OpenCV.java
...12import io.appium.java_client.MobileBy;13import io.appium.java_client.android.Activity;14import io.appium.java_client.android.AndroidDriver;15import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;16import io.appium.java_client.imagecomparison.SimilarityMatchingResult;17public class OpenCV 18{19 public static void main(String[] args) throws Exception20 {21 //visual testing via opencv22 String AppV1="D:\\batch249\\TheAppV1.apk";23 String appUpgrade="D:\\batch249\\TheAppV2.apk";24 String APPKG="io.cloudgrey.the_app";25 String APPACT="com.reactnativenavigation.controllers.NavigationActivity";26 String msg="Hello KALAM sir";27 By msgInput=MobileBy.AccessibilityId("messageInput");28 By echoBox=MobileBy.AccessibilityId("Echo Box");29 By savesmsg=MobileBy.AccessibilityId(msg);30 By savemsgbtn=MobileBy.AccessibilityId("messageSaveBtn");31 //start appium server32 Runtime.getRuntime().exec("cmd.exe /c start cmd.exe /k \"appium\"");33 URL u=new URL("http://0.0.0.0:4723/wd/hub");34 //Define desired capabilities related to device and app35 DesiredCapabilities dc=new DesiredCapabilities();36 dc.setCapability(CapabilityType.BROWSER_NAME,"");37 dc.setCapability("deviceName","emulator-5554");38 dc.setCapability("platformName","android");39 dc.setCapability("platformVersion","8.1.0");40 dc.setCapability("autoGrantPermissions","true");41 dc.setCapability("adbExecTimeout","50000");42 dc.setCapability("app",AppV1);43 dc.setCapability("uninstallOtherPackages","io.cloudgrey.the_app");44 //Launch app in device through appium server by creating driver object45 AndroidDriver driver;46 while(2>1)47 {48 try49 {50 driver=new AndroidDriver(u,dc);51 break;52 }53 catch(Exception ex)54 {55 56 }57 }58 Thread.sleep(10000);59 //create a folder for results60 SimpleDateFormat sf=new SimpleDateFormat("dd-MMM-yyyy-hh-mm-ss");61 Date dt=new Date();62 File resfolder=new File("VTResOn"+sf.format(dt));63 resfolder.mkdir();64 try65 {66 WebDriverWait w=new WebDriverWait(driver,20);67 w.until(ExpectedConditions.presenceOfElementLocated(echoBox));68 //Take screenshot of home screen in version169 File hs=driver.getScreenshotAs(OutputType.FILE);70 w.until(ExpectedConditions.elementToBeClickable(echoBox)).click();71 w.until(ExpectedConditions.presenceOfElementLocated(msgInput)).sendKeys(msg);72 w.until(ExpectedConditions.presenceOfElementLocated(savemsgbtn)).click();73 w.until(ExpectedConditions.presenceOfElementLocated(savesmsg));74 //Take screenshot of saved message screen in version 175 File ms=driver.getScreenshotAs(OutputType.FILE);76 Thread.sleep(10000);77 driver.installApp(appUpgrade);78 Thread.sleep(10000);79 Activity activity=new Activity(APPKG,APPACT);80 driver.startActivity(activity);81 w.until(ExpectedConditions.presenceOfElementLocated(echoBox));82 //Test the Home screen in version2 with version183 doVisualCheck(driver,"homescreen",hs,resfolder);84 w.until(ExpectedConditions.elementToBeClickable(echoBox)).click();85 w.until(ExpectedConditions.presenceOfElementLocated(savesmsg));86 //Test the saved message screen in version2 with version 187 doVisualCheck(driver,"msgscreen",ms,resfolder);88 }89 catch(Exception ex)90 {91 System.out.println(ex.getMessage());92 }93 //close app94 driver.closeApp();95 //stop appium server96 Runtime.getRuntime().exec("taskkill /F /IM node.exe");97 Runtime.getRuntime().exec("taskkill /F /IM cmd.exe");98 }99 public static void doVisualCheck(AndroidDriver driver, String checkName, File baseImg, File resfolder) throws Exception100 {101 SimilarityMatchingOptions opts=new SimilarityMatchingOptions();102 opts.withEnabledVisualization();103 File newImg=driver.getScreenshotAs(OutputType.FILE);104 SimilarityMatchingResult res=driver.getImagesSimilarity(baseImg,newImg);105 //if the similarity is not high enough,consider the check to have failed106 if(res.getScore()<1.0)107 {108 File difffile=new File(resfolder.getAbsolutePath()+"/Fail_"+checkName+".png");109 res.storeVisualization(difffile);110 System.out.println("Visual check of"+checkName+"failed; due to similarity match was only"+res.getScore()+111 ",and below the threshold of 1.0 Visualization written to"+difffile.getAbsolutePath());112 }113 else114 {115 System.out.println(String.format("Visual check of '%s' passed;similarity match was %f",116 checkName,res.getScore()));117 }118 }...
VehiclePage.java
Source:VehiclePage.java
...3import io.appium.java_client.TouchAction;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.android.AndroidElement;6import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;7import io.appium.java_client.imagecomparison.SimilarityMatchingResult;8import io.appium.java_client.pagefactory.AndroidFindBy;9import io.appium.java_client.touch.offset.PointOption;10import org.apache.commons.io.FileUtils;11import org.openqa.selenium.By;12import org.openqa.selenium.OutputType;13import java.io.File;14import java.util.List;15public class VehiclePage extends CommonPage {16 public @AndroidFindBy(id = "com.codecool.holabusz:id/centerTextView")17 MobileElement noStopsText;18 public String noStopsSentence = "There are no stops available in 0 meters";19 public @AndroidFindBy(xpath = "//android.widget.Toast[@text='Permission Denied']")20 MobileElement deniedToast;21 public @AndroidFindBy(id = "com.codecool.holabusz:id/testText")22 MobileElement meterText;23 public @AndroidFindBy(id = "com.codecool.holabusz:id/seekBar")24 MobileElement meterSlider;25 public @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/" +26 "android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/" +27 "android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/" +28 "androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]/android.widget.TextView[1]")29 MobileElement firstVehicleNumber;30 public VehiclePage(AndroidDriver<AndroidElement> androidDriver) {31 super(androidDriver);32 }33 public int getNumberOfVehicles() {34 List<AndroidElement> vehicles = androidDriver.findElements(By.xpath("/hierarchy/android.widget.FrameLayout/" +35 "android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/" +36 "android.view.ViewGroup/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup"));37 if (vehicles.size() < 1) {38 return 0;39 }40 return vehicles.size();41 }42 public void setMeterSlider(double percent) {43 int startX = meterSlider.getLocation().getX();44 int startY = meterSlider.getLocation().getY();45 int endX = meterSlider.getSize().getWidth();46 int moveToXDirectionAt = (int)(endX * percent);47 TouchAction act = new TouchAction(androidDriver);48 act.press(PointOption.point(startX, startY)).moveTo(PointOption.point(moveToXDirectionAt, startY)).release().perform();49 }50 public boolean doVisualCheck(String checkName) throws Exception {51 String baselineFileName = "C:\\Users\\ilove\\IdeaProjects\\holabusTest\\src\\test\\resources\\validationPictures\\"+checkName+".png";52 File baselineImg = new File(baselineFileName);53 System.out.println(baselineFileName);54 if (!baselineImg.exists()){55 System.out.printf("No baseline found for '%s' check capturing baseline!%n",checkName);56 File newBaseline = androidDriver.getScreenshotAs(OutputType.FILE);57 FileUtils.copyFile(newBaseline,new File(baselineFileName));58 return false;59 }60 SimilarityMatchingOptions opts = new SimilarityMatchingOptions();61 opts.withEnabledVisualization();62 SimilarityMatchingResult res = androidDriver.getImagesSimilarity(baselineImg, androidDriver.getScreenshotAs(OutputType.FILE), opts);63 if(res.getScore()<0.99){64 File failViz = new File("C:\\Users\\ilove\\IdeaProjects\\holabusTest\\src\\test\\resources\\failedValidations\\"+checkName+".png");65 res.storeVisualization(failViz);66 return false;67 }68 System.out.println("Visual is passed!");69 return true;70 }71}...
ImagesComparisonTest.java
Source:ImagesComparisonTest.java
...25import io.appium.java_client.imagecomparison.MatchingFunction;26import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions;27import io.appium.java_client.imagecomparison.OccurrenceMatchingResult;28import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;29import io.appium.java_client.imagecomparison.SimilarityMatchingResult;30import org.apache.commons.codec.binary.Base64;31import org.junit.Test;32import org.openqa.selenium.OutputType;33public class ImagesComparisonTest extends AppIOSTest {34 @Test35 public void verifyFeaturesMatching() {36 byte[] screenshot = Base64.encodeBase64(driver.getScreenshotAs(OutputType.BYTES));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()56 .withEnabledVisualization());57 assertThat(result.getVisualization().length, is(greaterThan(0)));58 assertNotNull(result.getRect());59 }60 @Test61 public void verifySimilarityCalculation() {62 byte[] screenshot = Base64.encodeBase64(driver.getScreenshotAs(OutputType.BYTES));63 SimilarityMatchingResult result = driver64 .getImagesSimilarity(screenshot, screenshot, new SimilarityMatchingOptions()65 .withEnabledVisualization());66 assertThat(result.getVisualization().length, is(greaterThan(0)));67 assertThat(result.getScore(), is(greaterThan(0.0)));68 }69}
nativeMobileTests.java
Source:nativeMobileTests.java
1package scenarios;2import bean.DataBean;3import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;4import io.appium.java_client.imagecomparison.SimilarityMatchingResult;5import org.apache.commons.codec.binary.Base64;6import org.openqa.selenium.OutputType;7import org.testng.annotations.Test;8import pageObjects.BudgetPage;9import pageObjects.RegisterPage;10import setup.BaseTest;11import setup.DataProvider;12import static org.assertj.core.api.Assertions.assertThat;13public class nativeMobileTests extends BaseTest {14 /*15 On the decision of bonus task, I chose the solution of comparing screenshots until and after click on button.16 I have found this on the appium documentation (https://appium.io/docs/en/writing-running-appium/image-comparison/)17 But this method required additional npm module as 'opencv4nodejs'. I have installed it but the exception is appearing anyway.18 Could you clarify, this decision is convenient or not?19 */20 @Test(priority = 1, groups = {"native"}, description = "This simple test just click on the Sign In button")21 public void simpleNativeTest() throws IllegalAccessException, NoSuchFieldException, InstantiationException {22 byte[] screenshot1 = Base64.encodeBase64(getDriver().getScreenshotAs(OutputType.BYTES));23 getPo().getWelement("signInBtn").click();24 byte[] screenshot2 = Base64.encodeBase64(getDriver().getScreenshotAs(OutputType.BYTES));25 SimilarityMatchingResult result = getDriver()26 .getImagesSimilarity(screenshot1, screenshot2, new SimilarityMatchingOptions()27 .withEnabledVisualization());28 assertThat(result.getVisualization().length).isGreaterThan(0);29 assertThat(result.getScore()).isGreaterThan(0.0);30 System.out.println("Simplest Android native test done");31 }32 @Test(priority = 2,33 groups = {"native"},34 description = "Register and Login assert",35 dataProviderClass = DataProvider.class,36 dataProvider = "data")37 public void registerAndLoginTest(DataBean data) throws IllegalAccessException, NoSuchFieldException, InstantiationException {38 getPo().getWelement("registerBtn").click();39 RegisterPage registerPage = getPo().getNativeRegisterPage("registerPage");...
ImageButtonTest.java
Source:ImageButtonTest.java
1package testcases;2import io.appium.java_client.android.AndroidDriver;3import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions;4import io.appium.java_client.imagecomparison.OccurrenceMatchingResult;5import io.appium.java_client.imagecomparison.SimilarityMatchingResult;6import org.apache.commons.codec.binary.Base64;7import org.openqa.selenium.By;8import org.openqa.selenium.OutputType;9import org.openqa.selenium.WebElement;10import org.testng.Assert;11import org.testng.annotations.BeforeTest;12import org.testng.annotations.Test;13import utils.ImageUtils;14import java.awt.*;15import java.io.File;16import java.io.IOException;17import java.net.URISyntaxException;18import java.net.URL;19/**...
SimilarityMatchingResult
Using AI Code Generation
1package appium.java;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.remote.DesiredCapabilities;8import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;9import io.appium.java_client.imagecomparison.SimilarityMatchingResult;10import io.appium.java_client.remote.AutomationName;11import io.appium.java_client.remote.MobileCapabilityType;12import io.appium.java_client.remote.MobilePlatform;13import io.appium.java_client.remote.MobileOption;14import io.appium.java_client.remote.MobileOptions;15import io.appium.java_client.remote.MobileType;16import io.appium.java_client.remote.MobileWebOption;17import io.appium.java_client.remote.OperaMobileOption;18import io.appium.java_client.remote.OperaOption;19import io.appium.java_client.remote.SafariMobileOption;20import io.appium.java_client.remote.SafariOption;21import io.appium.java_client.remote.YouiEngineOption;22import io.appium.java_client.service.local.AppiumDriverLocalService;23import io.appium.java_client.service.local.AppiumServiceBuilder;24import io.appium.java_client.service.local.flags.GeneralServerFlag;25public class AppiumJava {26 public static void main(String[] args) throws MalformedURLException, InterruptedException {27 AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().usingAnyFreePort().withArgument(GeneralServerFlag.SESSION_OVERRIDE));28 service.start();29 System.out.println("Appium Server Started");30 DesiredCapabilities cap = new DesiredCapabilities();31 cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");32 cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);33 cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);34 cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 60);35 cap.setCapability(MobileCapabilityType.APP, "C:\\Users\\Srinivas\\Desktop\\appium\\ApiDemos-debug.apk");
SimilarityMatchingResult
Using AI Code Generation
1import java.io.File;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.remote.DesiredCapabilities;6import io.appium.java_client.MobileElement;7import io.appium.java_client.android.AndroidDriver;8import io.appium.java_client.imagecomparison.SimilarityMatchingOptions;9import io.appium.java_client.imagecomparison.SimilarityMatchingResult;10import io.appium.java_client.remote.MobileCapabilityType;11public class ImageComparison {12public static void main(String[] args) throws MalformedURLException, InterruptedException {13 DesiredCapabilities cap = new DesiredCapabilities();14 cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");15 cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");16 cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9.0");17 cap.setCapability("appPackage", "com.android.calculator2");18 cap.setCapability("appActivity", "com.android.calculator2.Calculator");
SimilarityMatchingResult
Using AI Code Generation
1SimilarityMatchingResult result = driver.findImageElement(imagePath);2ImageComparisonMatchingResult result = driver.findImageElement(imagePath);3ImageComparisonMatchingResult result = driver.findImageElement(imagePath);4ImageComparisonMatchingResult result = driver.findImageElement(imagePath);5ImageComparisonMatchingResult result = driver.findImageElement(imagePath);6ImageComparisonMatchingResult result = driver.findImageElement(imagePath);7ImageComparisonMatchingResult result = driver.findImageElement(imagePath);8ImageComparisonMatchingResult result = driver.findImageElement(imagePath);9ImageComparisonMatchingResult result = driver.findImageElement(imagePath);10ImageComparisonMatchingResult result = driver.findImageElement(imagePath);11ImageComparisonMatchingResult result = driver.findImageElement(imagePath);12ImageComparisonMatchingResult result = driver.findImageElement(imagePath);13ImageComparisonMatchingResult result = driver.findImageElement(imagePath);
SimilarityMatchingResult
Using AI Code Generation
1SimilarityMatchingResult matchingResult = driver.findImageElement(imagePath);2ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.MATCH_TEMPLATE);3ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.SIFT);4ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.SURF);5ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.ORB);6ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.BRISK);7ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.AKAZE);8ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.BF);9ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.FLANN);10ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.SIFT, 0.5);11ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.SURF, 0.5);12ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.ORB, 0.5);13ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.BRISK, 0.5);14ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.AKAZE, 0.5);15ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.BF, 0.5);16ImageComparisonResult comparisonResult = driver.findImageElement(imagePath, ImageComparisonMode.FLANN, 0.5);17ImageElement imageElement = driver.findImageElement(imagePath);18ImageElement imageElement = driver.findImageElement(imagePath, ImageComparisonMode.MATCH_TEMPLATE);19ImageElement imageElement = driver.findImageElement(imagePath, ImageComparisonMode.SIFT);20ImageElement imageElement = driver.findImageElement(imagePath, ImageComparisonMode.SURF);21ImageElement imageElement = driver.findImageElement(imagePath, ImageComparisonMode.ORB);
SimilarityMatchingResult
Using AI Code Generation
1SimilarityMatchingResult result = driver.findImageElement("path/to/image");2ImageComparisonFeature imageComparisonFeature = driver.getImageComparisonFeature();3SimilarityMatchingResult result = imageComparisonFeature.findImageElement("path/to/image");4ImageComparisonResult imageComparisonResult = driver.compareImages("path/to/image1", "path/to/image2");5ImageComparisonFeature imageComparisonFeature = driver.getImageComparisonFeature();6ImageComparisonResult imageComparisonResult = imageComparisonFeature.compareImages("path/to/image1", "path/to/image2");7val result: SimilarityMatchingResult = driver.findImageElement("path/to/image")8val imageComparisonFeature: ImageComparisonFeature = driver.getImageComparisonFeature()9val result: SimilarityMatchingResult = imageComparisonFeature.findImageElement("path/to/image")10val imageComparisonResult: ImageComparisonResult = driver.compareImages("path/to/image1", "path/to/image2")11val imageComparisonFeature: ImageComparisonFeature = driver.getImageComparisonFeature()12val imageComparisonResult: ImageComparisonResult = imageComparisonFeature.compareImages("path/to/image1", "path/to/image2")13var imageComparisonFeature = driver.getImageComparisonFeature();14var result = imageComparisonFeature.findImageElement("path/to/image");15var imageComparisonResult = driver.compareImages("path/to/image1", "path/to/image2");16var imageComparisonFeature = driver.getImageComparisonFeature();17var imageComparisonResult = imageComparisonFeature.compareImages("path/to/image1", "path/to/image2");
SimilarityMatchingResult
Using AI Code Generation
1SimilarityMatchingResult result = driver.findImageElement(file);2System.out.println("Matched Percentage: " + result.getScore());3System.out.println("Matched Rectangle: " + result.getRect());4ImageElement imageElement = driver.findImageElement(file);5System.out.println("Matched Percentage: " + imageElement.getScore());6System.out.println("Matched Rectangle: " + imageElement.getRect());7ImageElement imageElement = driver.findImageElement("path/to/image");8System.out.println("Matched Percentage: " + imageElement.getScore());9System.out.println("Matched Rectangle: " + imageElement.getRect());10ImageElement imageElement = driver.findImageElement("image name");11System.out.println("Matched Percentage: " + imageElement.getScore());12System.out.println("Matched Rectangle: " + imageElement.getRect());13ImageElement imageElement = driver.findImageElement("image name", "base64 string of image");14System.out.println("Matched Percentage: " + imageElement.getScore());15System.out.println("Matched Rectangle: " + imageElement.getRect());16ImageElement imageElement = driver.findImageElement("image name", "base64 string of image", 0.9);17System.out.println("Matched Percentage: " + imageElement.getScore());18System.out.println("Matched Rectangle: " + imageElement.getRect());19ImageElement imageElement = driver.findImageElement("image name", "base64 string of image", 0.9, 0.9);20System.out.println("Matched Percentage: " + imageElement.getScore());21System.out.println("Matched Rectangle: " + imageElement.getRect());22ImageElement imageElement = driver.findImageElement("image name", "base64 string of image", 0.9, 0.9, 0.9);23System.out.println("Matched Percentage: " + image
SimilarityMatchingResult
Using AI Code Generation
1SimilarityMatchingResult result = driver.findImageElement(imageFile);2System.out.println("Similarity Score: " + result.getScore());3System.out.println("Similarity Matched Area: " + result.getRect().toString());4ImageComparisonResult result = driver.findImageElement(imageFile);5System.out.println("Similarity Score: " + result.getScore());6System.out.println("Similarity Matched Area: " + result.getRect().toString());7const result = await driver.findImageElement(imageFile);8console.log("Similarity Score: " + result.getScore());9console.log("Similarity Matched Area: " + result.getRect().toString());10result = driver.find_image_element(image_file)11result = driver.find_image_element(image_file)12print("Similarity Score: " + result.get_score)13print("Similarity Matched Area: " + result.get_rect)14var result = driver.FindImageElement(imageFile);15Console.WriteLine("Similarity Score: " + result.GetScore());16Console.WriteLine("Similarity Matched Area: " + result.GetRect().ToString());17$result = $driver->findImageElement($imageFile);18echo "Similarity Score: " . $result->getScore();19echo "Similarity Matched Area: " . $result->getRect()->toString();20val result = driver.findImageElement(imageFile)21println("Similarity Score: " + result.score)22println("Similarity Matched Area: " + result.rect)
SimilarityMatchingResult
Using AI Code Generation
1# from appium.webdriver.common.imagecomparison import SimilarityMatchingResult2# from appium.webdriver.common.imagecomparison import SimilarityMatchingOptions3# from selenium.webdriver.common.by import By4# from selenium.webdriver.support.ui import WebDriverWait5# from selenium.webdriver.support import expected_conditions as EC6# from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver7# from selenium.webdriver.remote.webelement import WebElement
SimilarityMatchingResult
Using AI Code Generation
1driver.findImageElement("path/to/image1.png").getImageSimilarity("path/to/image2.png");2double similarityScore = similarityMatchingResult.getSimilarity();3Rectangle matchingRectangle = similarityMatchingResult.getRectangle();4const imageComparison = new ImageComparison();5const similarityMatchingResult = imageComparison.getSimilarity("path/to/image1.png", 6"path/to/image2.png");7const similarityScore = similarityMatchingResult.similarity;8const matchingRectangle = similarityMatchingResult.rectangle;9from appium.webdriver.imagecomparison import ImageComparison10image_comparison = ImageComparison(self.driver)11similarity_matching_result = image_comparison.get_similarity('path/to/image1.png', 12image_comparison = Appium::ImageComparison.new(driver)13similarity_matching_result = image_comparison.get_similarity('path/to/image1.png', 14$similarityMatchingResult = $imageComparison->getSimilarity('path/to/image1.png', 15'path/to/image2.png');16$similarityScore = $similarityMatchingResult->similarity;17$matchingRectangle = $similarityMatchingResult->rectangle;
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!!