Skip to content

Commit 5ba85c8

Browse files
Turmiosoisetu
authored andcommitted
Support getting and setting clipboard content
One could have application where there are for example hot keys or buttons that copies data to clipboard or from clipboard to application. These new keywords is made to support these cases.
1 parent e158741 commit 5ba85c8

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import org.testfx.api.FxRobotInterface;
3030
import java.awt.*;
3131
import java.awt.datatransfer.Clipboard;
32+
import java.awt.datatransfer.DataFlavor;
3233
import java.awt.datatransfer.StringSelection;
34+
import java.awt.datatransfer.UnsupportedFlavorException;
3335
import java.util.Arrays;
3436
import java.util.concurrent.ExecutionException;
3537

@@ -252,4 +254,47 @@ public int setWriteSpeed(int milliseconds) {
252254
return oldSleepMillis;
253255
}
254256

257+
258+
@RobotKeyword("Reads clipboard content as text.")
259+
public String getClipboardContent() {
260+
if (TestFxAdapter.isHeadless) {
261+
RobotLog.warn("Headless mode does not support clipboard.");
262+
return "";
263+
} else {
264+
try {
265+
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
266+
String contents = (String) c.getData(DataFlavor.stringFlavor);
267+
return contents;
268+
} catch (UnsupportedFlavorException e) {
269+
throw new JavaFXLibraryNonFatalException("Unable to get clipboard contents. Getting current content as string is not supported", e);
270+
} catch (Exception e) {
271+
if(e instanceof JavaFXLibraryNonFatalException) {
272+
throw (JavaFXLibraryNonFatalException) e;
273+
}
274+
throw new JavaFXLibraryNonFatalException("Unable to get clipboard contents.", e);
275+
}
276+
}
277+
}
278+
279+
@RobotKeyword("Writes a given text characters to clipboard.\n\n"
280+
+ "``text`` is the text characters to write\n"
281+
+ "\nExample: \n"
282+
+ "| Set Clipboard Content | Clipboard value as string | \n")
283+
@ArgumentNames({"text" })
284+
public void setClipboardContent(String text) {
285+
if (TestFxAdapter.isHeadless) {
286+
RobotLog.warn("Headless mode does not support clipboard.");
287+
} else {
288+
try {
289+
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
290+
StringSelection testData = new StringSelection(text);
291+
c.setContents(testData, testData);
292+
} catch (Exception e) {
293+
if(e instanceof JavaFXLibraryNonFatalException) {
294+
throw e;
295+
}
296+
throw new JavaFXLibraryNonFatalException("Unable to set clipboard contents.", e);
297+
}
298+
}
299+
}
255300
}

src/test/robotframework/acceptance/MiscTests.robot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ Library Keyword Timeout Should Happen
399399
... Write Robot Framework
400400
[Teardown] Run Keywords Set Timeout ${old_timeout} AND Set Write Speed ${old_write_speed}
401401

402+
Clipboard Contents
403+
[Tags] smoke
404+
Set Clipboard Content text=JavaFXLibrary is a great tool!
405+
${READ_CONTENT}= Get Clipboard Content
406+
Should Be Equal ${READ_CONTENT} JavaFXLibrary is a great tool!
407+
402408
*** Keywords ***
403409
Setup All Tests
404410
Import JavaFXLibrary

0 commit comments

Comments
 (0)