Skip to content

Commit f56306a

Browse files
committed
Capturing active window if locator is not given
Capturing full screen in case of error (as focus is not probably set to window that is interesting Possibility to capture whole (primary) screen as a separate keyword Fixes #19
1 parent 599c6df commit f56306a

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818
package javafxlibrary.keywords.AdditionalKeywords;
1919

20-
import javafx.stage.Screen;
20+
import javafx.geometry.Rectangle2D;
2121
import javafxlibrary.keywords.Keywords.ScreenCapturing;
2222
import javafxlibrary.utils.RobotLog;
2323
import javafxlibrary.utils.TestFxAdapter;
24+
25+
import java.awt.GraphicsDevice;
26+
import java.awt.GraphicsEnvironment;
27+
2428
import org.robotframework.javalib.annotation.RobotKeywords;
2529

2630
@RobotKeywords
@@ -30,7 +34,7 @@ public class RunOnFailure extends TestFxAdapter{
3034
private String runOnFailureKeyword = "Take Screenshot";
3135

3236
// Only run keyword on failure if false
33-
private boolean runningOnFailureRoutine = false;
37+
private static boolean runningOnFailureRoutine = false;
3438

3539

3640
// ##############################
@@ -55,17 +59,19 @@ public void runOnFailure() {
5559

5660
runningOnFailureRoutine = true;
5761

58-
RobotLog.info("JavaFXLibrary keyword has failed!");
59-
if (robot == null) {
60-
RobotLog.error("FxRobot not initialized, launch test application with the library");
61-
}
62-
63-
if (robot.targetWindow() != null) {
64-
new ScreenCapturing().captureImage(robot.targetWindow());
65-
} else {
66-
new ScreenCapturing().captureImage(Screen.getPrimary().getBounds());
67-
}
68-
69-
runningOnFailureRoutine = false;
62+
try {
63+
RobotLog.info("JavaFXLibrary keyword has failed!");
64+
if (robot == null) {
65+
RobotLog.error("FxRobot not initialized, launch test application with the library");
66+
} else {
67+
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
68+
RobotLog.debug("Capturing screenshot from primary screen with resolution "+gd.getDisplayMode().getWidth()+"x"+gd.getDisplayMode().getHeight()+".");
69+
new ScreenCapturing().captureImage(new Rectangle2D(0, 0, gd.getDisplayMode().getWidth(), gd.getDisplayMode().getHeight()));
70+
}
71+
} catch (Exception e) {
72+
RobotLog.error("Error when capturing screenshot. Actual error: "+e.getMessage());
73+
} finally {
74+
runningOnFailureRoutine = false;
75+
}
7076
}
7177
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
package javafxlibrary.keywords.Keywords;
1919

2020
import javafx.embed.swing.SwingFXUtils;
21+
import javafx.scene.Scene;
2122
import javafx.geometry.Bounds;
23+
import javafx.geometry.Rectangle2D;
2224
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
25+
import javafxlibrary.keywords.AdditionalKeywords.ConvenienceKeywords;
26+
import javafxlibrary.keywords.AdditionalKeywords.Find;
2327
import javafxlibrary.utils.RobotLog;
2428
import javafxlibrary.utils.TestFxAdapter;
2529
import org.apache.commons.io.IOUtils;
@@ -29,6 +33,9 @@
2933
import org.robotframework.javalib.annotation.RobotKeywords;
3034
import javafx.scene.image.Image;
3135
import javax.imageio.ImageIO;
36+
37+
import java.awt.GraphicsDevice;
38+
import java.awt.GraphicsEnvironment;
3239
import java.awt.image.BufferedImage;
3340
import java.io.*;
3441
import java.net.MalformedURLException;
@@ -58,13 +65,25 @@ else if (value.toLowerCase().equals("diskonly"))
5865
throw new JavaFXLibraryNonFatalException("Value \"" + value + "\" is not supported! Value must be either " +
5966
"\"EMBEDDED\" or \"DISKONLY\"");
6067
}
68+
69+
@RobotKeyword("Returns a screenshot from whole primary screen. Note that this shows also other applications that are open.")
70+
public Object capturePrimaryScreen() {
71+
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
72+
return this.captureImage(new Rectangle2D(0, 0, gd.getDisplayMode().getWidth(), gd.getDisplayMode().getHeight()));
73+
}
74+
75+
@RobotKeywordOverload
76+
public Object captureImage() {
77+
return captureImage(robot.targetWindow());
78+
}
6179

6280
@RobotKeywordOverload
6381
public Object captureImage(Object locator){
6482
return captureImage(locator, true);
6583
}
6684

67-
@RobotKeyword("Returns a screenshot of the given locator.\n\n"
85+
@RobotKeyword("Returns a screenshot of the given locator, or if not given from whole active window.\n\n"
86+
+ "Note that active window might only be part of the visible window, it e.g. dialog is active.\n\n"
6887
+ "``locator`` is either a _query_ or _Object:Bounds, Node, Point2D, Rectangle, PointQuery, Scene, Window_ for identifying the element, see "
6988
+ "`3. Locating or specifying UI elements`. \n\n"
7089
+ "Argument ``logImage`` is a boolean value that specifies whether a captured image is also printed to test execution log. \n\n "
@@ -73,8 +92,9 @@ public Object captureImage(Object locator){
7392
+ "| ${capture}= | Capture Image | ${region} | \n"
7493
+ "| ${capture}= | Capture Image | ${node} | \n"
7594
+ "| ${capture}= | Capture Image | ${window} | \n"
95+
+ "| ${capture}= | Capture Image | | \n"
7696
+ "| ${capture}= | Capture Image | \\#id | logImage=False |\n" )
77-
@ArgumentNames({"locator", "logImage=True"})
97+
@ArgumentNames({"locator=target window", "logImage=True"})
7898
public Object captureImage(Object locator, boolean logImage){
7999
if (locator == null)
80100
throw new JavaFXLibraryNonFatalException("Unable to capture image, given locator was null!");
@@ -118,6 +138,16 @@ public Object captureImage(Object locator, boolean logImage){
118138
throw new JavaFXLibraryNonFatalException("Unable to take capture : \"" + locator + "\"", e);
119139
}
120140
}
141+
142+
@RobotKeyword("Returns a screenshot of the scene conatining given locator.\n\n"
143+
+ "``locator`` is a query locator, see `3.1 Using queries`.\n\n "
144+
+ "\nExample:\n"
145+
+ "| ${capture}= | Capture Scene Containing Node | ${node} | \n" )
146+
@ArgumentNames({"locator", "logImage=True"})
147+
public Object captureSceneContainingNode(Object locator) {
148+
Scene scene = (Scene) useMappedObject(new ConvenienceKeywords().getScene(locator));
149+
return this.captureImage(scene);
150+
}
121151

122152
@RobotKeyword("Loads an image from the given _path_ in hard drive \n\n"
123153
+ "``path`` is the source path for image in local hard drive. \n\n"

0 commit comments

Comments
 (0)