99import com .assertthat .selenium_shutterbug .utils .image .ImageProcessor ;
1010import org .openqa .selenium .WebDriver ;
1111
12+ import javax .imageio .ImageIO ;
1213import java .awt .*;
1314import java .awt .image .BufferedImage ;
1415import java .io .File ;
16+ import java .io .IOException ;
1517import java .nio .file .Files ;
1618import java .nio .file .Path ;
1719import java .nio .file .Paths ;
2325 */
2426public abstract class Snapshot <T extends Snapshot > {
2527
28+ static final String ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE = "Requested element is outside the viewport" ;
2629 private static final String EXTENSION = "PNG" ;
27- protected static final String ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE = "Requested element is outside the viewport" ;
2830 protected BufferedImage image ;
29- protected BufferedImage thumbnailImage ;
30- protected WebDriver driver ;
31- protected Double devicePixelRatio = 1D ;
31+ private BufferedImage thumbnailImage ;
32+ WebDriver driver ;
33+ Double devicePixelRatio = 1D ;
3234 private String fileName = new SimpleDateFormat ("yyyy_MM_dd_HH_mm_ss_SSS" ).format (new Date ())
3335 + "." + EXTENSION .toLowerCase ();
3436 private Path location = Paths .get ("./screenshots/" );
@@ -63,35 +65,35 @@ public T withTitle(String title) {
6365 * Generate a thumbnail of the original screenshot.
6466 * Will save different thumbnails depends on when it was called in the chain.
6567 *
66- * @param path to save thumbnail image to
67- * @param name of the resulting image
68+ * @param path to save thumbnail image to
69+ * @param name of the resulting image
6870 * @param scale to apply
6971 * @return instance of type Snapshot
7072 */
7173 public T withThumbnail (String path , String name , double scale ) {
7274 File thumbnailFile = new File (path , name );
73- if (!Files .exists (Paths .get (path ))) {
75+ if (!Files .exists (Paths .get (path ))) {
7476 thumbnailFile .mkdirs ();
7577 }
76- thumbnailImage = ImageProcessor .scale (image ,scale );
78+ thumbnailImage = ImageProcessor .scale (image , scale );
7779 FileUtil .writeImage (thumbnailImage , EXTENSION , thumbnailFile );
7880 return self ();
7981 }
80-
82+
8183 /**
8284 * Generate cropped thumbnail of the original screenshot.
8385 * Will save different thumbnails depends on when it was called in the chain.
8486 *
85- * @param path to save thumbnail image to
86- * @param name of the resulting image
87- * @param scale to apply
88- * @param cropWidth e.g. 0.2 will leave 20% of the initial width
87+ * @param path to save thumbnail image to
88+ * @param name of the resulting image
89+ * @param scale to apply
90+ * @param cropWidth e.g. 0.2 will leave 20% of the initial width
8991 * @param cropHeight e.g. 0.1 will leave 10% of the initial width
9092 * @return instance of type Snapshot
9193 */
92- public T withCroppedThumbnail (String path , String name , double scale , double cropWidth , double cropHeight ) {
94+ public T withCroppedThumbnail (String path , String name , double scale , double cropWidth , double cropHeight ) {
9395 File thumbnailFile = getFile (path , name );
94- thumbnailImage = ImageProcessor .cropAndScale (image ,scale , cropWidth , cropHeight );
96+ thumbnailImage = ImageProcessor .cropAndScale (image , scale , cropWidth , cropHeight );
9597 FileUtil .writeImage (thumbnailImage , EXTENSION , thumbnailFile );
9698 return self ();
9799 }
@@ -100,16 +102,16 @@ public T withCroppedThumbnail(String path, String name, double scale, double cr
100102 * Generate cropped thumbnail of the original screenshot.
101103 * Will save different thumbnails depends on when it was called in the chain.
102104 *
103- * @param path to save thumbnail image to
104- * @param name of the resulting image
105- * @param scale to apply
106- * @param maxWidth max width in pixels. If set to -1 the actual image width is used
105+ * @param path to save thumbnail image to
106+ * @param name of the resulting image
107+ * @param scale to apply
108+ * @param maxWidth max width in pixels. If set to -1 the actual image width is used
107109 * @param maxHeight max height in pixels. If set to -1 the actual image height is used
108110 * @return instance of type Snapshot
109111 */
110- public T withCroppedThumbnail (String path , String name , double scale , int maxWidth , int maxHeight ) {
112+ public T withCroppedThumbnail (String path , String name , double scale , int maxWidth , int maxHeight ) {
111113 File thumbnailFile = getFile (path , name );
112- thumbnailImage = ImageProcessor .cropAndScale (image ,scale , maxWidth , maxHeight );
114+ thumbnailImage = ImageProcessor .cropAndScale (image , scale , maxWidth , maxHeight );
113115 FileUtil .writeImage (thumbnailImage , EXTENSION , thumbnailFile );
114116 return self ();
115117 }
@@ -133,39 +135,39 @@ private File getFile(String path, String name) {
133135 * Generate cropped thumbnail of the original screenshot.
134136 * Will save different thumbnails depends on when it was called in the chain.
135137 *
136- * @param scale to apply
137- * @param cropWidth e.g. 0.2 will leave 20% of the initial width
138+ * @param scale to apply
139+ * @param cropWidth e.g. 0.2 will leave 20% of the initial width
138140 * @param cropHeight e.g. 0.1 will leave 10% of the initial width
139141 * @return instance of type Snapshot
140142 */
141143 public T withCroppedThumbnail (double scale , double cropWidth , double cropHeight ) {
142- return withCroppedThumbnail (Paths .get (location .toString (), "./thumbnails" ).toString (), "thumb_" + fileName , scale ,cropWidth ,cropHeight );
144+ return withCroppedThumbnail (Paths .get (location .toString (), "./thumbnails" ).toString (), "thumb_" + fileName , scale , cropWidth , cropHeight );
143145 }
144-
146+
145147 /**
146148 * Generate cropped thumbnail of the original screenshot.
147149 * Will save different thumbnails depends on when it was called in the chain.
148150 *
149- * @param scale to apply
150- * @param maxWidth max width in pixels. If set to -1 the actual image width is used
151+ * @param scale to apply
152+ * @param maxWidth max width in pixels. If set to -1 the actual image width is used
151153 * @param maxHeight max height in pixels. If set to -1 the actual image height is used
152154 * @return instance of type Snapshot
153155 */
154156 public T withCroppedThumbnail (double scale , int maxWidth , int maxHeight ) {
155- return withCroppedThumbnail (Paths .get (location .toString (), "./thumbnails" ).toString (), "thumb_" + fileName , scale , maxWidth ,maxHeight );
157+ return withCroppedThumbnail (Paths .get (location .toString (), "./thumbnails" ).toString (), "thumb_" + fileName , scale , maxWidth , maxHeight );
156158 }
157-
159+
158160 /**
159161 * Generate a thumbnail of the original screenshot.
160162 * Will save different thumbnails depends on when it was called in the chain.
161163 *
162- * @param path to save thumbnail image to
163- * @param name of the resulting image
164+ * @param path to save thumbnail image to
165+ * @param name of the resulting image
164166 * @param scale to apply
165167 * @return instance of type Snapshot
166168 */
167169 public T withThumbnail (Path path , String name , double scale ) {
168- return withThumbnail (path .toString (),name ,scale );
170+ return withThumbnail (path .toString (), name , scale );
169171 }
170172
171173 /**
@@ -206,7 +208,7 @@ protected void setImage(BufferedImage image) {
206208 */
207209 public void save () {
208210 File screenshotFile = new File (location .toString (), fileName );
209- if (!Files .exists (location )) {
211+ if (!Files .exists (location )) {
210212 screenshotFile .mkdirs ();
211213 }
212214 if (title != null && !title .isEmpty ()) {
@@ -218,15 +220,16 @@ public void save() {
218220 /**
219221 * Final method to be called in the chain.
220222 * Actually saves processed image to the specified path.
221- * @param path to save image to
223+ *
224+ * @param path to save image to
222225 */
223226 public void save (String path ) {
224227 this .location = Paths .get (path );
225228 save ();
226229 }
227230
228231 /**
229- * @param other Snapshot to compare with
232+ * @param other Snapshot to compare with
230233 * @param deviation allowed deviation while comparing.
231234 * @return true if the the percentage of differences
232235 * between current image and provided one is less than or equal to <b>deviation</b>
@@ -256,12 +259,20 @@ public boolean equals(Object o) {
256259 * @return true if the the provided image and current image are strictly equal.
257260 */
258261 public boolean equals (BufferedImage image ) {
259- if (this .getImage () == image ) return true ;
260- return getImage () != null ? ImageProcessor .imagesAreEquals (getImage (), image , 0 ) : image == null ;
262+ return equals (image , 0 );
261263 }
262264
263265 /**
264- * @param image BufferedImage to compare with.
266+ * @param path path to image to compare to.
267+ * @return true if the the provided image and current image are strictly equal.
268+ * @throws IOException if unable to read image from path
269+ */
270+ public boolean equals (String path ) throws IOException {
271+ return equals (path , 0 );
272+ }
273+
274+ /**
275+ * @param image BufferedImage to compare with.
265276 * @param deviation allowed deviation while comparing.
266277 * @return true if the the percentage of differences
267278 * between current image and provided one is less than or equal to <b>deviation</b>
@@ -272,18 +283,31 @@ public boolean equals(BufferedImage image, double deviation) {
272283 }
273284
274285 /**
275- * @param image BufferedImage to compare with.
286+ * @param path path to image to compare to.
287+ * @param deviation allowed deviation while comparing.
288+ * @return true if the the percentage of differences
289+ * between current image and provided one is less than or equal to <b>deviation</b>
290+ * @throws IOException if unable to read image from path
291+ */
292+ public boolean equals (String path , double deviation ) throws IOException {
293+ BufferedImage image = ImageIO .read (new File (path ));
294+ if (this .getImage () == image ) return true ;
295+ return getImage () != null ? ImageProcessor .imagesAreEquals (getImage (), image , deviation ) : image == null ;
296+ }
297+
298+ /**
299+ * @param image BufferedImage to compare with.
276300 * @param resultingImagePath path with name to save to resulting images with diff
277301 * @return true if the the provided image and current image are strictly equal.
278302 */
279303 public boolean equalsWithDiff (BufferedImage image , String resultingImagePath ) {
280- return equalsWithDiff (image , resultingImagePath ,0 );
304+ return equalsWithDiff (image , resultingImagePath , 0 );
281305 }
282306
283307 /**
284- * @param image BufferedImage to compare with.
308+ * @param image BufferedImage to compare with.
285309 * @param resultingImagePath path with name to save to resulting images with diff
286- * @param deviation allowed deviation while comparing
310+ * @param deviation allowed deviation while comparing
287311 * @return true if the the provided image and current image are strictly equal.
288312 */
289313 public boolean equalsWithDiff (BufferedImage image , String resultingImagePath , double deviation ) {
@@ -292,23 +316,49 @@ public boolean equalsWithDiff(BufferedImage image, String resultingImagePath, do
292316 }
293317
294318 /**
295- * @param image Snapshot to compare with.
319+ * @param image Snapshot to compare with.
296320 * @param resultingImagePath path with name to save to resulting images with diff
297321 * @return true if the the provided image and current image are strictly equal.
298322 */
299323 public boolean equalsWithDiff (Snapshot image , String resultingImagePath ) {
300- return equalsWithDiff (image , resultingImagePath ,0 );
324+ return equalsWithDiff (image , resultingImagePath , 0 );
301325 }
302326
303327 /**
304- * @param image Snapshot to compare with.
328+ * @param image Snapshot to compare with.
305329 * @param resultingImagePath path with name to save to resulting images with diff
306- * @param deviation allowed deviation while comparing
330+ * @param deviation allowed deviation while comparing
307331 * @return true if the the provided image and current image are strictly equal.
308332 */
309333 public boolean equalsWithDiff (Snapshot image , String resultingImagePath , double deviation ) {
310334 if (this == image ) return true ;
311- return getImage () != null ? ImageProcessor .imagesAreEqualsWithDiff (getImage (), image .getImage (),resultingImagePath , deviation ) : image == null ;
335+ return getImage () != null ? ImageProcessor .imagesAreEqualsWithDiff (getImage (), image .getImage (), resultingImagePath , deviation ) : image == null ;
336+ }
337+
338+ /*===========*/
339+
340+ /**
341+ * @param path path to image to compare to
342+ * @param resultingImagePath path with name to save to resulting images with diff
343+ * @return true if the the provided image and current image are strictly equal.
344+ * @throws IOException if unable to read image from path
345+ */
346+ public boolean equalsWithDiff (String path , String resultingImagePath ) throws IOException {
347+ return equalsWithDiff (path , resultingImagePath , 0 );
348+ }
349+
350+ /**
351+ * @param path BufferedImage to compare with.
352+ * @param resultingImagePath path with name to save to resulting images with diff
353+ * @param deviation allowed deviation while comparing
354+ * @return true if the the provided image and current image are strictly equal.
355+ * @throws IOException if unable to read image from path
356+ */
357+ public boolean equalsWithDiff (String path , String resultingImagePath ,
358+ double deviation ) throws IOException {
359+ BufferedImage image = ImageIO .read (new File (path ));
360+ if (this .getImage () == image ) return true ;
361+ return getImage () != null ? ImageProcessor .imagesAreEqualsWithDiff (getImage (), image , resultingImagePath , deviation ) : image == null ;
312362 }
313363
314364 /**
0 commit comments