I am trying to run some selenium cases on a headless linux system through docker in which the "user" clicks a button that copys text to the system clipboard. When I run this code, I get an error that says "No X11 DISPLAY variable was set, but this program performed an operation which requires it." I am now running the case using xvfb to account for the lack of X11 Display variable, but the code still does not work. Now, I am getting a nullpointer when trying to access the contents of the system clipboard.
Here is the code that I am running to get the copied text.
String copied = null;
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = clipboard.getContents(null);
DataFlavor[] availableFlavors = clipboard.getAvailableDataFlavors();
if ( transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor) ) {
copied = (String)transferable.getTransferData(DataFlavor.stringFlavor);
} else {
System.out.println("Could not find a suitable flavor.");
if ( transferable.getTransferDataFlavors().length == 0 ) {
System.out.println("no supported flavors");
}
for ( DataFlavor availableFlav : availableFlavors ) {
System.out.println("availableFlav = " + availableFlav);
}
for ( DataFlavor flavaflav : transferable.getTransferDataFlavors() ) {
System.out.println("transferDataFlavor = " + flavaflav);
}
}
// I am not sure what could cause the below exceptions to happen, but we should just fail the case if they occur.
} catch (IOException e) {
e.printStackTrace();
fail("IOException while fetching copied text.");
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
fail("UnsupportedFlavorException while fetching copied text.");
}
return copied;
Running this code with xvfb is resulting in the following output:
Could not find a suitable flavor.
no supported flavors
And then throws the NullPointerException.
Is there a way to fake a system clipboard that actually works in this headless linux server? What might be causing the system clipboard running in xvfb to have nothing copied?