Skip to content

Commit 95c424a

Browse files
committed
Fix screen capturing temp file usage, fixes #36
1 parent e8c4e05 commit 95c424a

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/main/java/javafxlibrary/keywords/Keywords/ScreenCapturing.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javafxlibrary.keywords.AdditionalKeywords.ConvenienceKeywords;
2626
import javafxlibrary.utils.RobotLog;
2727
import javafxlibrary.utils.TestFxAdapter;
28+
import org.apache.commons.io.FileUtils;
2829
import org.apache.commons.io.IOUtils;
2930
import org.robotframework.javalib.annotation.ArgumentNames;
3031
import org.robotframework.javalib.annotation.RobotKeyword;
@@ -97,18 +98,21 @@ public Object captureImage(Object locator, boolean logImage){
9798
robotContext().getCaptureSupport().saveImage(image, path);
9899

99100
if (logImage) {
100-
Double printSize = targetBounds.getWidth() > 800 ? 800 : targetBounds.getWidth();
101+
double printSize = targetBounds.getWidth() > 800 ? 800 : targetBounds.getWidth();
101102

102103
if(TestFxAdapter.logImages.toLowerCase().equals("embedded")) {
103104
Image resizedImage = resizeImage(image, path);
104105
Path tempPath = Paths.get(getCurrentSessionScreenshotDirectory(), "temp.png");
105106
robotContext().getCaptureSupport().saveImage(resizedImage, tempPath);
106107

107108
File imageFile = convertToJpeg(tempPath);
108-
byte[] imageBytes = IOUtils.toByteArray(new FileInputStream(imageFile));
109+
byte[] imageBytes = FileUtils.readFileToByteArray(imageFile);
109110
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+
}
112116
RobotLog.html("<a href=\"" + path + "\">"
113117
+ "<img title=\"Click for full size image\" src=\"data:image/png;base64," + encodedImage + "\" width=\"" + printSize + "px\">"
114118
+ "</a>");
@@ -131,7 +135,7 @@ public Object captureImage(Object locator, boolean logImage){
131135
}
132136
}
133137

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"
135139
+ "``locator`` is a query locator, see `3.1 Locator syntax`.\n\n "
136140
+ "\nExample:\n"
137141
+ "| ${capture}= | Capture Scene Containing Node | ${node} | \n" )
@@ -189,17 +193,19 @@ public void saveImageAs(Image image, String path) {
189193

190194
private Path createNewImageFileNameWithPath(){
191195
ZonedDateTime errorDateTime = ZonedDateTime.now();
192-
String errorTimestamp = formatErrorTimestamp(errorDateTime, "yyyyMMdd-HHmmss-SSS");
196+
String errorTimestamp = formatErrorTimestamp(errorDateTime);
193197
String errorImageFilename = "JavaFXLib-" + errorTimestamp + ".png";
194198
String errorImageFilePath = getCurrentSessionScreenshotDirectory();
195199
File errDir = new File(errorImageFilePath);
196200
if(!errDir.exists())
197-
errDir.mkdirs();
201+
if (!errDir.mkdirs()) {
202+
RobotLog.warn("Capture image directory \"" + errorImageFilePath + "\" creation failed.");
203+
}
198204
return Paths.get(errorImageFilePath, errorImageFilename);
199205
}
200206

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");
203209
return dateTime.format(formatter);
204210
}
205211

@@ -225,7 +231,11 @@ private File convertToJpeg(Path path) throws IOException {
225231
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
226232
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
227233
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+
}
229239
Path tempPathJpeg = Paths.get(getCurrentSessionScreenshotDirectory(), "temp.jpg");
230240
ImageIO.write(newBufferedImage, "jpg", tempPathJpeg.toFile());
231241
return tempPathJpeg.toFile();

0 commit comments

Comments
 (0)