Skip to content

Commit 296a2ea

Browse files
committed
Fix scrollable div and frame screenshot.
1 parent 0529c85 commit 296a2ea

File tree

13 files changed

+344
-175
lines changed

13 files changed

+344
-175
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,25 @@ Below are some basic examples of usage.
104104
### Frame screenshots
105105
- Take screenshot of scrollable frame locatable by supplied `frameID`:
106106
```java
107-
Shutterbug.shootFrame(driver, "frameID", Capture.FULL_SCROLL).save();
107+
Shutterbug.shootFrame(driver, "frameID", CaptureElement.FULL_SCROLL).save();
108108
```
109109

110110
- Take screenshot of scrollable frame web element:
111111
```java
112-
Shutterbug.shootFrame(driver, frameWebElement, Capture.FULL_SCROLL).save();
112+
Shutterbug.shootFrame(driver, frameWebElement, CaptureElement.FULL_SCROLL).save();
113113
```
114114

115+
**Please note** that currently full scrollable frame screenshot is only possible
116+
when the full frame is visible in the viewport. If frame size is greater
117+
than viewport size UnsupportedOperationException will be throws suggesting
118+
to use CaptureElement.VIEWPORT instead.
119+
115120
### Scrollable WebElements screenshots
116121

117122

118123
- Take screenshot of scrollable web element. Horizontal capture only:
119124
```java
120-
Shutterbug.shootElement(driver, webElement, Capture.HORIZONTAL_SCROLL).save();
125+
Shutterbug.shootElement(driver, webElement, CaptureElement.HORIZONTAL_SCROLL).save();
121126
```
122127

123128
### Operations chaining

pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.assertthat</groupId>
66
<artifactId>selenium-shutterbug</artifactId>
7-
<version>1.1-SNAPSHOT</version>
7+
<version>1.1</version>
88
<name>selenium-shutterbug</name>
99
<description>Utility library to create customized screenshots using Selenium WebDriver and Java AWT</description>
1010
<url>http://www.assertthat.com</url>
@@ -24,7 +24,7 @@
2424
<url>https://github.com/assertthat/selenium-shutterbug</url>
2525
<connection>scm:git:git://github.com/assertthat/selenium-shutterbug.git</connection>
2626
<developerConnection>scm:git:git@github.com:assertthat/selenium-shutterbug.git</developerConnection>
27-
<tag>v1.0</tag>
27+
<tag>v1.1</tag>
2828
</scm>
2929
<developers>
3030
<developer>
@@ -61,6 +61,16 @@
6161
<artifactId>java-semver</artifactId>
6262
<version>0.9.0</version>
6363
</dependency>
64+
<dependency>
65+
<groupId>ru.yandex.qatools.ashot</groupId>
66+
<artifactId>ashot</artifactId>
67+
<version>1.5.4</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>io.github.bonigarcia</groupId>
71+
<artifactId>webdrivermanager</artifactId>
72+
<version>4.0.0</version>
73+
</dependency>
6474
</dependencies>
6575

6676
<build>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
* Created by Glib_Briia on 17/06/2016.
1010
*/
1111
public enum Capture {
12-
VIEWPORT, FULL, FULL_SCROLL, VERTICAL_SCROLL, HORIZONTAL_SCROLL
12+
VIEWPORT, //capture visible part of the viewport only
13+
FULL, // full page screenshot using devtools
14+
FULL_SCROLL, // full page screenshot using scroll & stitch method
15+
VERTICAL_SCROLL, //vertical scroll page screenshot using scroll & stitch method
16+
HORIZONTAL_SCROLL // horizontal scroll page screenshot using scroll & stitch method
1317
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.assertthat.selenium_shutterbug.core;
2+
3+
public enum CaptureElement {
4+
FULL_SCROLL, //full element/frame screenshot using scroll & stitch method
5+
VIEWPORT, //capture visible part of the viewport only
6+
VERTICAL_SCROLL, //vertical scroll element/frame screenshot using scroll & stitch method
7+
HORIZONTAL_SCROLL //horizontal scroll element/frame screenshot using scroll & stitch method
8+
}

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

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,13 @@ public static PageSnapshot shootPage(WebDriver driver, Capture capture,
154154
pageScreenshot.setImage(browser.takeFullPageScreenshot());
155155
break;
156156
case VERTICAL_SCROLL:
157-
pageScreenshot.setImage(browser.takeFullPageVerticalScreenshot(false, null));
157+
pageScreenshot.setImage(browser.takeFullPageVerticalScreenshotScroll(null));
158158
break;
159159
case HORIZONTAL_SCROLL:
160-
_SCROLL:
161-
pageScreenshot.setImage(browser.takeFullPageHorizontalScreenshot(false, null));
160+
pageScreenshot.setImage(browser.takeFullPageHorizontalScreenshotScroll(null));
162161
break;
163162
case FULL_SCROLL:
164-
pageScreenshot.setImage(browser.takeFullPageScreenshotScroll(false, null));
163+
pageScreenshot.setImage(browser.takeFullPageScreenshotScroll(null));
165164
break;
166165
}
167166
return pageScreenshot;
@@ -197,7 +196,8 @@ public static ElementSnapshot shootElement(WebDriver driver, WebElement element)
197196
* @return ElementSnapshot instance
198197
*/
199198
public static ElementSnapshot shootElement(WebDriver driver,
200-
WebElement element, Capture capture) {
199+
WebElement element,
200+
CaptureElement capture) {
201201
return shootElement(driver, element, capture, true);
202202
}
203203

@@ -214,7 +214,7 @@ public static ElementSnapshot shootElement(WebDriver driver, WebElement element,
214214
Browser browser = new Browser(driver, useDevicePixelRatio);
215215
ElementSnapshot elementSnapshot = new ElementSnapshot(driver, browser.getDevicePixelRatio());
216216
browser.scrollToElement(element);
217-
elementSnapshot.setImage(browser.takeScreenshot(), browser.getBoundingClientRect(element));
217+
elementSnapshot.setImage(browser.takeScreenshot(), browser.getCoordinates(element));
218218
return elementSnapshot;
219219
}
220220

@@ -230,23 +230,23 @@ public static ElementSnapshot shootElement(WebDriver driver, WebElement element,
230230
*/
231231
public static ElementSnapshot shootElement(WebDriver driver,
232232
WebElement element,
233-
Capture capture,
233+
CaptureElement capture,
234234
boolean useDevicePixelRatio) {
235235
Browser browser = new Browser(driver, useDevicePixelRatio);
236236
ElementSnapshot elementSnapshot = new ElementSnapshot(driver, browser.getDevicePixelRatio());
237237
browser.scrollToElement(element);
238238
switch (capture) {
239-
case VIEWPORT:
240-
elementSnapshot.setImage(browser.takeElementViewportScreenshot(element));
241-
break;
242239
case VERTICAL_SCROLL:
243-
elementSnapshot.setImage(browser.takeFullElementVerticalScreenshot(element));
240+
elementSnapshot.setImage(browser.takeFullElementVerticalScreenshotScroll(element));
244241
break;
245242
case HORIZONTAL_SCROLL:
246-
elementSnapshot.setImage(browser.takeFullElementHorizontalScreenshot(element));
243+
elementSnapshot.setImage(browser.takeFullElementHorizontalScreenshotScroll(element));
244+
break;
245+
case FULL_SCROLL:
246+
elementSnapshot.setImage(browser.takeFullElementScreenshotScroll(element));
247247
break;
248248
default:
249-
elementSnapshot.setImage(browser.takeFullElementScreenshot(element));
249+
elementSnapshot.setImage(browser.takeElementViewportScreenshot(element));
250250
}
251251
return elementSnapshot;
252252
}
@@ -263,7 +263,7 @@ public static ElementSnapshot shootElementVerticallyCentered(WebDriver driver, W
263263
Browser browser = new Browser(driver, useDevicePixelRatio);
264264
ElementSnapshot elementSnapshot = new ElementSnapshot(driver, browser.getDevicePixelRatio());
265265
browser.scrollToElementVerticalCentered(element);
266-
elementSnapshot.setImage(browser.takeScreenshot(), browser.getBoundingClientRect(element));
266+
elementSnapshot.setImage(browser.takeScreenshot(), browser.getCoordinates(element));
267267
return elementSnapshot;
268268
}
269269

@@ -278,7 +278,7 @@ public static ElementSnapshot shootElementVerticallyCentered(WebDriver driver, W
278278
* @return PageSnapshot instance
279279
*/
280280
public static PageSnapshot shootFrame(WebDriver driver, String frameId,
281-
Capture capture,
281+
CaptureElement capture,
282282
boolean useDevicePixelRatio) {
283283
WebElement frame = driver.findElement(By.id(frameId));
284284
return shootFrame(driver, frame, capture, 0,
@@ -296,7 +296,8 @@ public static PageSnapshot shootFrame(WebDriver driver, String frameId,
296296
* @return PageSnapshot instance
297297
*/
298298
public static PageSnapshot shootFrame(WebDriver driver, WebElement frame,
299-
Capture capture, boolean useDevicePixelRatio) {
299+
CaptureElement capture,
300+
boolean useDevicePixelRatio) {
300301
return shootFrame(driver, frame, capture, 0,
301302
useDevicePixelRatio);
302303
}
@@ -306,12 +307,12 @@ public static PageSnapshot shootFrame(WebDriver driver, WebElement frame,
306307
* and need to scroll while making screenshots, either vertically or
307308
* horizontally or both directions.
308309
*
309-
* @param driver WebDriver instance
310-
* @param capture Capture type
310+
* @param driver WebDriver instance
311+
* @param capture Capture type
311312
* @return PageSnapshot instance
312313
*/
313314
public static PageSnapshot shootFrame(WebDriver driver, WebElement frame,
314-
Capture capture) {
315+
CaptureElement capture) {
315316
return shootFrame(driver, frame, capture, 0,
316317
true);
317318
}
@@ -329,12 +330,22 @@ public static PageSnapshot shootFrame(WebDriver driver, WebElement frame,
329330
* @return PageSnapshot instance
330331
*/
331332
public static PageSnapshot shootFrame(WebDriver driver, WebElement frame,
332-
Capture capture, int betweenScrollTimeout, boolean useDevicePixelRatio) {
333+
CaptureElement capture,
334+
int betweenScrollTimeout, boolean useDevicePixelRatio) {
333335
Browser browser = new Browser(driver, useDevicePixelRatio);
334336
browser.setBetweenScrollTimeout(betweenScrollTimeout);
335-
336337
browser.scrollToElement(frame);
337-
Coordinates coordinates = browser.getBoundingClientRect(frame);
338+
Coordinates coordinates = browser.getCoordinates(frame);
339+
340+
Browser browserParent = new Browser(driver, useDevicePixelRatio);
341+
if (capture != CaptureElement.VIEWPORT &&
342+
(coordinates.getWidth() > browserParent.getViewportWidth() || coordinates.getHeight() > browserParent.getViewportHeight())) {
343+
throw new UnsupportedOperationException("Full frame screenshot is" +
344+
" " +
345+
"only available if WHOLE frame is fully visible in the " +
346+
"viewport. Use CaptureElement.VIEWPORT in case frame is " +
347+
"outside of visible viewport.");
348+
}
338349
driver.switchTo().frame(frame);
339350

340351
if (beforeShootCondition != null) {
@@ -346,17 +357,23 @@ public static PageSnapshot shootFrame(WebDriver driver, WebElement frame,
346357

347358
PageSnapshot pageScreenshot = new PageSnapshot(driver, browser.getDevicePixelRatio());
348359
switch (capture) {
349-
case VIEWPORT:
350-
pageScreenshot.setImage(browser.takeFrameViewportScreenshot());
351-
break;
352360
case VERTICAL_SCROLL:
353-
pageScreenshot.setImage(browser.takeFullPageVerticalScreenshot(true, coordinates));
361+
pageScreenshot.setImage(browser
362+
.takeFullPageVerticalScreenshotScroll(coordinates));
354363
break;
355364
case HORIZONTAL_SCROLL:
356-
pageScreenshot.setImage(browser.takeFullPageHorizontalScreenshot(true, coordinates));
365+
pageScreenshot.setImage(browser
366+
.takeFullPageHorizontalScreenshotScroll(coordinates));
367+
break;
368+
case FULL_SCROLL:
369+
pageScreenshot.setImage(browser
370+
.takeFullPageScreenshotScroll(coordinates));
357371
break;
358372
default:
359-
pageScreenshot.setImage(browser.takeFullPageScreenshotScroll(true, coordinates));
373+
pageScreenshot.setImage(browser
374+
.takeFrameViewportScreenshot(coordinates));
375+
break;
376+
360377
}
361378
return pageScreenshot;
362379
}
@@ -372,21 +389,21 @@ public static PageSnapshot shootFrame(WebDriver driver, WebElement frame,
372389
* @return PageSnapshot instance
373390
*/
374391
public static PageSnapshot shootFrame(WebDriver driver, String frameId,
375-
Capture capture) {
392+
CaptureElement capture) {
376393
WebElement frame = driver.findElement(By.id(frameId));
377394
return shootFrame(driver, frame, capture, true);
378395
}
379396

380397
/**
381398
* To be used when screenshotting the frame
382-
* Takes full frame screenshot be default
399+
* Takes viewport of the frame screenshot be default
383400
*
384401
* @param driver WebDriver instance
385402
* @param frameId Id of the frame element
386403
* @return PageSnapshot instance
387404
*/
388405
public static PageSnapshot shootFrame(WebDriver driver, String frameId) {
389406
WebElement frame = driver.findElement(By.id(frameId));
390-
return shootFrame(driver, frame, Capture.FULL_SCROLL, true);
407+
return shootFrame(driver, frame, CaptureElement.VIEWPORT, true);
391408
}
392409
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.assertthat.selenium_shutterbug.utils.image.model.ImageData;
99
import com.assertthat.selenium_shutterbug.utils.web.Coordinates;
1010

11+
import javax.imageio.ImageIO;
1112
import java.awt.*;
1213
import java.awt.color.ColorSpace;
1314
import java.awt.image.BufferedImage;
@@ -17,6 +18,8 @@
1718
import java.awt.image.ConvolveOp;
1819
import java.awt.image.Kernel;
1920
import java.awt.image.PixelGrabber;
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
2023

2124
/**
2225
* Created by Glib_Briia on 17/06/2016.
@@ -204,4 +207,13 @@ public static boolean hasAlpha(Image image) {
204207
ColorModel cm = pg.getColorModel();
205208
return cm.hasAlpha();
206209
}
210+
211+
public static BufferedImage createImageFromBytes(byte[] imageData) {
212+
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
213+
try {
214+
return ImageIO.read(bais);
215+
} catch (IOException e) {
216+
throw new RuntimeException(e);
217+
}
218+
}
207219
}

0 commit comments

Comments
 (0)