|
| 1 | +package aquality.selenium.elements.interfaces; |
| 2 | + |
| 3 | +import aquality.selenium.browser.AqualityServices; |
| 4 | +import aquality.selenium.browser.JavaScript; |
| 5 | +import org.opencv.core.Point; |
| 6 | +import org.opencv.core.*; |
| 7 | +import org.opencv.imgcodecs.Imgcodecs; |
| 8 | +import org.opencv.imgproc.Imgproc; |
| 9 | +import org.openqa.selenium.*; |
| 10 | +import org.openqa.selenium.interactions.Locatable; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Comparator; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +/** |
| 19 | + * Locator to search elements by image. |
| 20 | + * Takes screenshot and finds match using openCV. |
| 21 | + * Performs screenshot scaling if devicePixelRatio != 1. |
| 22 | + * Then finds elements by coordinates using javascript. |
| 23 | + */ |
| 24 | +public class ByImage extends By { |
| 25 | + private static boolean wasLibraryLoaded = false; |
| 26 | + private final Mat template; |
| 27 | + private final String description; |
| 28 | + private float threshold = 1 - AqualityServices.getConfiguration().getVisualizationConfiguration().getDefaultThreshold(); |
| 29 | + |
| 30 | + private static void loadLibrary() { |
| 31 | + if (!wasLibraryLoaded) { |
| 32 | + nu.pattern.OpenCV.loadShared(); |
| 33 | + System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME); |
| 34 | + wasLibraryLoaded = true; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor accepting image file. |
| 40 | + * |
| 41 | + * @param file image file to locate element by. |
| 42 | + */ |
| 43 | + public ByImage(File file) { |
| 44 | + loadLibrary(); |
| 45 | + description = file.getName(); |
| 46 | + this.template = Imgcodecs.imread(file.getAbsolutePath(), Imgcodecs.IMREAD_UNCHANGED); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor accepting image bytes. |
| 51 | + * |
| 52 | + * @param bytes image bytes to locate element by. |
| 53 | + */ |
| 54 | + public ByImage(byte[] bytes) { |
| 55 | + loadLibrary(); |
| 56 | + description = String.format("bytes[%d]", bytes.length); |
| 57 | + this.template = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_UNCHANGED); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Sets threshold of image similarity. |
| 62 | + * @param threshold a float between 0 and 1, where 1 means 100% match, and 0.5 means 50% match. |
| 63 | + * @return current instance of ByImage locator. |
| 64 | + */ |
| 65 | + public ByImage setThreshold(float threshold) { |
| 66 | + if (threshold < 0 || threshold > 1) { |
| 67 | + throw new IllegalArgumentException("Threshold must be a float between 0 and 1."); |
| 68 | + } |
| 69 | + this.threshold = threshold; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Gets threshold of image similarity. |
| 75 | + * @return current value of threshold. |
| 76 | + */ |
| 77 | + public float getThreshold() { |
| 78 | + return threshold; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String toString() { |
| 83 | + return String.format("ByImage: %s, size: (width:%d, height:%d)", description, template.width(), template.height()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean equals(Object o) { |
| 88 | + if (!(o instanceof ByImage)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + ByImage that = (ByImage) o; |
| 93 | + |
| 94 | + return this.template.equals(that.template); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public int hashCode() { |
| 99 | + return template.hashCode(); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + @Override |
| 104 | + public List<WebElement> findElements(SearchContext context) { |
| 105 | + Mat source = getScreenshot(context); |
| 106 | + Mat result = new Mat(); |
| 107 | + Imgproc.matchTemplate(source, template, result, Imgproc.TM_CCOEFF_NORMED); |
| 108 | + |
| 109 | + Core.MinMaxLocResult minMaxLoc = Core.minMaxLoc(result); |
| 110 | + |
| 111 | + int matchCounter = Math.abs((result.width() - template.width() + 1) * (result.height() - template.height() + 1)); |
| 112 | + List<Point> matchLocations = new ArrayList<>(); |
| 113 | + while (matchCounter > 0 && minMaxLoc.maxVal >= threshold) { |
| 114 | + matchCounter--; |
| 115 | + Point matchLocation = minMaxLoc.maxLoc; |
| 116 | + matchLocations.add(matchLocation); |
| 117 | + Imgproc.rectangle(result, new Point(matchLocation.x, matchLocation.y), new Point(matchLocation.x + template.cols(), |
| 118 | + matchLocation.y + template.rows()), new Scalar(0, 0, 0), -1); |
| 119 | + minMaxLoc = Core.minMaxLoc(result); |
| 120 | + } |
| 121 | + |
| 122 | + return matchLocations.stream().map(matchLocation -> getElementOnPoint(matchLocation, context)).collect(Collectors.toList()); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Gets a single element on point (find by center coordinates, then select closest to matchLocation). |
| 127 | + * |
| 128 | + * @param matchLocation location of the upper-left point of the element. |
| 129 | + * @param context search context. |
| 130 | + * If the searchContext is Locatable (like WebElement), adjust coordinates to be absolute coordinates. |
| 131 | + * @return the closest found element. |
| 132 | + */ |
| 133 | + protected WebElement getElementOnPoint(Point matchLocation, SearchContext context) { |
| 134 | + if (context instanceof Locatable) { |
| 135 | + final org.openqa.selenium.Point point = ((Locatable) context).getCoordinates().onPage(); |
| 136 | + matchLocation.x += point.getX(); |
| 137 | + matchLocation.y += point.getY(); |
| 138 | + } |
| 139 | + int centerX = (int) (matchLocation.x + (template.width() / 2)); |
| 140 | + int centerY = (int) (matchLocation.y + (template.height() / 2)); |
| 141 | + //noinspection unchecked |
| 142 | + List<WebElement> elements = (List<WebElement>) AqualityServices.getBrowser() |
| 143 | + .executeScript(JavaScript.GET_ELEMENTS_FROM_POINT, centerX, centerY); |
| 144 | + elements.sort(Comparator.comparingDouble(e -> distanceToPoint(matchLocation, e))); |
| 145 | + return elements.get(0); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Calculates distance from element to matching point. |
| 150 | + * |
| 151 | + * @param matchLocation matching point. |
| 152 | + * @param element target element. |
| 153 | + * @return distance in pixels. |
| 154 | + */ |
| 155 | + protected static double distanceToPoint(Point matchLocation, WebElement element) { |
| 156 | + org.openqa.selenium.Point elementLocation = element.getLocation(); |
| 157 | + return Math.sqrt(Math.pow(matchLocation.x - elementLocation.x, 2) + Math.pow(matchLocation.y - elementLocation.y, 2)); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Takes screenshot from searchContext if supported, or from browser. |
| 162 | + * |
| 163 | + * @param context search context for element location. |
| 164 | + * @return captured screenshot as Mat object. |
| 165 | + */ |
| 166 | + protected Mat getScreenshot(SearchContext context) { |
| 167 | + byte[] screenshotBytes = context instanceof TakesScreenshot |
| 168 | + ? ((TakesScreenshot) context).getScreenshotAs(OutputType.BYTES) |
| 169 | + : AqualityServices.getBrowser().getScreenshot(); |
| 170 | + boolean isBrowserScreenshot = context instanceof WebDriver || !(context instanceof TakesScreenshot); |
| 171 | + Mat source = Imgcodecs.imdecode(new MatOfByte(screenshotBytes), Imgcodecs.IMREAD_UNCHANGED); |
| 172 | + long devicePixelRatio = (long) AqualityServices.getBrowser().executeScript(JavaScript.GET_DEVICE_PIXEL_RATIO); |
| 173 | + if (devicePixelRatio != 1 && isBrowserScreenshot) { |
| 174 | + int scaledWidth = (int) (source.width() / devicePixelRatio); |
| 175 | + int scaledHeight = (int) (source.height() / devicePixelRatio); |
| 176 | + Imgproc.resize(source, source, new Size(scaledWidth, scaledHeight), 0, 0, Imgproc.INTER_AREA); |
| 177 | + } |
| 178 | + return source; |
| 179 | + } |
| 180 | +} |
0 commit comments