Skip to content

Commit ade1afe

Browse files
committed
Implement screenshot scaling in case when devicePixelRatio!=1 (like on modern Mac)
1 parent f6a94f0 commit ade1afe

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/main/java/aquality/selenium/browser/JavaScript.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public enum JavaScript {
2121
GET_CHECKBOX_STATE("getCheckBxState.js"),
2222
GET_COMBOBOX_SELECTED_TEXT("getCmbText.js"),
2323
GET_COMBOBOX_TEXTS("getCmbValues.js"),
24+
GET_DEVICE_PIXEL_RATIO("getDevicePixelRatio.js"),
2425
GET_ELEMENT_BY_XPATH("getElementByXpath.js"),
2526
GET_ELEMENTS_FROM_POINT("getElementsFromPoint.js"),
2627
GET_ELEMENT_XPATH("getElementXPath.js"),

src/main/java/aquality/selenium/elements/interfaces/ByImage.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
public 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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return window.devicePixelRatio;

src/test/java/theinternet/forms/BrokenImagesForm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.List;
99

1010
public class BrokenImagesForm extends TheInternetForm {
11-
private final By imageLocator = new ByImage(FileUtil.getResourceFileByName("brokenImage.png"));
11+
private final By imageLocator = new ByImage(FileUtil.getResourceFileByName("brokenImage.png"), true);
1212

1313
public BrokenImagesForm(){
1414
super(By.id("content"), "Broken Images form");

0 commit comments

Comments
 (0)