25
25
import javafxlibrary .keywords .AdditionalKeywords .ConvenienceKeywords ;
26
26
import javafxlibrary .utils .RobotLog ;
27
27
import javafxlibrary .utils .TestFxAdapter ;
28
+ import org .apache .commons .io .FileUtils ;
28
29
import org .apache .commons .io .IOUtils ;
29
30
import org .robotframework .javalib .annotation .ArgumentNames ;
30
31
import org .robotframework .javalib .annotation .RobotKeyword ;
@@ -97,18 +98,21 @@ public Object captureImage(Object locator, boolean logImage){
97
98
robotContext ().getCaptureSupport ().saveImage (image , path );
98
99
99
100
if (logImage ) {
100
- Double printSize = targetBounds .getWidth () > 800 ? 800 : targetBounds .getWidth ();
101
+ double printSize = targetBounds .getWidth () > 800 ? 800 : targetBounds .getWidth ();
101
102
102
103
if (TestFxAdapter .logImages .toLowerCase ().equals ("embedded" )) {
103
104
Image resizedImage = resizeImage (image , path );
104
105
Path tempPath = Paths .get (getCurrentSessionScreenshotDirectory (), "temp.png" );
105
106
robotContext ().getCaptureSupport ().saveImage (resizedImage , tempPath );
106
107
107
108
File imageFile = convertToJpeg (tempPath );
108
- byte [] imageBytes = IOUtils . toByteArray ( new FileInputStream ( imageFile ) );
109
+ byte [] imageBytes = FileUtils . readFileToByteArray ( imageFile );
109
110
String encodedImage = Base64 .getEncoder ().encodeToString (imageBytes );
110
- imageFile .delete ();
111
-
111
+ if (imageFile .exists ()) {
112
+ if (!imageFile .delete ()) {
113
+ RobotLog .warn ("Capture temporary image \" " + imageFile .getAbsolutePath () + "\" deletion failed." );
114
+ }
115
+ }
112
116
RobotLog .html ("<a href=\" " + path + "\" >"
113
117
+ "<img title=\" Click for full size image\" src=\" data:image/png;base64," + encodedImage + "\" width=\" " + printSize + "px\" >"
114
118
+ "</a>" );
@@ -131,7 +135,7 @@ public Object captureImage(Object locator, boolean logImage){
131
135
}
132
136
}
133
137
134
- @ RobotKeyword ("Returns a screenshot of the scene conatining given locator.\n \n "
138
+ @ RobotKeyword ("Returns a screenshot of the scene containing given locator.\n \n "
135
139
+ "``locator`` is a query locator, see `3.1 Locator syntax`.\n \n "
136
140
+ "\n Example:\n "
137
141
+ "| ${capture}= | Capture Scene Containing Node | ${node} | \n " )
@@ -189,17 +193,19 @@ public void saveImageAs(Image image, String path) {
189
193
190
194
private Path createNewImageFileNameWithPath (){
191
195
ZonedDateTime errorDateTime = ZonedDateTime .now ();
192
- String errorTimestamp = formatErrorTimestamp (errorDateTime , "yyyyMMdd-HHmmss-SSS" );
196
+ String errorTimestamp = formatErrorTimestamp (errorDateTime );
193
197
String errorImageFilename = "JavaFXLib-" + errorTimestamp + ".png" ;
194
198
String errorImageFilePath = getCurrentSessionScreenshotDirectory ();
195
199
File errDir = new File (errorImageFilePath );
196
200
if (!errDir .exists ())
197
- errDir .mkdirs ();
201
+ if (!errDir .mkdirs ()) {
202
+ RobotLog .warn ("Capture image directory \" " + errorImageFilePath + "\" creation failed." );
203
+ }
198
204
return Paths .get (errorImageFilePath , errorImageFilename );
199
205
}
200
206
201
- private static String formatErrorTimestamp (ZonedDateTime dateTime , String dateTimePattern ) {
202
- DateTimeFormatter formatter = DateTimeFormatter .ofPattern (dateTimePattern );
207
+ private static String formatErrorTimestamp (ZonedDateTime dateTime ) {
208
+ DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyyMMdd-HHmmss-SSS" );
203
209
return dateTime .format (formatter );
204
210
}
205
211
@@ -225,7 +231,11 @@ private File convertToJpeg(Path path) throws IOException {
225
231
BufferedImage newBufferedImage = new BufferedImage (bufferedImage .getWidth (),
226
232
bufferedImage .getHeight (), BufferedImage .TYPE_INT_RGB );
227
233
newBufferedImage .createGraphics ().drawImage (bufferedImage , 0 , 0 , java .awt .Color .WHITE , null );
228
- path .toFile ().delete ();
234
+ if (path .toFile ().exists ()) {
235
+ if (!path .toFile ().delete ()) {
236
+ RobotLog .warn ("Capture temporary image \" " + path + "\" deletion failed." );
237
+ }
238
+ }
229
239
Path tempPathJpeg = Paths .get (getCurrentSessionScreenshotDirectory (), "temp.jpg" );
230
240
ImageIO .write (newBufferedImage , "jpg" , tempPathJpeg .toFile ());
231
241
return tempPathJpeg .toFile ();
0 commit comments