Skip to content

Commit 42d2dd4

Browse files
committed
Allow access to scroll timeout through public API
1 parent cbc0f8d commit 42d2dd4

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package com.assertthat.selenium_shutterbug.core;
77

8-
import com.assertthat.selenium_shutterbug.utils.image.ImageProcessor;
98
import com.assertthat.selenium_shutterbug.utils.web.Browser;
109
import com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy;
1110
import org.openqa.selenium.WebDriver;
@@ -16,6 +15,8 @@
1615
*/
1716
public class Shutterbug {
1817

18+
private static final int DEFAULT_SCROLL_TIMEOUT = 100;
19+
1920
/**
2021
* Make screenshot of the viewport only.
2122
* To be used when screenshotting the page
@@ -41,7 +42,22 @@ public static PageSnapshot shootPage(WebDriver driver) {
4142
* @return PageSnapshot instance
4243
*/
4344
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll) {
45+
return shootPage(driver, scroll, DEFAULT_SCROLL_TIMEOUT);
46+
}
47+
48+
/**
49+
* To be used when screenshotting the page
50+
* and need to scroll while making screenshots, either vertically or
51+
* horizontally or both directions (Chrome).
52+
*
53+
* @param driver WebDriver instance
54+
* @param scroll ScrollStrategy How you need to scroll
55+
* @param scrollTimeout Timeout to wait after scrolling and before taking screenshot
56+
* @return PageSnapshot instance
57+
*/
58+
public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, int scrollTimeout) {
4459
Browser browser = new Browser(driver);
60+
browser.setScrollTimeout(scrollTimeout);
4561
PageSnapshot pageScreenshot = new PageSnapshot(driver);
4662
switch (scroll) {
4763
case HORIZONTALLY:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public abstract class Snapshot<T extends Snapshot> {
2525

2626
private static final String EXTENSION = "PNG";
27-
protected static final String ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE = "Use ScrollStrategy.HORIZONTALLY, ScrollStrategy.VERTICALLY or ScrollStrategy.BOTH_DIRECTIONS to shoot the element outside the viewport";
27+
protected static final String ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE = "Requested element is outside the viewport";
2828
protected BufferedImage image;
2929
protected BufferedImage thumbnailImage;
3030
protected WebDriver driver;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class Browser {
3939
private int viewportHeight = -1;
4040
private int currentScrollX;
4141
private int currentScrollY;
42+
private int scrollTimeout;
4243

4344
public Browser(WebDriver driver) {
4445
this.driver = driver;
@@ -52,6 +53,10 @@ public static void wait(int milis) {
5253
}
5354
}
5455

56+
public void setScrollTimeout(int scrollTimeout){
57+
this.scrollTimeout = scrollTimeout;
58+
}
59+
5560
public BufferedImage takeScreenshot() {
5661
File srcFile = ((TakesScreenshot) this.getUnderlyingDriver()).getScreenshotAs(OutputType.FILE);
5762
try {
@@ -66,16 +71,16 @@ public BufferedImage takeScreenshotEntirePage() {
6671
Graphics2D g = combinedImage.createGraphics();
6772
int horizontalIterations = (int) Math.ceil(((double) this.getDocWidth()) / this.getViewportWidth());
6873
int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight());
69-
outerloop:
74+
outer_loop:
7075
for (int j = 0; j < verticalIterations; j++) {
7176
this.scrollTo(0, j * this.getViewportHeight());
7277
for (int i = 0; i < horizontalIterations; i++) {
7378
this.scrollTo(i * this.getViewportWidth(), this.getViewportHeight() * j);
74-
wait(50);
79+
wait(scrollTimeout);
7580
Image image = takeScreenshot();
7681
g.drawImage(image, this.getCurrentScrollX(), this.getCurrentScrollY(), null);
7782
if(this.getDocWidth() == image.getWidth(null) && this.getDocHeight() == image.getHeight(null)){
78-
break outerloop;
83+
break outer_loop;
7984
}
8085
}
8186
}
@@ -89,6 +94,7 @@ public BufferedImage takeScreenshotScrollHorizontally() {
8994
int horizontalIterations = (int) Math.ceil(((double) this.getDocWidth()) / this.getViewportWidth());
9095
for (int i = 0; i < horizontalIterations; i++) {
9196
this.scrollTo(i * this.getViewportWidth(), 0);
97+
wait(scrollTimeout);
9298
Image image = takeScreenshot();
9399
g.drawImage(image, this.getCurrentScrollX(), 0, null);
94100
if(this.getDocWidth() == image.getWidth(null)){
@@ -105,6 +111,7 @@ public BufferedImage takeScreenshotScrollVertically() {
105111
int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight());
106112
for (int j = 0; j < verticalIterations; j++) {
107113
this.scrollTo(0, j * this.getViewportHeight());
114+
wait(scrollTimeout);
108115
Image image = takeScreenshot();
109116
g.drawImage(image, 0, this.getCurrentScrollY(), null);
110117
if(this.getDocHeight() == image.getHeight(null)){

0 commit comments

Comments
 (0)