Skip to content

Commit 6a007f3

Browse files
committed
Add retina support. Fixes #17
1 parent 72bcde3 commit 6a007f3

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

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

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,36 @@ public class Shutterbug {
1818
private static final int DEFAULT_SCROLL_TIMEOUT = 100;
1919

2020
/**
21-
* Make screenshot of the viewport only.
22-
* To be used when screenshotting the page
23-
* and don't need to scroll while making screenshots (FF, IE).
21+
* Make screen shot of the viewport only.
22+
* To be used when screen shooting the page
23+
* and don't need to scroll while making screen shots (FF, IE).
2424
*
2525
* @param driver WebDriver instance
2626
* @return PageSnapshot instance
2727
*/
2828
public static PageSnapshot shootPage(WebDriver driver) {
29-
Browser browser = new Browser(driver);
29+
return shootPage(driver,false);
30+
}
31+
32+
/**
33+
* Make screen shot of the viewport only.
34+
* To be used when screen shooting the page
35+
* and don't need to scroll while making screen shots (FF, IE).
36+
*
37+
* @param driver WebDriver instance
38+
* @param useDevicePixelRatio whether or not take into account device pixel ratio
39+
* @return PageSnapshot instance
40+
*/
41+
public static PageSnapshot shootPage(WebDriver driver, boolean useDevicePixelRatio) {
42+
Browser browser = new Browser(driver, useDevicePixelRatio);
3043
PageSnapshot pageScreenshot = new PageSnapshot(driver);
3144
pageScreenshot.setImage(browser.takeScreenshot());
3245
return pageScreenshot;
3346
}
3447

3548
/**
36-
* To be used when screenshotting the page
37-
* and need to scroll while making screenshots, either vertically or
49+
* To be used when screen shooting the page
50+
* and need to scroll while making screen shots, either vertically or
3851
* horizontally or both directions (Chrome).
3952
*
4053
* @param driver WebDriver instance
@@ -46,17 +59,32 @@ public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll) {
4659
}
4760

4861
/**
49-
* To be used when screenshotting the page
50-
* and need to scroll while making screenshots, either vertically or
62+
* To be used when screen shooting the page
63+
* and need to scroll while making screen shots, either vertically or
5164
* horizontally or both directions (Chrome).
5265
*
5366
* @param driver WebDriver instance
5467
* @param scroll ScrollStrategy How you need to scroll
55-
* @param scrollTimeout Timeout to wait after scrolling and before taking screenshot
68+
* @param scrollTimeout Timeout to wait after scrolling and before taking screen shot
5669
* @return PageSnapshot instance
5770
*/
5871
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int scrollTimeout) {
59-
Browser browser = new Browser(driver);
72+
return shootPage(driver,scroll,scrollTimeout,false);
73+
}
74+
75+
/**
76+
* To be used when screen shooting the page
77+
* and need to scroll while making screen shots, either vertically or
78+
* horizontally or both directions (Chrome).
79+
*
80+
* @param driver WebDriver instance
81+
* @param scroll ScrollStrategy How you need to scroll
82+
* @param scrollTimeout Timeout to wait after scrolling and before taking screen shot
83+
* @param useDevicePixelRatio whether or not take into account device pixel ratio
84+
* @return PageSnapshot instance
85+
*/
86+
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int scrollTimeout, boolean useDevicePixelRatio) {
87+
Browser browser = new Browser(driver, useDevicePixelRatio);
6088
browser.setScrollTimeout(scrollTimeout);
6189
PageSnapshot pageScreenshot = new PageSnapshot(driver);
6290
switch (scroll) {
@@ -80,7 +108,19 @@ public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, in
80108
* @return ElementSnapshot instance
81109
*/
82110
public static ElementSnapshot shootElement(WebDriver driver, WebElement element) {
83-
Browser browser = new Browser(driver);
111+
return shootElement(driver,element,false);
112+
}
113+
114+
/**
115+
* To be used when need to screenshot particular element.
116+
*
117+
* @param driver WebDriver instance
118+
* @param element WebElement instance to be screen shot
119+
* @param useDevicePixelRatio whether or not take into account device pixel ratio
120+
* @return ElementSnapshot instance
121+
*/
122+
public static ElementSnapshot shootElement(WebDriver driver, WebElement element, boolean useDevicePixelRatio) {
123+
Browser browser = new Browser(driver,useDevicePixelRatio);
84124
ElementSnapshot elementSnapshot = new ElementSnapshot(driver);
85125
browser.scrollToElement(element);
86126
elementSnapshot.setImage(browser.takeScreenshot(),browser.getBoundingClientRect(element));

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.io.IOException;
1818
import java.util.ArrayList;
1919

20+
import static java.lang.Math.toIntExact;
21+
2022
/**
2123
* Created by Glib_Briia on 17/06/2016.
2224
*/
@@ -31,6 +33,7 @@ public class Browser {
3133
public static final String SCROLL_INTO_VIEW_JS = "js/scroll-element-into-view.js";
3234
public static final String CURRENT_SCROLL_Y_JS = "js/get-current-scrollY.js";
3335
public static final String CURRENT_SCROLL_X_JS = "js/get-current-scrollX.js";
36+
public static final String DEVICE_PIXEL_RATIO = "js/get-device-pixel-ratio.js";
3437

3538
private WebDriver driver;
3639
private int docHeight = -1;
@@ -40,9 +43,11 @@ public class Browser {
4043
private int currentScrollX;
4144
private int currentScrollY;
4245
private int scrollTimeout;
46+
private Long devicePixelRatio;
4347

44-
public Browser(WebDriver driver) {
48+
public Browser(WebDriver driver, boolean useDevicePixelRatio) {
4549
this.driver = driver;
50+
devicePixelRatio = useDevicePixelRatio?((Long)executeJsScript(DEVICE_PIXEL_RATIO)):1;
4651
}
4752

4853
public static void wait(int milis) {
@@ -138,27 +143,27 @@ public WebDriver getUnderlyingDriver() {
138143
}
139144

140145
public int getCurrentScrollX() {
141-
return ((Long) executeJsScript(Browser.CURRENT_SCROLL_X_JS)).intValue();
146+
return (int)(((Long) executeJsScript(Browser.CURRENT_SCROLL_X_JS))*devicePixelRatio);
142147
}
143148

144149
public int getCurrentScrollY() {
145-
return ((Long) executeJsScript(Browser.CURRENT_SCROLL_Y_JS)).intValue();
150+
return (int)(((Long) executeJsScript(Browser.CURRENT_SCROLL_Y_JS))*devicePixelRatio);
146151
}
147152

148153
public int getDocWidth() {
149-
return docWidth != -1 ? docWidth : ((Long) executeJsScript(MAX_DOC_WIDTH_JS)).intValue();
154+
return docWidth != -1 ? docWidth : (int)(((Long) executeJsScript(MAX_DOC_WIDTH_JS))*devicePixelRatio);
150155
}
151156

152157
public int getDocHeight() {
153-
return docHeight != -1 ? docHeight : ((Long) executeJsScript(MAX_DOC_HEIGHT_JS)).intValue();
158+
return docHeight != -1 ? docHeight : (int)(((Long) executeJsScript(MAX_DOC_HEIGHT_JS))*devicePixelRatio);
154159
}
155160

156161
public int getViewportWidth() {
157-
return viewportWidth != -1 ? viewportWidth : ((Long) executeJsScript(VIEWPORT_WIDTH_JS)).intValue();
162+
return viewportWidth != -1 ? viewportWidth : (int)(((Long) executeJsScript(VIEWPORT_WIDTH_JS))*devicePixelRatio);
158163
}
159164

160165
public int getViewportHeight() {
161-
return viewportHeight != -1 ? viewportHeight : ((Long) executeJsScript(VIEWPORT_HEIGHT_JS)).intValue();
166+
return viewportHeight != -1 ? viewportHeight : (int)(((Long) executeJsScript(VIEWPORT_HEIGHT_JS)).intValue()*devicePixelRatio);
162167
}
163168

164169
public Coordinates getBoundingClientRect(WebElement element) {
@@ -174,7 +179,7 @@ public void scrollToElement(WebElement element) {
174179
}
175180

176181
public void scrollTo(int x, int y) {
177-
executeJsScript(SCROLL_TO_JS, x, y);
182+
executeJsScript(SCROLL_TO_JS, x/devicePixelRatio, y/devicePixelRatio);
178183
}
179184

180185
public Object executeJsScript(String filePath, Object... arg) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return window.devicePixelRatio;

0 commit comments

Comments
 (0)