2323public class ByImage extends By {
2424 private static boolean wasLibraryLoaded = false ;
2525 private final Mat template ;
26+ private final boolean doScaling ;
2627
2728 private static void loadLibrary () {
2829 if (!wasLibraryLoaded ) {
@@ -38,8 +39,19 @@ private static void loadLibrary() {
3839 * @param file image file to locate element by.
3940 */
4041 public ByImage (File file ) {
42+ this (file , false );
43+ }
44+
45+ /**
46+ * Constructor accepting image file.
47+ *
48+ * @param file image file to locate element by.
49+ * @param doScaling perform screenshot scaling if devicePixelRatio != 1
50+ */
51+ public ByImage (File file , boolean doScaling ) {
4152 loadLibrary ();
4253 this .template = Imgcodecs .imread (file .getAbsolutePath (), Imgcodecs .IMREAD_UNCHANGED );
54+ this .doScaling = doScaling ;
4355 }
4456
4557 /**
@@ -48,8 +60,19 @@ public ByImage(File file) {
4860 * @param bytes image bytes to locate element by.
4961 */
5062 public ByImage (byte [] bytes ) {
63+ this (bytes , false );
64+ }
65+
66+ /**
67+ * Constructor accepting image file.
68+ *
69+ * @param bytes image bytes to locate element by.
70+ * @param doScaling perform screenshot scaling if devicePixelRatio != 1
71+ */
72+ public ByImage (byte [] bytes , boolean doScaling ) {
5173 loadLibrary ();
5274 this .template = Imgcodecs .imdecode (new MatOfByte (bytes ), Imgcodecs .IMREAD_UNCHANGED );
75+ this .doScaling = doScaling ;
5376 }
5477
5578 @ Override
@@ -61,6 +84,12 @@ public String toString() {
6184 public List <WebElement > findElements (SearchContext context ) {
6285 byte [] screenshotBytes = getScreenshot (context );
6386 Mat source = Imgcodecs .imdecode (new MatOfByte (screenshotBytes ), Imgcodecs .IMREAD_UNCHANGED );
87+ long devicePixelRatio = (long ) AqualityServices .getBrowser ().executeScript (JavaScript .GET_DEVICE_PIXEL_RATIO );
88+ if (devicePixelRatio != 1 && doScaling ) {
89+ int scaledWidth = (int ) (source .width () / devicePixelRatio );
90+ int scaledHeight = (int ) (source .height () / devicePixelRatio );
91+ Imgproc .resize (source , source , new Size (scaledWidth , scaledHeight ), 0 , 0 , Imgproc .INTER_AREA );
92+ }
6493 Mat result = new Mat ();
6594 Imgproc .matchTemplate (source , template , result , Imgproc .TM_CCOEFF_NORMED );
6695
0 commit comments