Skip to content

Commit 17edace

Browse files
committed
Embed images in log.html
Images are now embedded into the output log file with base64 encoding. This fixes visibility problems caused by incorrect paths when files are moved between systems and enables logging images without transferring any files on remote mode (Closes #5 and closes #6). Also added keyword Set Image Logging which allows users to toggle image logging on and off. When logging is set to off, automatic captures on library keywords failing will not be taken at all. As an example use case all tests containing 'negative' tag now have image logging disabled in library's acceptance test suite.
1 parent 9bb86f6 commit 17edace

22 files changed

+168
-84
lines changed

src/main/java/javafxlibrary/keywords/AdditionalKeywords/RunOnFailure.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ public void runOnFailure() {
5757

5858
if (robot == null) {
5959
RobotLog.error("FxRobot not initialized, launch test application with the library");
60-
} else {
60+
} else if (TestFxAdapter.logImages) {
6161
RobotLog.info("JavaFxLibrary keyword has failed! Below a screenshot from erroneous situation:");
6262
if (robot.targetWindow() != null) {
6363
new ScreenCapturing().captureImage(robot.targetWindow());
6464
} else
6565
new ScreenCapturing().captureImage(Screen.getPrimary().getBounds());
66+
} else {
67+
RobotLog.info("JavaFXLibrary keyword has failed!");
68+
RobotLog.info("Not taking a screenshot since Set Image Logging is set to off. To enable screenshots " +
69+
"on failed keywords, use keyword 'Set Image Logging ON'");
6670
}
6771

6872
runningOnFailureRoutine = false;

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,43 @@
2222
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
2323
import javafxlibrary.utils.RobotLog;
2424
import javafxlibrary.utils.TestFxAdapter;
25+
import org.apache.commons.io.IOUtils;
2526
import org.robotframework.javalib.annotation.ArgumentNames;
2627
import org.robotframework.javalib.annotation.RobotKeyword;
2728
import org.robotframework.javalib.annotation.RobotKeywordOverload;
2829
import org.robotframework.javalib.annotation.RobotKeywords;
2930
import javafx.scene.image.Image;
3031
import javax.imageio.ImageIO;
3132
import java.io.File;
33+
import java.io.FileInputStream;
34+
import java.io.IOException;
3235
import java.net.URL;
3336
import java.nio.file.Path;
3437
import java.nio.file.Paths;
3538
import java.time.ZonedDateTime;
3639
import java.time.format.DateTimeFormatter;
40+
import java.util.Base64;
41+
3742
import static javafxlibrary.utils.HelperFunctions.*;
3843

3944
@RobotKeywords
4045
public class ScreenCapturing extends TestFxAdapter {
4146

47+
@RobotKeyword
48+
@ArgumentNames({ "value" })
49+
public void setImageLogging(String value) {
50+
if (value.toLowerCase().equals("on"))
51+
TestFxAdapter.logImages = true;
52+
else if (value.toLowerCase().equals("off"))
53+
TestFxAdapter.logImages = false;
54+
else
55+
throw new JavaFXLibraryNonFatalException("Value \"" + value + "\" is not supported! Value must be either " +
56+
"\"ON\" or \"OFF\"");
57+
}
58+
4259
@RobotKeywordOverload
4360
public Object captureImage(Object locator){
44-
return captureImage(locator, true);
61+
return captureImage(locator, TestFxAdapter.logImages);
4562
}
4663

4764
@RobotKeyword("Returns a screenshot of the given locator.\n\n"
@@ -56,7 +73,7 @@ public Object captureImage(Object locator){
5673
+ "| ${capture}= | Capture Image | \\#id | logImage=False |\n" )
5774
@ArgumentNames({"locator", "logImage=True"})
5875
public Object captureImage(Object locator, boolean logImage){
59-
if(locator == null)
76+
if (locator == null)
6077
throw new JavaFXLibraryNonFatalException("Unable to capture image, given locator was null!");
6178

6279
RobotLog.info("Capturing screenshot from locator: \"" + locator + "\"");
@@ -67,18 +84,24 @@ public Object captureImage(Object locator, boolean logImage){
6784
image = robot.capture(targetBounds).getImage();
6885
Path path = createNewImageFileNameWithPath();
6986
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);
7092

71-
if(logImage) {
93+
if (logImage) {
7294
Double printSize = ( targetBounds.getWidth() > 800 ) ? 800 : targetBounds.getWidth();
73-
System.out.println("*HTML* <img src=\"" + path + "\" width=\"" + printSize + "px\">");
95+
System.out.println("*HTML* <img src=\"data:image/png;base64," + encodedImage + "\" width=\"" + printSize + "px\">");
7496
}
75-
7697
return mapObject(image);
7798

99+
} catch (IOException e) {
100+
throw new JavaFXLibraryNonFatalException("Unable to take capture : \"" + locator + "\"", e);
78101
} catch (Exception e) {
79-
if(e instanceof JavaFXLibraryNonFatalException)
102+
if (e instanceof JavaFXLibraryNonFatalException)
80103
throw e;
81-
throw new JavaFXLibraryNonFatalException("Unable to take capture : \"" + locator.toString() + "\"", e);
104+
throw new JavaFXLibraryNonFatalException("Unable to take capture : \"" + locator + "\"", e);
82105
}
83106
}
84107

src/main/java/javafxlibrary/utils/TestFxAdapter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public static void setRobotContext(FxRobotContext context) {
4444
// TODO: consider adding support for multiple sessions
4545
private static Session activeSession = null;
4646

47+
protected static boolean logImages = true;
48+
4749
// internal book keeping for objects
4850
public static HashMap objectMap = new HashMap();
4951

src/test/robotframework/acceptance/0_ClickRobotTest.robot

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
*** Settings ***
2-
Documentation Tests to test javafxlibrary.keywords.ClickRobot related keywords
3-
Resource ../resource.robot
4-
Suite Setup Setup all tests
5-
Suite Teardown Teardown all tests
6-
Test Setup Reset Counters
7-
Force tags set-clickrobot
2+
Documentation Tests to test javafxlibrary.keywords.ClickRobot related keywords
3+
Resource ../resource.robot
4+
Suite Setup Setup all tests
5+
Suite Teardown Teardown all tests
6+
Test Setup Setup test case
7+
Test Teardown Enable Image Logging
8+
Force tags set-clickrobot
89
910
*** Variables ***
1011
${TEST_APPLICATION} javafxlibrary.testapps.TestClickRobot
@@ -263,6 +264,10 @@ Setup all tests
263264
Set Window Values
264265
Set Timeout 1
265266
267+
Setup test case
268+
Reset Counters
269+
Disable Image Logging For Negative Tests
270+
266271
Teardown all tests
267272
Close Javafx Application
268273

src/test/robotframework/acceptance/BoundsLocationTest.robot

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
*** Settings ***
2-
Documentation Tests to test javafxlibrary.keywords.BoundsLocation related keywords
3-
Resource ../resource.robot
4-
Suite Setup Setup all tests
5-
Suite Teardown Teardown all tests
6-
Force Tags set-boundslocation
2+
Documentation Tests to test javafxlibrary.keywords.BoundsLocation related keywords
3+
Resource ../resource.robot
4+
Suite Setup Setup all tests
5+
Suite Teardown Teardown all tests
6+
Test Setup Disable Image Logging For Negative Tests
7+
Test Teardown Enable Image Logging
8+
Force Tags set-boundslocation
79
810
*** Variables ***
911
${TEST_APPLICATION} javafxlibrary.testapps.TestBoundsLocation

src/test/robotframework/acceptance/DatePickerTest.robot

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
*** Settings ***
2-
Documentation Tests to test DatePicker related keywords
3-
Resource ../resource.robot
4-
Suite Setup Setup all tests
5-
Suite Teardown Teardown all tests
6-
Test Teardown Clear Text Input css=.text-field
7-
Force Tags set-datepicker
2+
Documentation Tests to test DatePicker related keywords
3+
Resource ../resource.robot
4+
Suite Setup Setup all tests
5+
Suite Teardown Teardown all tests
6+
Test Setup Disable Image Logging For Negative Tests
7+
Test Teardown Teardown test case
8+
Force Tags set-datepicker
89

910
*** Variables ***
1011
${TEST_APPLICATION} javafxlibrary.testapps.DatePickerApp
@@ -40,6 +41,10 @@ Setup all tests
4041
Teardown all tests
4142
Close Javafx Application
4243

44+
Teardown test case
45+
Clear Text Input css=.text-field
46+
Enable Image Logging
47+
4348
Set Year
4449
[Arguments] ${year}
4550
${time_labels} Find All css=.spinner-label

src/test/robotframework/acceptance/DragRobotTest.robot

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
*** Settings ***
2-
Documentation Tests to test javafxlibrary.keywords.DragRobot related keywords
3-
Resource ../resource.robot
4-
Suite Setup Setup all tests
5-
Suite Teardown Teardown all tests
6-
Force tags set-dragrobot
2+
Documentation Tests to test javafxlibrary.keywords.DragRobot related keywords
3+
Resource ../resource.robot
4+
Suite Setup Setup all tests
5+
Suite Teardown Teardown all tests
6+
Test Setup Disable Image Logging For Negative Tests
7+
Test Teardown Enable Image Logging
8+
Force tags set-dragrobot
79
810
*** Variables ***
911
${TEST_APPLICATION} javafxlibrary.testapps.TestDragRobot

src/test/robotframework/acceptance/FindTest.robot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Documentation Tests to test javafxlibrary.keywords.AdditionalKeywords.Find
33
Resource ../resource.robot
44
Suite Setup Setup All Tests
55
Suite Teardown Teardown all tests
6+
Test Setup Disable Image Logging For Negative Tests
7+
Test Teardown Enable Image Logging
68
Force Tags set-find
79

810
*** Variables ***

src/test/robotframework/acceptance/KeyboardRobotTest.robot

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
*** Settings ***
2-
Documentation Tests to test javafxlibrary.keywords.KeyboardRobot related keywords
3-
Resource ../resource.robot
4-
Suite Setup Setup all tests
5-
Suite Teardown Teardown all tests
6-
Force Tags set-keyboardrobot
2+
Documentation Tests to test javafxlibrary.keywords.KeyboardRobot related keywords
3+
Resource ../resource.robot
4+
Suite Setup Setup all tests
5+
Suite Teardown Teardown all tests
6+
Test Setup Disable Image Logging For Negative Tests
7+
Test Teardown Enable Image Logging
8+
Force Tags set-keyboardrobot
79
810
*** Variables ***
911
${TEST_APPLICATION} javafxlibrary.testapps.TestKeyboardRobot

src/test/robotframework/acceptance/MenuAppTest.robot

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
*** Settings ***
2-
Documentation Tests to test javafxlibrary keywords
3-
Resource ../resource.robot
4-
Suite Setup Setup all tests
5-
Suite Teardown Teardown all tests
6-
Force Tags set-menuapp
2+
Documentation Tests to test javafxlibrary keywords
3+
Resource ../resource.robot
4+
Suite Setup Setup all tests
5+
Suite Teardown Teardown all tests
6+
Test Setup Disable Image Logging For Negative Tests
7+
Test Teardown Enable Image Logging
8+
Force Tags set-menuapp
79
810
*** Variables ***
911
${TEST_APPLICATION} javafxlibrary.testapps.MenuApp

0 commit comments

Comments
 (0)