3232import java .io .File ;
3333import java .io .FileInputStream ;
3434import java .io .IOException ;
35+ import java .net .MalformedURLException ;
3536import java .net .URL ;
3637import java .nio .file .Path ;
3738import java .nio .file .Paths ;
@@ -84,14 +85,22 @@ public Object captureImage(Object locator, boolean logImage){
8485 image = robot .capture (targetBounds ).getImage ();
8586 Path path = createNewImageFileNameWithPath ();
8687 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 );
9288
9389 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 ();
95104 System .out .println ("*HTML* <img src=\" data:image/png;base64," + encodedImage + "\" width=\" " + printSize + "px\" >" );
96105 }
97106 return mapObject (image );
@@ -159,11 +168,28 @@ private Path createNewImageFileNameWithPath(){
159168 File errDir = new File (errorImageFilePath );
160169 if (!errDir .exists ())
161170 errDir .mkdirs ();
162- return Paths .get ( errorImageFilePath , errorImageFilename );
171+ return Paths .get (errorImageFilePath , errorImageFilename );
163172 }
164173
165174 private static String formatErrorTimestamp (ZonedDateTime dateTime , String dateTimePattern ) {
166175 DateTimeFormatter formatter = DateTimeFormatter .ofPattern (dateTimePattern );
167176 return dateTime .format (formatter );
168177 }
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+ }
169195}
0 commit comments