0

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?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Saminathan S
  • 325
  • 2
  • 11
  • There is no way that one can access the clip board in the SWTBot Thread as I have learned that SWTBot thread is the user thread and used for simulating user actions. Hence one has to sync the SWTBot thread to the UI thread and get the access to the clip board. The solution from Rüdiger Herrmann helped – Saminathan S Mar 03 '15 at 10:26

1 Answers1

1

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.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79