Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Capturing active window if locator is not given #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;


// ##############################
Expand All @@ -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;
}
}
}
34 changes: 32 additions & 2 deletions src/main/java/javafxlibrary/keywords/Keywords/ScreenCapturing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 "
Expand All @@ -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!");
Expand Down Expand Up @@ -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"
Expand Down