【问题标题】:Selenium Error: no display specifiedSelenium 错误:未指定显示
【发布时间】:2014-08-30 10:42:36
【问题描述】:

我已经在 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 运行它,并在查看器中(有效)我​​遇到了同样的问题。

【问题讨论】:

标签: firefox testing selenium


【解决方案1】:

您收到此错误,因为您尚未设置 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

然后你就可以运行你的测试了。

【讨论】:

  • 我建议将 xvfb 和 DISPLAY 变量的管理留给辅助脚本。看到这个答案:stackoverflow.com/a/14155698/376138
  • 这种方法似乎不再适用于 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
  • 不需要 Xvfb,xorg-xserver-video-dummy 也适用于 stock xorg :)
【解决方案2】:

当然,脚本是要走的路,但是遍历所有可能的 DISPLAY 值不如使用正确的 DISPLAY 值好。至少在 debian/ubuntu 中也不需要 xvfb。只要正确,Selenium 可以使用当前的 DISPLAY 会话变量在本地或远程运行。请参阅我在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

【讨论】:

  • "至少在 debian/ubuntu 中也不需要 xvfb" 这假设实际上有一个显示器,而在无头服务器上则不是这样。所以经常需要 Xvfb。
  • @Corey 我假设您没有在无头模式下运行。您当然可以这样做,但是您将无法实时观看您的测试,这在某些情况下可能没问题。我使用桌面作为服务器来测试 UI,因为毕竟 UI 是在桌面而不是服务器中运行的。
  • 使用带有物理显示器的台式机适用于小型执行环境,但不适用于大规模或具有成本效益的自动化。
  • 使用虚拟桌面可以很好地扩展,并且确实提供了具有成本效益的自动化。这里不需要实物展示。
【解决方案3】:

如今,设置 headless 就像将选项传递给 selenium 浏览器驱动程序一样简单。 在大多数环境中,这可以通过在运行测试之前设置环境变量 MOZ_HEADLESS 来完成,即尝试:

export MOZ_HEADLESS=1

然后,重新运行您的测试,它应该可以无头运行。

如果您运气不好,并且它没有获取 env var,请尝试在驱动程序配置中启用无头支持。例如:使用phpunit-selenium lib,执行以下操作:

火狐

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

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

有关更多硒选项,请参阅php-webdriver wiki。

【讨论】:

  • export MOZ_HEADLESS=1 足以让我在 Ubuntu 上运行 WSL2
【解决方案4】:

以下不是正确的变量:

$ export PATH=:0;

这定义了在哪里可以找到可执行文件,例如在 /bin、/usr/local/bin 中。 您正在使用 X11 变体,在这种情况下,:0 指的是 DISPLAY localhost:0。 所以你可能打算这样做:

$ export DISPLAY=:0

但正如其他人指出的那样,该 DISPLAY 地址实际上需要一个 Xserver(虚拟或其他)。您不能只是虚构一个值并希望它会起作用。

要查找您的用户有权连接的 DISPLAY 列表,您可以使用以下命令,然后根据(主机:displayNumber 或 :displayNumber,如果在本地主机上)设置您的 DISPLAY 变量:

$ xauth 列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多