29
29
import org .testfx .api .FxRobotInterface ;
30
30
import java .awt .*;
31
31
import java .awt .datatransfer .Clipboard ;
32
+ import java .awt .datatransfer .DataFlavor ;
32
33
import java .awt .datatransfer .StringSelection ;
34
+ import java .awt .datatransfer .UnsupportedFlavorException ;
33
35
import java .util .Arrays ;
34
36
import java .util .concurrent .ExecutionException ;
35
37
@@ -252,4 +254,47 @@ public int setWriteSpeed(int milliseconds) {
252
254
return oldSleepMillis ;
253
255
}
254
256
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
+ + "\n Example: \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
+ }
255
300
}
0 commit comments