Skip to content

Commit a528c28

Browse files
committed
Fixes #68: Adding after scroll timeout, before taking screenshot using Chrome commands
1 parent c20770b commit a528c28

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

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

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,26 @@ public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll) {
6565
*
6666
* @param driver WebDriver instance
6767
* @param scroll ScrollStrategy How you need to scroll
68-
* @param scrollTimeout Timeout to wait after scrolling and before taking screen shot
68+
* @param betweenScrollTimeout Timeout to wait after scrolling and before taking screenshot
6969
* @return PageSnapshot instance
7070
*/
71-
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int scrollTimeout) {
72-
return shootPage(driver,scroll,scrollTimeout,false);
71+
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int betweenScrollTimeout) {
72+
return shootPage(driver,scroll,betweenScrollTimeout,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 betweenScrollTimeout Timeout to wait after scrolling and before taking screenshot
83+
* @param afterScrollTimeout Timeout to wait after scrolling and before taking screenshot
84+
* @return PageSnapshot instance
85+
*/
86+
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int betweenScrollTimeout, int afterScrollTimeout) {
87+
return shootPage(driver,scroll,betweenScrollTimeout,false, afterScrollTimeout);
7388
}
7489

7590
/**
@@ -93,13 +108,45 @@ public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, bo
93108
*
94109
* @param driver WebDriver instance
95110
* @param scroll ScrollStrategy How you need to scroll
96-
* @param scrollTimeout Timeout to wait after scrolling and before taking screen shot
111+
* @param useDevicePixelRatio whether or not take into account device pixel ratio
112+
* @param afterScrollTimeout Timeout to wait after scrolling and before taking screenshot
113+
* @return PageSnapshot instance
114+
*/
115+
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, boolean useDevicePixelRatio, int afterScrollTimeout) {
116+
return shootPage(driver,scroll,0,useDevicePixelRatio, afterScrollTimeout);
117+
}
118+
119+
/**
120+
* To be used when screen shooting the page
121+
* and need to scroll while making screen shots, either vertically or
122+
* horizontally or both directions (Chrome).
123+
*
124+
* @param driver WebDriver instance
125+
* @param scroll ScrollStrategy How you need to scroll
126+
* @param betweenScrollTimeout Timeout to wait between each scrolling operation
127+
* @param useDevicePixelRatio whether or not take into account device pixel ratio
128+
* @return PageSnapshot instance
129+
*/
130+
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int betweenScrollTimeout, boolean useDevicePixelRatio) {
131+
return shootPage(driver,scroll,betweenScrollTimeout,useDevicePixelRatio,0);
132+
}
133+
134+
/**
135+
* To be used when screen shooting the page
136+
* and need to scroll while making screen shots, either vertically or
137+
* horizontally or both directions (Chrome).
138+
*
139+
* @param driver WebDriver instance
140+
* @param scroll ScrollStrategy How you need to scroll
141+
* @param betweenScrollTimeout Timeout to wait between each scrolling operation
142+
* @param afterScrollTimeout Timeout to wait after scrolling and before taking screenshot
97143
* @param useDevicePixelRatio whether or not take into account device pixel ratio
98144
* @return PageSnapshot instance
99145
*/
100-
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int scrollTimeout, boolean useDevicePixelRatio) {
146+
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int betweenScrollTimeout, boolean useDevicePixelRatio, int afterScrollTimeout) {
101147
Browser browser = new Browser(driver, useDevicePixelRatio);
102-
browser.setScrollTimeout(scrollTimeout);
148+
browser.setBetweenScrollTimeout(betweenScrollTimeout);
149+
browser.setAfterScrollTimeout(afterScrollTimeout);
103150

104151
PageSnapshot pageScreenshot = new PageSnapshot(driver, browser.getDevicePixelRatio());
105152
switch (scroll) {

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public class Browser {
5858
private int docWidth = -1;
5959
private int viewportWidth = -1;
6060
private int viewportHeight = -1;
61-
private int scrollTimeout;
61+
private int betweenScrollTimeout;
62+
63+
private int afterScrollTimeout;
6264
private Double devicePixelRatio = 1.0;
6365

6466
public Browser(WebDriver driver, boolean useDevicePixelRatio) {
@@ -81,10 +83,15 @@ public static void wait(int ms) {
8183
}
8284
}
8385

84-
public void setScrollTimeout(int scrollTimeout) {
85-
this.scrollTimeout = scrollTimeout;
86+
public void setBetweenScrollTimeout(int betweenScrollTimeout) {
87+
this.betweenScrollTimeout = betweenScrollTimeout;
88+
}
89+
90+
public void setAfterScrollTimeout(int afterScrollTimeout) {
91+
this.afterScrollTimeout = afterScrollTimeout;
8692
}
8793

94+
8895
public BufferedImage takeScreenshot() {
8996
File srcFile = ((TakesScreenshot) this.getUnderlyingDriver()).getScreenshotAs(OutputType.FILE);
9097
try {
@@ -161,7 +168,7 @@ public BufferedImage takeScreenshotEntirePageDefault() {
161168
this.scrollTo(0, j * _viewportHeight);
162169
for (int i = 0; i < horizontalIterations; i++) {
163170
this.scrollTo(i * _viewportWidth, _viewportHeight * j);
164-
wait(scrollTimeout);
171+
wait(betweenScrollTimeout);
165172
Image image = takeScreenshot();
166173
g.drawImage(image, this.getCurrentScrollX(), this.getCurrentScrollY(), null);
167174
if (_docWidth == image.getWidth(null) && _docHeight == image.getHeight(null)) {
@@ -183,10 +190,11 @@ public BufferedImage takeScreenshotEntirePageUsingChromeCommand() {
183190
int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight());
184191
for (int j = 0; j < verticalIterations; j++) {
185192
this.scrollTo(0, j * this.getViewportHeight());
186-
wait(scrollTimeout);
193+
wait(betweenScrollTimeout);
187194
}
188195
Object metrics = this.evaluate(FileUtil.getJsScript(ALL_METRICS));
189196
this.sendCommand("Emulation.setDeviceMetricsOverride", metrics);
197+
wait(afterScrollTimeout);
190198
Object result = this.sendCommand("Page.captureScreenshot", ImmutableMap.of("format", "png", "fromSurface", true));
191199
this.sendCommand("Emulation.clearDeviceMetricsOverride", ImmutableMap.of());
192200
return decodeBase64EncodedPng((String) ((Map<String, ?>) result).get("data"));

0 commit comments

Comments
 (0)