33import com .assertthat .selenium_screenshotter .utils .file .FileUtil ;
44import com .assertthat .selenium_screenshotter .utils .image .ImageProcessor ;
55import com .assertthat .selenium_screenshotter .utils .web .Browser ;
6- import com .assertthat .selenium_screenshotter .utils .web .Screenshotter ;
76import com .assertthat .selenium_screenshotter .utils .web .ScrollStrategy ;
87import org .openqa .selenium .WebDriver ;
98import org .openqa .selenium .WebElement ;
@@ -23,68 +22,13 @@ public abstract class Screenshot<T extends Screenshot<T>> {
2322
2423 private static final String extension = "PNG" ;
2524 protected BufferedImage image ;
25+ protected BufferedImage thumbnailImage ;
2626 protected WebDriver driver ;
2727 private String fileName = LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy_MM_dd_HH_mm_ss_SSS" ))
2828 + "." + extension .toLowerCase ();
2929 private Path location = Paths .get ("./screenshots/" );
3030 private String title ;
3131
32- /**
33- * Make screenshot of the viewport only.
34- * To be used when screenshotting the page
35- * and don't need to scroll while making screenshots (FF, IE).
36- *
37- * @param driver WebDriver instance
38- * @return PageScreenshot instance
39- */
40- public static PageScreenshot page (WebDriver driver ) {
41- Browser browser = new Browser (driver );
42- PageScreenshot pageScreenshot = new PageScreenshot (driver );
43- pageScreenshot .setImage (Screenshotter .takeScreenshot (browser ));
44- return pageScreenshot ;
45- }
46-
47- /**
48- * To be used when screenshotting the page
49- * and need to scroll while making screenshots, either vertically or
50- * horizontally or both directions (Chrome).
51- *
52- * @param driver WebDriver instance
53- * @param scroll ScrollStrategy How you need to scroll
54- * @return PageScreenshot instance
55- */
56- public static PageScreenshot page (WebDriver driver , ScrollStrategy scroll ) {
57- Browser browser = new Browser (driver );
58- PageScreenshot pageScreenshot = new PageScreenshot (driver );
59- switch (scroll ) {
60- case HORIZONTALLY :
61- pageScreenshot .setImage (Screenshotter .takeScreenshotScrollHorizontally (browser ));
62- break ;
63- case VERTICALLY :
64- pageScreenshot .setImage (Screenshotter .takeScreenshotScrollVertically (browser ));
65- break ;
66- case BOTH_DIRECTIONS :
67- pageScreenshot .setImage (Screenshotter .takeScreenshotEntirePage (browser ));
68- }
69- return pageScreenshot ;
70- }
71-
72- /**
73- * To be used when need to screenshot particular element.
74- *
75- * @param driver WebDriver instance
76- * @param element WebElement instance to be screenshotted
77- * @return ElementScreenshot instance
78- */
79- public static ElementScreenshot element (WebDriver driver , WebElement element ) {
80- Browser browser = new Browser (driver );
81- ElementScreenshot elementScreenshot = new ElementScreenshot (driver , element );
82- browser .scrollToElement (element );
83- elementScreenshot .setImage (Screenshotter .takeScreenshot (browser ));
84- elementScreenshot .setImage (ImageProcessor .getElement (elementScreenshot .getImage (), browser .getBoundingClientRect (element )));
85- return elementScreenshot ;
86- }
87-
8832 protected abstract T self ();
8933
9034 /**
@@ -109,6 +53,36 @@ public T withTitle(String title) {
10953 return self ();
11054 }
11155
56+
57+ /**
58+ * Generate a thumbnail of the original screenshot.
59+ * Will save different thumbnails depends on when it was called in the chain.
60+ *
61+ * @param path to save thumbnail image to
62+ * @param name of the resulting image
63+ * @param scale to apply
64+ * @return instance of type Screenshot
65+ */
66+ public T withThumbnail (String path , String name , double scale ) {
67+ File thumbnailFile = new File (path .toString (), name );
68+ thumbnailFile .mkdirs ();
69+ thumbnailImage =ImageProcessor .scale (image ,0.5 );
70+ FileUtil .writeImage (thumbnailImage , extension , thumbnailFile );
71+ return self ();
72+ }
73+
74+ /**
75+ * Generate a thumbnail of the original screenshot.
76+ * Will save different thumbnails depends on when it was called in the chain.
77+ *
78+ * @param scale to apply
79+ * @return instance of type Screenshot
80+ */
81+ public T withThumbnail (double scale ) {
82+ withThumbnail (Paths .get (location .toString (),"./thumbnails" ).toString (),"thumb_" +fileName ,scale );
83+ return self ();
84+ }
85+
11286 /**
11387 * Apply gray-and-white filter to the image.
11488 *
@@ -136,6 +110,7 @@ protected void setImage(BufferedImage image) {
136110 */
137111 public void save () {
138112 File screenshotFile = new File (location .toString (), fileName );
113+ screenshotFile .mkdirs ();
139114 if (title != null && !title .isEmpty ()) {
140115 image = ImageProcessor .addTitle (image , title , Color .red , new Font ("Serif" , Font .BOLD , 20 ));
141116 }
@@ -146,30 +121,16 @@ public void save() {
146121 * Final method to be called in the chain.
147122 * Actually saves processed image to the specified path.
148123 */
149- public void save (String location ) {
150- File screenshotFile = new File (location , fileName );
151- if (title != null && !title .isEmpty ()) {
152- image = ImageProcessor .addTitle (image , title , Color .red , new Font ("Serif" , Font .BOLD , 20 ));
153- }
154- FileUtil .writeImage (image , extension , screenshotFile );
124+ public void save (String path ) {
125+ this .location = Paths .get (path );
126+ save ();
155127 }
156128
157129 /**
158- * Final method to be called in the chain.
159- * Actually saves processed image to the specified path.
160- */
161- public void save (Path location ) {
162- File screenshotFile = new File (location .toString (), fileName );
163- if (title != null && !title .isEmpty ()) {
164- image = ImageProcessor .addTitle (image , title , Color .red , new Font ("Serif" , Font .BOLD , 20 ));
165- }
166- FileUtil .writeImage (image , extension , screenshotFile );
167- }
168-
169- /**
170- * @param o
171- * @param deviation
172- * @return
130+ * @param o Object to compare with
131+ * @param deviation allowed deviation while comparing.
132+ * @return true if the the percentage of differences
133+ * between current image and provided one is less than or equal to <b>deviation</b>
173134 */
174135 public boolean equals (Object o , double deviation ) {
175136 if (this == o ) return true ;
0 commit comments