javaswtswtbotsystem-testing

Clipboard access in SWTBot


How to access Clipboard while running SWTBot tests?

Following code throws Invalid thread access.

Clipboard clipBoard = new Clipboard(SWTUtils.display());
Object object = clipBoard.getContents(TextTransfer.getInstance());

The above code is working when run in the UI Thread. Is this the only way?

How to access the clipboard in the SwtBot thread?


Solution

  • Like with any other access of UI elements in SWTBot, you need to delegate the Clipboard access to the UI thread.

    If there isn't a ClipboardBot in SWTBot already, you can do this yourself like so:

    final Object[] object = { null }
    display.syncExec( new Runnable() {
      public void run() {
        Clipboard clipBoard = new Clipboard( display );
        object[ 0 ] = clipBoard.getContents( TextTransfer.getInstance() );
        clipboard.dispose();
      }
    } );
    

    If you would like to use some of the SWTBot infrastructure to accomplish this, there is also a post about how to extend SWTBot.