Selenium错误:未指定显示器

10

我在一个Debian虚拟机中安装了selenium-server-standalone-2.42.2.jar,并安装了Firefox 29.0。

现在,我正在尝试使用phpunit运行该目录中唯一的文件:以下脚本:

<?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());
    }
}

我遇到了以下错误:

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

这是什么意思?

我看了几个帖子,显然我需要做以下操作,我试过了:

1)在命令行中输入以下内容

export PATH=:0;

结果:我得到了相同的错误。

2)我安装了 vnc4server,并将 debian-vm:1 设置为应用程序,然后设置 export PATH=debian-vm:1,使用 realvnc 运行它,在查看器中(可以正常工作),我遇到了相同的问题。


你的服务器在运行吗? 这里有同样的错误:http://stackoverflow.com/questions/10011343/selenium-2-unable-to-connect-to-host - Decypher
Firefox 29.0 这个版本可以在以下链接中下载:https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/29.0/linux-x86_64/en-US/。 - Alfonso Fernandez-Ocampo
你解决了这个问题吗?似乎是我遇到的同样问题。 - Eljah
不,我没有。抱歉,我最近又尝试解决了一次,但仍然存在同样的问题。 - Alfonso Fernandez-Ocampo
4个回答

19

您收到此错误是因为您尚未设置DISPLAY变量。这里是如何在无头机器上执行测试的指南。

您需要首先安装Xvfb和一个浏览器:

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

然后启动Xvfb:

Xvfb &

设置DISPLAY并启动Selenium:

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

然后您就能够运行您的测试。


3
我建议将xvfb和DISPLAY变量的管理交给一个帮助脚本。请参考此答案:https://dev59.com/PXM_5IYBdhLWcg3wWRoF#14155698 - pixelistik
2
这种方法似乎在Ubuntu 16.04上不再适用:我总是会收到错误消息 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 - xuhdev
没有必要使用 Xvfb,xorg-xserver-video-dummy 在标准的 xorg 中也完全可以胜任 :) - hanshenrik
@xuhdev 遇到这样的问题,我用以下方法解决了:Xvfb :99 & 然后 export DISPLAY=:99 - Jan

9
近年来,通过向Selenium浏览器驱动程序传递选项,设置无界面已经变得非常简单。在大多数环境下,可以在运行测试之前设置环境变量MOZ_HEADLESS来完成此操作,例如:try:
export MOZ_HEADLESS=1

接下来,重新运行你的测试,它应该会以无头模式运行。

如果你没有运气,环境变量没有被识别,尝试在驱动程序配置中启用无头支持。例如:使用 phpunit-selenium 库,请执行以下操作:

Firefox

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

Chrome

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

请查看php-webdriver维基获取更多Selenium选项。


4
在我运行带有Ubuntu的WSL2时,"export MOZ_HEADLESS=1" 对我来说已经足够。 - Mark

5
当然,脚本编写是正确的方法,但是遍历所有可能的DISPLAY值并不如使用正确的DISPLAY值好。而且在debian/ubuntu中至少不需要xvfb。只要正确设置当前的DISPLAY会话变量,Selenium就可以在本地或远程运行。请参阅我在http://thinkinginsoftware.blogspot.com/2015/02/setting-display-variable-to-avoid-no.html中的帖子,简而言之:
# 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

1
在Debian/Ubuntu中至少不需要xvfb,但这假定实际上有一个显示器,而在无头服务器上并非如此。因此,很常需要使用Xvfb。 - Corey Goldberg
@Corey 我假设你没有在无头模式下运行。当然,你可以这样做,但是这样你将无法实时观看测试,这在某些情况下可能是可以接受的。我使用桌面作为服务器来测试 UI,因为毕竟 UI 运行在桌面上而不是服务器上。 - Nestor Urquiza
使用带有物理显示器的台式机适用于小型执行环境,但对于大规模或成本效益自动化来说并不可行。 - Corey Goldberg
1
使用虚拟桌面可以完美地扩展,并提供成本效益的自动化。这里不需要物理显示器。 - Nestor Urquiza

3
以下是错误的变量:
```shell $ export PATH=:0; ```
这会定义可执行文件的路径,例如/bin、/usr/local/bin。在处理X11变体时,:0指的是显示localhost:0。因此,您可能打算使用以下内容:
```shell $ export DISPLAY=:0 ```
但是,正如其他人所指出的那样,该DISPLAY地址实际上需要存在一个Xserver(虚拟或其他)。您不能只随意编写值并希望它能够工作。
要查找用户有权连接的DISPLAY列表,请使用以下命令,然后根据主机和显示器编号设置DISPLAY变量(如果在本地主机上,则为:displayNumber):
```shell $ xauth list ```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接