9
9
import com .assertthat .selenium_shutterbug .utils .image .ImageProcessor ;
10
10
import org .openqa .selenium .WebDriver ;
11
11
12
+ import javax .imageio .ImageIO ;
12
13
import java .awt .*;
13
14
import java .awt .image .BufferedImage ;
14
15
import java .io .File ;
16
+ import java .io .IOException ;
15
17
import java .nio .file .Files ;
16
18
import java .nio .file .Path ;
17
19
import java .nio .file .Paths ;
23
25
*/
24
26
public abstract class Snapshot <T extends Snapshot > {
25
27
28
+ static final String ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE = "Requested element is outside the viewport" ;
26
29
private static final String EXTENSION = "PNG" ;
27
- protected static final String ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE = "Requested element is outside the viewport" ;
28
30
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 ;
32
34
private String fileName = new SimpleDateFormat ("yyyy_MM_dd_HH_mm_ss_SSS" ).format (new Date ())
33
35
+ "." + EXTENSION .toLowerCase ();
34
36
private Path location = Paths .get ("./screenshots/" );
@@ -63,35 +65,35 @@ public T withTitle(String title) {
63
65
* Generate a thumbnail of the original screenshot.
64
66
* Will save different thumbnails depends on when it was called in the chain.
65
67
*
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
68
70
* @param scale to apply
69
71
* @return instance of type Snapshot
70
72
*/
71
73
public T withThumbnail (String path , String name , double scale ) {
72
74
File thumbnailFile = new File (path , name );
73
- if (!Files .exists (Paths .get (path ))) {
75
+ if (!Files .exists (Paths .get (path ))) {
74
76
thumbnailFile .mkdirs ();
75
77
}
76
- thumbnailImage = ImageProcessor .scale (image ,scale );
78
+ thumbnailImage = ImageProcessor .scale (image , scale );
77
79
FileUtil .writeImage (thumbnailImage , EXTENSION , thumbnailFile );
78
80
return self ();
79
81
}
80
-
82
+
81
83
/**
82
84
* Generate cropped thumbnail of the original screenshot.
83
85
* Will save different thumbnails depends on when it was called in the chain.
84
86
*
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
89
91
* @param cropHeight e.g. 0.1 will leave 10% of the initial width
90
92
* @return instance of type Snapshot
91
93
*/
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 ) {
93
95
File thumbnailFile = getFile (path , name );
94
- thumbnailImage = ImageProcessor .cropAndScale (image ,scale , cropWidth , cropHeight );
96
+ thumbnailImage = ImageProcessor .cropAndScale (image , scale , cropWidth , cropHeight );
95
97
FileUtil .writeImage (thumbnailImage , EXTENSION , thumbnailFile );
96
98
return self ();
97
99
}
@@ -100,16 +102,16 @@ public T withCroppedThumbnail(String path, String name, double scale, double cr
100
102
* Generate cropped thumbnail of the original screenshot.
101
103
* Will save different thumbnails depends on when it was called in the chain.
102
104
*
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
107
109
* @param maxHeight max height in pixels. If set to -1 the actual image height is used
108
110
* @return instance of type Snapshot
109
111
*/
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 ) {
111
113
File thumbnailFile = getFile (path , name );
112
- thumbnailImage = ImageProcessor .cropAndScale (image ,scale , maxWidth , maxHeight );
114
+ thumbnailImage = ImageProcessor .cropAndScale (image , scale , maxWidth , maxHeight );
113
115
FileUtil .writeImage (thumbnailImage , EXTENSION , thumbnailFile );
114
116
return self ();
115
117
}
@@ -133,39 +135,39 @@ private File getFile(String path, String name) {
133
135
* Generate cropped thumbnail of the original screenshot.
134
136
* Will save different thumbnails depends on when it was called in the chain.
135
137
*
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
138
140
* @param cropHeight e.g. 0.1 will leave 10% of the initial width
139
141
* @return instance of type Snapshot
140
142
*/
141
143
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 );
143
145
}
144
-
146
+
145
147
/**
146
148
* Generate cropped thumbnail of the original screenshot.
147
149
* Will save different thumbnails depends on when it was called in the chain.
148
150
*
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
151
153
* @param maxHeight max height in pixels. If set to -1 the actual image height is used
152
154
* @return instance of type Snapshot
153
155
*/
154
156
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 );
156
158
}
157
-
159
+
158
160
/**
159
161
* Generate a thumbnail of the original screenshot.
160
162
* Will save different thumbnails depends on when it was called in the chain.
161
163
*
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
164
166
* @param scale to apply
165
167
* @return instance of type Snapshot
166
168
*/
167
169
public T withThumbnail (Path path , String name , double scale ) {
168
- return withThumbnail (path .toString (),name ,scale );
170
+ return withThumbnail (path .toString (), name , scale );
169
171
}
170
172
171
173
/**
@@ -206,7 +208,7 @@ protected void setImage(BufferedImage image) {
206
208
*/
207
209
public void save () {
208
210
File screenshotFile = new File (location .toString (), fileName );
209
- if (!Files .exists (location )) {
211
+ if (!Files .exists (location )) {
210
212
screenshotFile .mkdirs ();
211
213
}
212
214
if (title != null && !title .isEmpty ()) {
@@ -218,15 +220,16 @@ public void save() {
218
220
/**
219
221
* Final method to be called in the chain.
220
222
* Actually saves processed image to the specified path.
221
- * @param path to save image to
223
+ *
224
+ * @param path to save image to
222
225
*/
223
226
public void save (String path ) {
224
227
this .location = Paths .get (path );
225
228
save ();
226
229
}
227
230
228
231
/**
229
- * @param other Snapshot to compare with
232
+ * @param other Snapshot to compare with
230
233
* @param deviation allowed deviation while comparing.
231
234
* @return true if the the percentage of differences
232
235
* between current image and provided one is less than or equal to <b>deviation</b>
@@ -256,12 +259,20 @@ public boolean equals(Object o) {
256
259
* @return true if the the provided image and current image are strictly equal.
257
260
*/
258
261
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 );
261
263
}
262
264
263
265
/**
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.
265
276
* @param deviation allowed deviation while comparing.
266
277
* @return true if the the percentage of differences
267
278
* 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) {
272
283
}
273
284
274
285
/**
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.
276
300
* @param resultingImagePath path with name to save to resulting images with diff
277
301
* @return true if the the provided image and current image are strictly equal.
278
302
*/
279
303
public boolean equalsWithDiff (BufferedImage image , String resultingImagePath ) {
280
- return equalsWithDiff (image , resultingImagePath ,0 );
304
+ return equalsWithDiff (image , resultingImagePath , 0 );
281
305
}
282
306
283
307
/**
284
- * @param image BufferedImage to compare with.
308
+ * @param image BufferedImage to compare with.
285
309
* @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
287
311
* @return true if the the provided image and current image are strictly equal.
288
312
*/
289
313
public boolean equalsWithDiff (BufferedImage image , String resultingImagePath , double deviation ) {
@@ -292,23 +316,49 @@ public boolean equalsWithDiff(BufferedImage image, String resultingImagePath, do
292
316
}
293
317
294
318
/**
295
- * @param image Snapshot to compare with.
319
+ * @param image Snapshot to compare with.
296
320
* @param resultingImagePath path with name to save to resulting images with diff
297
321
* @return true if the the provided image and current image are strictly equal.
298
322
*/
299
323
public boolean equalsWithDiff (Snapshot image , String resultingImagePath ) {
300
- return equalsWithDiff (image , resultingImagePath ,0 );
324
+ return equalsWithDiff (image , resultingImagePath , 0 );
301
325
}
302
326
303
327
/**
304
- * @param image Snapshot to compare with.
328
+ * @param image Snapshot to compare with.
305
329
* @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
307
331
* @return true if the the provided image and current image are strictly equal.
308
332
*/
309
333
public boolean equalsWithDiff (Snapshot image , String resultingImagePath , double deviation ) {
310
334
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 ;
312
362
}
313
363
314
364
/**
0 commit comments