@@ -25,6 +25,7 @@ public class ByImage extends By {
2525 private static boolean wasLibraryLoaded = false ;
2626 private final Mat template ;
2727 private final String description ;
28+ private float threshold = 1 - AqualityServices .getConfiguration ().getVisualizationConfiguration ().getDefaultThreshold ();
2829
2930 private static void loadLibrary () {
3031 if (!wasLibraryLoaded ) {
@@ -56,6 +57,27 @@ public ByImage(byte[] bytes) {
5657 this .template = Imgcodecs .imdecode (new MatOfByte (bytes ), Imgcodecs .IMREAD_UNCHANGED );
5758 }
5859
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+
5981 @ Override
6082 public String toString () {
6183 return String .format ("ByImage: %s, size: (width:%d, height:%d)" , description , template .width (), template .height ());
@@ -84,7 +106,6 @@ public List<WebElement> findElements(SearchContext context) {
84106 Mat result = new Mat ();
85107 Imgproc .matchTemplate (source , template , result , Imgproc .TM_CCOEFF_NORMED );
86108
87- float threshold = 1 - AqualityServices .getConfiguration ().getVisualizationConfiguration ().getDefaultThreshold ();
88109 Core .MinMaxLocResult minMaxLoc = Core .minMaxLoc (result );
89110
90111 int matchCounter = Math .abs ((result .width () - template .width () + 1 ) * (result .height () - template .height () + 1 ));
0 commit comments