1

I have a Selenium Java test copying table grid values to clipboard. Then I'm extracting the clipboard content with the following method:

public static String extractTextFromClipboard(){
    try {
        return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
    }catch (Exception e){
        ConsoleLogger.error("Failed to extract text from clipboard " + e.getMessage());
        return "";
    }
}

and if works just fine when I run the tests locally.
But when I run this test on Jenkins it fails with the following error:

No X11 DISPLAY variable was set, but this program performed an operation which requires it.   

I saw several questions here dealing with the above error but I still do not understand what can I do to fix my issue?
Are there alternative ways to extract clipboard content so it will work for me?

sound wave
  • 3,191
  • 3
  • 11
  • 29
Unused hero
  • 307
  • 1
  • 9
  • What is the Jenkins host you are running your jobs on? Does [this answer](https://stackoverflow.com/a/784415/3730626) seem helpful? – M B Feb 23 '23 at 07:16
  • I don't actually know, I'm writing Selenium tests and infrastructure, need to ask devops guy about that – Unused hero Feb 23 '23 at 07:18
  • sounds like you need a display to have a clipboard. The error you are receiving is telling you there is no display or "virtual" display. – pcalkins Feb 23 '23 at 20:13
  • I'm not really understand how it works when Selenium tests are running on Jenkins, but I know we run our tests in regular, not headless mode. So I do not understand why this difference comes when running not locally. I will try to ask our devops engineers – Unused hero Feb 23 '23 at 21:13

1 Answers1

1

Possible solution to your problem:

  1. One potential solution is to use a virtual display such as Xvfb (X virtual framebuffer) This will allow the clipboard access code to run without errors. You can install Xvfb on your Jenkins server and then start a virtual display before running your tests.

sudo apt-get install xvfb

Xvfb :99 -screen 0 1024x768x16 &
export DISPLAY=:99
mvn test
  1. OR YOU could try using a different library for accessing the clipboard content that does not require an X11 display. For example, you can use the JavaFX library.
 <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>16</version>
    </dependency>

import javafx.scene.input.Clipboard; import javafx.scene.input.ClipboardContent;

public static String extractTextFromClipboard(){
    Clipboard clipboard = Clipboard.getSystemClipboard();
    if (clipboard.hasString()) {
        return clipboard.getString();
    } else {
        ConsoleLogger.error("Failed to extract text from clipboard");
        return "";
    }
}
Abhishek Dhoundiyal
  • 1,359
  • 1
  • 12
  • 19
  • I've tried your solutions 2 and 3. None worked for me. I added the dependencies in pom, but in the class I see "can not resolve symbol". `import javafx.scene.input.Clipboard;` - `javafx` is not defined, `import org.apache.commons.cli.Clipboard;` - `cli` is not defined. – Unused hero Feb 26 '23 at 17:48