selenium-webdriverselenium-chromedrivergithub-actionsworkflow

HeadlessException in Github Actions


I have a Selenium testsuite, which I try to execute with use of Github Actions. I initialize chromedriver with these arguments:

case "chrome-headless":
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("start-maximized");
WebDriverManager.chromedriver().setup();
return new ChromeDriver(chromeOptions);

mvn install -DbrowserType=chrome-headless is used to execute tests. It works locally also in headless mode, but fails in case of workflow execution. Error message:

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


Solution

  • Two additional settings may solve your issue

    options.addArguments("--disable-dev-shm-usage");

    Install X virtual frame builder and setup virtual display. This will simulate GUI for tests that still require some form of it even in headless mode:

    - name: Install Xvfb
      run: sudo apt-get install -y xvfb dbus-x11
    
    - name: Run Xvfb
      run: |
        Xvfb :99 -ac -screen 0 1920x1080x24 &
        sudo service dbus start
    
    - name: Set virtual display
      run: echo "DISPLAY=:99" >> $GITHUB_ENV
    
    - name: Run Maven Tests
      run: |
        export NO_AT_BRIDGE=1
        mvn install -DbrowserType=chrome-headless