Skip to content

Commit dfe23ac

Browse files
committed
Use devicePixelRatio for element operations
1 parent a997eb4 commit dfe23ac

File tree

8 files changed

+31
-25
lines changed

8 files changed

+31
-25
lines changed

src/main/java/com/assertthat/selenium_shutterbug/core/ElementSnapshot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
*/
2020
public class ElementSnapshot extends Snapshot {
2121

22-
ElementSnapshot(WebDriver driver) {
22+
ElementSnapshot(WebDriver driver, Double devicePixelRatio) {
2323
this.driver = driver;
24+
this.devicePixelRatio = devicePixelRatio;
2425
}
2526

2627
protected void setImage(BufferedImage image, Coordinates coords) {

src/main/java/com/assertthat/selenium_shutterbug/core/PageSnapshot.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
*/
2020
public class PageSnapshot extends Snapshot {
2121

22-
PageSnapshot(WebDriver driver) {
22+
PageSnapshot(WebDriver driver, Double devicePixelRatio) {
2323
this.driver = driver;
24+
this.devicePixelRatio = devicePixelRatio;
2425
}
2526

2627
/**
@@ -50,7 +51,7 @@ public PageSnapshot highlight(WebElement element) {
5051
*/
5152
public PageSnapshot highlight(WebElement element, Color color, int lineWidth) {
5253
try {
53-
image = ImageProcessor.highlight(image, new Coordinates(element), color, lineWidth);
54+
image = ImageProcessor.highlight(image, new Coordinates(element, devicePixelRatio), color, lineWidth);
5455
} catch (RasterFormatException rfe) {
5556
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
5657
}
@@ -91,7 +92,7 @@ public PageSnapshot highlightWithText(WebElement element, String text) {
9192
public PageSnapshot highlightWithText(WebElement element, Color elementColor, int lineWidth, String text, Color textColor, Font textFont) {
9293
try {
9394
highlight(element, elementColor, 0);
94-
Coordinates coords = new Coordinates(element);
95+
Coordinates coords = new Coordinates(element, devicePixelRatio);
9596
image = ImageProcessor.addText(image, coords.getX(), coords.getY() - textFont.getSize() / 2, text, textColor, textFont);
9697
} catch (RasterFormatException rfe) {
9798
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
@@ -117,7 +118,7 @@ public PageSnapshot blur() {
117118
*/
118119
public PageSnapshot blur(WebElement element) {
119120
try {
120-
image = ImageProcessor.blurArea(image, new Coordinates(element));
121+
image = ImageProcessor.blurArea(image, new Coordinates(element, devicePixelRatio));
121122
}catch(RasterFormatException rfe) {
122123
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
123124
}
@@ -133,7 +134,7 @@ public PageSnapshot blur(WebElement element) {
133134
*/
134135
public PageSnapshot monochrome(WebElement element) {
135136
try {
136-
image = ImageProcessor.monochromeArea(image, new Coordinates(element));
137+
image = ImageProcessor.monochromeArea(image, new Coordinates(element,devicePixelRatio));
137138
} catch (RasterFormatException rfe) {
138139
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
139140
}
@@ -148,7 +149,7 @@ public PageSnapshot monochrome(WebElement element) {
148149
*/
149150
public PageSnapshot blurExcept(WebElement element) {
150151
try{
151-
image = ImageProcessor.blurExceptArea(image, new Coordinates(element));
152+
image = ImageProcessor.blurExceptArea(image, new Coordinates(element,devicePixelRatio));
152153
}catch(RasterFormatException rfe){
153154
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE,rfe);
154155
}
@@ -165,7 +166,7 @@ public PageSnapshot blurExcept(WebElement element) {
165166
*/
166167
public PageSnapshot cropAround(WebElement element, int offsetX, int offsetY) {
167168
try{
168-
image = ImageProcessor.cropAround(image, new Coordinates(element), offsetX, offsetY);
169+
image = ImageProcessor.cropAround(image, new Coordinates(element,devicePixelRatio), offsetX, offsetY);
169170
}catch(RasterFormatException rfe){
170171
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE,rfe);
171172
}

src/main/java/com/assertthat/selenium_shutterbug/core/Shutterbug.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static PageSnapshot shootPage(WebDriver driver) {
4040
*/
4141
public static PageSnapshot shootPage(WebDriver driver, boolean useDevicePixelRatio) {
4242
Browser browser = new Browser(driver, useDevicePixelRatio);
43-
PageSnapshot pageScreenshot = new PageSnapshot(driver);
43+
PageSnapshot pageScreenshot = new PageSnapshot(driver,browser.getDevicePixelRatio());
4444
pageScreenshot.setImage(browser.takeScreenshot());
4545
return pageScreenshot;
4646
}
@@ -86,7 +86,7 @@ public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, in
8686
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int scrollTimeout, boolean useDevicePixelRatio) {
8787
Browser browser = new Browser(driver, useDevicePixelRatio);
8888
browser.setScrollTimeout(scrollTimeout);
89-
PageSnapshot pageScreenshot = new PageSnapshot(driver);
89+
PageSnapshot pageScreenshot = new PageSnapshot(driver, browser.getDevicePixelRatio());
9090
switch (scroll) {
9191
case HORIZONTALLY:
9292
pageScreenshot.setImage(browser.takeScreenshotScrollHorizontally());
@@ -120,8 +120,8 @@ public static ElementSnapshot shootElement(WebDriver driver, WebElement element)
120120
* @return ElementSnapshot instance
121121
*/
122122
public static ElementSnapshot shootElement(WebDriver driver, WebElement element, boolean useDevicePixelRatio) {
123-
Browser browser = new Browser(driver,useDevicePixelRatio);
124-
ElementSnapshot elementSnapshot = new ElementSnapshot(driver);
123+
Browser browser = new Browser(driver, useDevicePixelRatio);
124+
ElementSnapshot elementSnapshot = new ElementSnapshot(driver, browser.getDevicePixelRatio());
125125
browser.scrollToElement(element);
126126
elementSnapshot.setImage(browser.takeScreenshot(),browser.getBoundingClientRect(element));
127127
return elementSnapshot;

src/main/java/com/assertthat/selenium_shutterbug/core/Snapshot.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public abstract class Snapshot<T extends Snapshot> {
2828
protected BufferedImage image;
2929
protected BufferedImage thumbnailImage;
3030
protected WebDriver driver;
31+
protected Double devicePixelRatio = 1D;
3132
private String fileName = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS").format(new Date())
3233
+ "." + EXTENSION.toLowerCase();
3334
private Path location = Paths.get("./screenshots/");

src/main/java/com/assertthat/selenium_shutterbug/utils/image/ImageProcessor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public static BufferedImage addText(BufferedImage sourceImage, int x, int y, Str
4646
Graphics2D g = sourceImage.createGraphics();
4747
g.setPaint(color);
4848
g.setFont(font);
49-
FontMetrics fm = g.getFontMetrics();
5049
g.drawString(text, x, y);
5150
g.dispose();
5251
return sourceImage;

src/main/java/com/assertthat/selenium_shutterbug/utils/web/Browser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public Browser(WebDriver driver, boolean useDevicePixelRatio) {
5151
}
5252
}
5353

54+
public Double getDevicePixelRatio() {
55+
return devicePixelRatio;
56+
}
57+
5458
public static void wait(int milis) {
5559
try {
5660
Thread.sleep(milis);
@@ -178,7 +182,7 @@ public Coordinates getBoundingClientRect(WebElement element) {
178182
ArrayList<String> list = (ArrayList<String>) executeJsScript(RELATIVE_COORDS_JS, element);
179183
Point start = new Point(Integer.parseInt(list.get(0)), Integer.parseInt(list.get(1)));
180184
Dimension size = new Dimension(Integer.parseInt(list.get(2)), Integer.parseInt(list.get(3)));
181-
return new Coordinates(start, size);
185+
return new Coordinates(start, size, devicePixelRatio);
182186
}
183187

184188
public void scrollToElement(WebElement element) {

src/main/java/com/assertthat/selenium_shutterbug/utils/web/Coordinates.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ public class Coordinates {
1919
private int x;
2020
private int y;
2121

22-
public Coordinates(WebElement element) {
22+
public Coordinates(WebElement element, Double devicePixelRatio) {
2323
Point point = element.getLocation();
2424
Dimension size = element.getSize();
25-
this.width = size.getWidth();
26-
this.height = size.getHeight();
27-
this.x = point.getX();
28-
this.y = point.getY();
25+
this.width = (int)(size.getWidth()*devicePixelRatio);
26+
this.height = (int)(size.getHeight()*devicePixelRatio);
27+
this.x = (int)(point.getX()*devicePixelRatio);
28+
this.y = (int)(point.getY()*devicePixelRatio);
2929
}
3030

31-
public Coordinates(Point point, Dimension size) {
32-
this.width = size.getWidth();
33-
this.height = size.getHeight();
34-
this.x = point.getX();
35-
this.y = point.getY();
31+
public Coordinates(Point point, Dimension size, Double devicePixelRatio) {
32+
this.width = (int)(size.getWidth()*devicePixelRatio);
33+
this.height = (int)(size.getHeight()*devicePixelRatio);
34+
this.x = (int)(point.getX()*devicePixelRatio);
35+
this.y = (int)(point.getY()*devicePixelRatio);
3636
}
3737

3838

src/test/java/com/assertthat/selenium_shutterbug/utils/image/ImageProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void testImagesAreEqualsWithDeviation() throws IOException {
7979
public void testHighlight() throws IOException {
8080
Point point = new Point(9,33);
8181
Dimension size = new Dimension(141,17);
82-
Coordinates coords = new Coordinates(point, size);
82+
Coordinates coords = new Coordinates(point, size, 1D);
8383
BufferedImage clearImage = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("clearImage.png"));
8484
BufferedImage highlightedExpectedImage = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("highlightedImage.png"));
8585
BufferedImage highlightedActualImage = ImageProcessor.highlight(clearImage, coords, Color.red, 3);

0 commit comments

Comments
 (0)