Skip to content

Commit c103ea0

Browse files
ChristophKaserglibas
authored andcommitted
Account for scrollbars when taking a complete screenshot
Before this change, there was a regular pattern of horizontal and vertical scrollbars whan taking a full page screenshot
1 parent ff0a6b0 commit c103ea0

File tree

1 file changed

+17
-6
lines changed
  • src/main/java/com/assertthat/selenium_shutterbug/utils/web

1 file changed

+17
-6
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,30 @@ public BufferedImage takeScreenshot() {
6767
}
6868

6969
public BufferedImage takeScreenshotEntirePage() {
70-
BufferedImage combinedImage = new BufferedImage(this.getDocWidth(), this.getDocHeight(), BufferedImage.TYPE_INT_ARGB);
70+
final int _docWidth = this.getDocWidth();
71+
final int _docHeight = this.getDocHeight();
72+
BufferedImage combinedImage = new BufferedImage(_docWidth, _docHeight, BufferedImage.TYPE_INT_ARGB);
7173
Graphics2D g = combinedImage.createGraphics();
72-
int horizontalIterations = (int) Math.ceil(((double) this.getDocWidth()) / this.getViewportWidth());
73-
int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight());
74+
int _viewportWidth = this.getViewportWidth();
75+
int _viewportHeight = this.getViewportHeight();
76+
final int scrollBarMaxWidth = 40; // this is probably too high, but better to be safe than sorry
77+
78+
if (_viewportWidth < _docWidth || (_viewportHeight < _docHeight && _viewportWidth - scrollBarMaxWidth < _docWidth))
79+
_viewportHeight-=scrollBarMaxWidth; // some space for a scrollbar
80+
if (_viewportHeight < _docHeight)
81+
_viewportWidth-=scrollBarMaxWidth; // some space for a scrollbar
82+
83+
int horizontalIterations = (int) Math.ceil(((double) _docWidth) / _viewportWidth);
84+
int verticalIterations = (int) Math.ceil(((double) _docHeight) / _viewportHeight);
7485
outer_loop:
7586
for (int j = 0; j < verticalIterations; j++) {
76-
this.scrollTo(0, j * this.getViewportHeight());
87+
this.scrollTo(0, j * _viewportHeight);
7788
for (int i = 0; i < horizontalIterations; i++) {
78-
this.scrollTo(i * this.getViewportWidth(), this.getViewportHeight() * j);
89+
this.scrollTo(i * _viewportWidth, _viewportHeight * j);
7990
wait(scrollTimeout);
8091
Image image = takeScreenshot();
8192
g.drawImage(image, this.getCurrentScrollX(), this.getCurrentScrollY(), null);
82-
if(this.getDocWidth() == image.getWidth(null) && this.getDocHeight() == image.getHeight(null)){
93+
if(_docWidth == image.getWidth(null) && _docHeight == image.getHeight(null)){
8394
break outer_loop;
8495
}
8596
}

0 commit comments

Comments
 (0)