32
32
import java .io .File ;
33
33
import java .io .FileInputStream ;
34
34
import java .io .IOException ;
35
+ import java .net .MalformedURLException ;
35
36
import java .net .URL ;
36
37
import java .nio .file .Path ;
37
38
import java .nio .file .Paths ;
@@ -84,14 +85,22 @@ public Object captureImage(Object locator, boolean logImage){
84
85
image = robot .capture (targetBounds ).getImage ();
85
86
Path path = createNewImageFileNameWithPath ();
86
87
robotContext .getCaptureSupport ().saveImage (image , path );
87
- File imageFile = path .toFile ();
88
- /* TODO: Copy and resize image to a temporary file and embed that instead
89
- Add path to the original file in logs in case greater resolution is needed */
90
- byte [] imageBytes = IOUtils .toByteArray (new FileInputStream (imageFile ));
91
- String encodedImage = Base64 .getEncoder ().encodeToString (imageBytes );
92
88
93
89
if (logImage ) {
94
- Double printSize = ( targetBounds .getWidth () > 800 ) ? 800 : targetBounds .getWidth ();
90
+ Image resizedImage = resizeImage (image , path );
91
+ Path tempPath = Paths .get (getCurrentSessionScreenshotDirectory (), "temp.png" );
92
+ robotContext .getCaptureSupport ().saveImage (resizedImage , tempPath );
93
+
94
+ File imageFile = tempPath .toFile ();
95
+ byte [] imageBytes = IOUtils .toByteArray (new FileInputStream (imageFile ));
96
+ String encodedImage = Base64 .getEncoder ().encodeToString (imageBytes );
97
+
98
+ if (imageFile .delete ())
99
+ RobotLog .debug ("Deleted temporary image file successfully." );
100
+ else
101
+ RobotLog .debug ("Could not delete the file: " + imageFile .toString ());
102
+
103
+ Double printSize = targetBounds .getWidth () > 800 ? 800 : targetBounds .getWidth ();
95
104
System .out .println ("*HTML* <img src=\" data:image/png;base64," + encodedImage + "\" width=\" " + printSize + "px\" >" );
96
105
}
97
106
return mapObject (image );
@@ -159,11 +168,28 @@ private Path createNewImageFileNameWithPath(){
159
168
File errDir = new File (errorImageFilePath );
160
169
if (!errDir .exists ())
161
170
errDir .mkdirs ();
162
- return Paths .get ( errorImageFilePath , errorImageFilename );
171
+ return Paths .get (errorImageFilePath , errorImageFilename );
163
172
}
164
173
165
174
private static String formatErrorTimestamp (ZonedDateTime dateTime , String dateTimePattern ) {
166
175
DateTimeFormatter formatter = DateTimeFormatter .ofPattern (dateTimePattern );
167
176
return dateTime .format (formatter );
168
177
}
178
+
179
+ private static Image resizeImage (Image image , Path path ) {
180
+ double width = image .getWidth ();
181
+ double height = image .getHeight ();
182
+
183
+ if (width < 800 )
184
+ return image ;
185
+
186
+ RobotLog .info ("Full resolution image can be found at " + path );
187
+ double multiplier = width / 800 ;
188
+ try {
189
+ String url = path .toUri ().toURL ().toString ();
190
+ return new Image (url , width / multiplier , height / multiplier , true , false );
191
+ } catch (MalformedURLException e ) {
192
+ throw new JavaFXLibraryNonFatalException ("Unable to log the screenshot: image resizing failed!" );
193
+ }
194
+ }
169
195
}
0 commit comments