Selenium Error: no display specified

29,099

Solution 1

You receive this error, because you have not set the DISPLAY variable. Here is a guide how to perform the test on a headless machine.

You have to install Xvfb and a browser first:

apt-get install xvfb
apt-get install firefox-mozilla-build

then start Xvfb:

Xvfb &

set DISPLAY and start Selenium:

export DISPLAY=localhost:0.0
java -jar selenium-server-standalone-2.44.0.jar

and then you will be able to run your tests.

Solution 2

These days setting up headless is as easy as passing an option to the selenium browser driver. On most environments this can be done by setting the env variable MOZ_HEADLESS before running your tests, i.e try:

export MOZ_HEADLESS=1

Then, rerun your tests and it should run headless.

If you're out of luck, and it doesn't pick up the env var, try enabling the headless support in the driver config. E.g: with phpunit-selenium lib, do this:

Firefox

$this->setDesiredCapabilities(['moz:firefoxOptions'=> ['args' => ['-headless']]]);

Chrome

$this->setDesiredCapabilities(['chromeOptions'=>['args'=>['headless']]]);

See php-webdriver wiki for more selenium options.

Solution 3

Certainly scripting is the way to go, however iterating through all possible DISPLAY values is not as good as using the right DISPLAY value. Also there is no need for xvfb at least in debian/ubuntu. Selenium can be run locally or remotely using a current DISPLAY session variable as long as it is correct. See my post in http://thinkinginsoftware.blogspot.com/2015/02/setting-display-variable-to-avoid-no.html but in short:

# Check current DISPLAY value
$ echo $DISPLAY
:0
# If xclock fails as below the variable is incorrect
$ xclock
No protocol specified
No protocol specified
Error: Can't open display: :0
# Find the correct value for the current user session
$ xauth list|grep `uname -n`
uselenium/unix:10  MIT-MAGIC-COOKIE-1  48531d0fefcd0a9bde13c4b2f5790a72
# Export with correct value
$ export DISPLAY=:10
# Now xclock runs
$ xclock

Solution 4

The following is not the right variable:

$ export PATH=:0;

That defines where to find executables, such as in /bin, /usr/local/bin. You're working with X11 variants and in that context, :0 refers to DISPLAY localhost:0. So you probably intended the following:

$ export DISPLAY=:0

But as others have pointed out there needs to actually be an Xserver (virtual or otherwise) at that DISPLAY address. You can't just make up a value and hope it will work.

To find a list of DISPLAYs that your user is authorized to connect to you can use the following, then set your DISPLAY variable according (host:displayNumber, or :displayNumber if on the local host):

$ xauth list

Share:
29,099
Alfonso Fernandez-Ocampo
Author by

Alfonso Fernandez-Ocampo

Updated on March 24, 2021

Comments

  • Alfonso Fernandez-Ocampo
    Alfonso Fernandez-Ocampo about 3 years

    I've installed selenium-server-standalone-2.42.2.jar in a debian virtual box

    and installed Firefox 29.0

    and trying to run the following script with phpunit which is the only file in the directory:

    <?php
    class TestLogin extends PHPUnit_Extensions_Selenium2TestCase{
    
        public function setUp()
        {
                $this->setHost('localhost');
                $this->setPort(4444);
                $this->setBrowser('firefox');
                $this->setBrowserUrl('http://debian-vm/phpUnitTutorial');
        }
    
        public function testHasLoginForm()
        {
                $this->url('index.php');
    
                $username = $this->byName('username');
                $password = $this->byName('password');
    
                $this->assertEquals('', $username->value());
                $this->assertEquals('', $password->value());
        }
    }
    

    I get the following error:

    1) TestLogin::testHasLoginForm
    PHPUnit_Extensions_Selenium2TestCase_WebDriverException: Unable to connect to host
    127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
    Error: no display specified
    Error: no display specified
    

    What does this mean?

    I've red several threads and apparently I had to do the following which I tried:

    1)to type this in the command shell

    export PATH=:0;
    

    Result: I got the same error.

    2) I've installed vnc4server and getting debian-vm:1 as a application I then set export PATH=debian-vm:1 run it with realvnc and in the viewer (which works) I got the same problem.

  • pixelistik
    pixelistik over 9 years
    I recommend to leave the management of xvfb and the DISPLAY variable to a helper script. See this answer: stackoverflow.com/a/14155698/376138
  • Corey Goldberg
    Corey Goldberg over 8 years
    "Also there is no need for xvfb at least in debian/ubuntu" That assumes there is actually a display, which is not the case on a headless server. So Xvfb is very often needed.
  • Nestor Urquiza
    Nestor Urquiza over 8 years
    @Corey I am assuming you are not running on headless mode. You can certainly do but then you will not be able to watch realtime your tests which might be ok in some cases. I use desktops working as servers to test the UI because after all UI runs in desktops and not servers.
  • Corey Goldberg
    Corey Goldberg over 8 years
    using desktops with physical displays is fine for small execution environments, but isn't viable for large scale or cost effective automation.
  • Nestor Urquiza
    Nestor Urquiza over 8 years
    Using virtual desktops scales perfectly well and provides cost effective automation indeed. There is no need for physical display here.
  • xuhdev
    xuhdev over 7 years
    This approach seems no longer working with Ubuntu 16.04: I always got the error Failed to connect to Mir: Failed to connect to server socket: No such file or directory Unable to init server: Broadway display type not supported: localhost:0.0 Error: cannot open display: localhost:0.0
  • hanshenrik
    hanshenrik over 7 years
    no need for Xvfb, the xorg-xserver-video-dummy works fine too with stock xorg :)
  • Mark
    Mark about 3 years
    export MOZ_HEADLESS=1 was sufficient for me running WSL2 with Ubuntu