diff --git a/src/main/java/javafxlibrary/keywords/AdditionalKeywords/RunOnFailure.java b/src/main/java/javafxlibrary/keywords/AdditionalKeywords/RunOnFailure.java index bd94c52..6797357 100644 --- a/src/main/java/javafxlibrary/keywords/AdditionalKeywords/RunOnFailure.java +++ b/src/main/java/javafxlibrary/keywords/AdditionalKeywords/RunOnFailure.java @@ -17,10 +17,14 @@ package javafxlibrary.keywords.AdditionalKeywords; -import javafx.stage.Screen; +import javafx.geometry.Rectangle2D; import javafxlibrary.keywords.Keywords.ScreenCapturing; import javafxlibrary.utils.RobotLog; import javafxlibrary.utils.TestFxAdapter; + +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; + import org.robotframework.javalib.annotation.RobotKeywords; @RobotKeywords @@ -30,7 +34,7 @@ public class RunOnFailure extends TestFxAdapter{ private String runOnFailureKeyword = "Take Screenshot"; // Only run keyword on failure if false - private boolean runningOnFailureRoutine = false; + private static boolean runningOnFailureRoutine = false; // ############################## @@ -55,17 +59,19 @@ public void runOnFailure() { runningOnFailureRoutine = true; - RobotLog.info("JavaFXLibrary keyword has failed!"); - if (robot == null) { - RobotLog.error("FxRobot not initialized, launch test application with the library"); - } - - if (robot.targetWindow() != null) { - new ScreenCapturing().captureImage(robot.targetWindow()); - } else { - new ScreenCapturing().captureImage(Screen.getPrimary().getBounds()); - } - - runningOnFailureRoutine = false; + try { + RobotLog.info("JavaFXLibrary keyword has failed!"); + if (robot == null) { + RobotLog.error("FxRobot not initialized, launch test application with the library"); + } else { + GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + RobotLog.debug("Capturing screenshot from primary screen with resolution "+gd.getDisplayMode().getWidth()+"x"+gd.getDisplayMode().getHeight()+"."); + new ScreenCapturing().captureImage(new Rectangle2D(0, 0, gd.getDisplayMode().getWidth(), gd.getDisplayMode().getHeight())); + } + } catch (Exception e) { + RobotLog.error("Error when capturing screenshot. Actual error: "+e.getMessage()); + } finally { + runningOnFailureRoutine = false; + } } } \ No newline at end of file diff --git a/src/main/java/javafxlibrary/keywords/Keywords/ScreenCapturing.java b/src/main/java/javafxlibrary/keywords/Keywords/ScreenCapturing.java index ce546dd..a240b75 100644 --- a/src/main/java/javafxlibrary/keywords/Keywords/ScreenCapturing.java +++ b/src/main/java/javafxlibrary/keywords/Keywords/ScreenCapturing.java @@ -18,8 +18,12 @@ package javafxlibrary.keywords.Keywords; import javafx.embed.swing.SwingFXUtils; +import javafx.scene.Scene; import javafx.geometry.Bounds; +import javafx.geometry.Rectangle2D; import javafxlibrary.exceptions.JavaFXLibraryNonFatalException; +import javafxlibrary.keywords.AdditionalKeywords.ConvenienceKeywords; +import javafxlibrary.keywords.AdditionalKeywords.Find; import javafxlibrary.utils.RobotLog; import javafxlibrary.utils.TestFxAdapter; import org.apache.commons.io.IOUtils; @@ -29,6 +33,9 @@ import org.robotframework.javalib.annotation.RobotKeywords; import javafx.scene.image.Image; import javax.imageio.ImageIO; + +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; import java.awt.image.BufferedImage; import java.io.*; import java.net.MalformedURLException; @@ -58,13 +65,25 @@ else if (value.toLowerCase().equals("diskonly")) throw new JavaFXLibraryNonFatalException("Value \"" + value + "\" is not supported! Value must be either " + "\"EMBEDDED\" or \"DISKONLY\""); } + + @RobotKeyword("Returns a screenshot from whole primary screen. Note that this shows also other applications that are open.") + public Object capturePrimaryScreen() { + GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + return this.captureImage(new Rectangle2D(0, 0, gd.getDisplayMode().getWidth(), gd.getDisplayMode().getHeight())); + } + + @RobotKeywordOverload + public Object captureImage() { + return captureImage(robot.targetWindow()); + } @RobotKeywordOverload public Object captureImage(Object locator){ return captureImage(locator, true); } - @RobotKeyword("Returns a screenshot of the given locator.\n\n" + @RobotKeyword("Returns a screenshot of the given locator, or if not given from whole active window.\n\n" + + "Note that active window might only be part of the visible window, it e.g. dialog is active.\n\n" + "``locator`` is either a _query_ or _Object:Bounds, Node, Point2D, Rectangle, PointQuery, Scene, Window_ for identifying the element, see " + "`3. Locating or specifying UI elements`. \n\n" + "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){ + "| ${capture}= | Capture Image | ${region} | \n" + "| ${capture}= | Capture Image | ${node} | \n" + "| ${capture}= | Capture Image | ${window} | \n" + + "| ${capture}= | Capture Image | | \n" + "| ${capture}= | Capture Image | \\#id | logImage=False |\n" ) - @ArgumentNames({"locator", "logImage=True"}) + @ArgumentNames({"locator=target window", "logImage=True"}) public Object captureImage(Object locator, boolean logImage){ if (locator == null) throw new JavaFXLibraryNonFatalException("Unable to capture image, given locator was null!"); @@ -118,6 +138,16 @@ public Object captureImage(Object locator, boolean logImage){ throw new JavaFXLibraryNonFatalException("Unable to take capture : \"" + locator + "\"", e); } } + + @RobotKeyword("Returns a screenshot of the scene conatining given locator.\n\n" + + "``locator`` is a query locator, see `3.1 Using queries`.\n\n " + + "\nExample:\n" + + "| ${capture}= | Capture Scene Containing Node | ${node} | \n" ) + @ArgumentNames({"locator", "logImage=True"}) + public Object captureSceneContainingNode(Object locator) { + Scene scene = (Scene) useMappedObject(new ConvenienceKeywords().getScene(locator)); + return this.captureImage(scene); + } @RobotKeyword("Loads an image from the given _path_ in hard drive \n\n" + "``path`` is the source path for image in local hard drive. \n\n"