Selenium错误:未指定显示

Alf*_*mpo 7 testing firefox selenium

我在debian虚拟盒中安装了selenium-server-standalone-2.42.2.jar

并安装了Firefox 29.0

并尝试使用phpunit运行以下脚本,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());
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

我已经多了几个线程,显然我必须做以下尝试:

1)在命令shell中键入它

export PATH=:0;
Run Code Online (Sandbox Code Playgroud)

结果:我得到了同样的错误.

2)我已经安装了vnc4server并将debian-vm:1作为应用程序然后设置export PATH=debian-vm:1运行它与realvnc并在查看器(这工作)我遇到了同样的问题.

Vol*_*enD 15

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

您必须先安装Xvfb和浏览器:

apt-get install xvfb
apt-get install firefox-mozilla-build
Run Code Online (Sandbox Code Playgroud)

然后启动Xvfb:

Xvfb &
Run Code Online (Sandbox Code Playgroud)

设置DISPLAY并启动Selenium:

export DISPLAY=localhost:0.0
java -jar selenium-server-standalone-2.44.0.jar
Run Code Online (Sandbox Code Playgroud)

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

  • 我建议将xvfb和DISPLAY变量的管理保留给帮助程序脚本.请参阅此答案:http://stackoverflow.com/a/14155698/376138 (3认同)
  • 这种方法似乎不再适用于 Ubuntu 16.04:我总是收到错误“无法连接到 Mir:无法连接到服务器套接字:没有这样的文件或目录无法初始化服务器:不支持百老汇显示类型:本地主机:0.0 错误:无法打开显示:localhost:0.0 ` (2认同)

tut*_*uju 9

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

export MOZ_HEADLESS=1
Run Code Online (Sandbox Code Playgroud)

然后,重新运行测试,它应该无头运行。

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

火狐浏览器

$this->setDesiredCapabilities(['moz:firefoxOptions'=> ['args' => ['-headless']]]);
Run Code Online (Sandbox Code Playgroud)

铬合金

$this->setDesiredCapabilities(['chromeOptions'=>['args'=>['headless']]]);
Run Code Online (Sandbox Code Playgroud)

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

  • `export MOZ_HEADLESS=1` 足以让我在 Ubuntu 上运行 WSL2 (4认同)

Nes*_*iza 5

当然脚本编写是可行的方法,但是遍历所有可能的 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
Run Code Online (Sandbox Code Playgroud)