linux - 在远程服务器上的 Tomcat 中运行 Selenium 测试给出 "Error: GDK_BACKEND does not match available displays"

标签 linux selenium tomcat selenium-webdriver

我试图在远程服务器上使用 WebDriver 执行 Selenium 测试,但失败并显示消息:

1503556834416   geckodriver INFO    geckodriver 0.18.0
1503556834420   geckodriver INFO    Listening on 127.0.0.1:13167
1503556834659   geckodriver::marionette INFO    Starting browser /usr/bin/firefox with args ["-marionette"]
Error: GDK_BACKEND does not match available displays

该测试是 Spring Boot webapp 的一部分,托管在 Tomcat 8.5.20 中。测试执行是通过调用 Spring Boot webapp 的 url 链接触发的。 使用带有 geckodriver 的浏览器 Firefox,这是导致错误的代码:

System.setProperty("webdriver.gecko.driver", "/opt/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.com");

服务器有一个 Debian 8 操作系统,以 LXDE 作为桌面环境。 不管我怎么称呼这个 webapp 链接:

  • ssh 登录并在远程服务器上使用 lynx
  • 在远程服务器上执行 tightvncserver :1,使用本地计算机上的远程桌面客户端,并在远程服务器上安装的 firefox 中调用此链接
  • 只需从我的本地机器调用远程 url

...我总是得到 GDK_BACKEND 与可用显示不匹配 错误。

在我的本地环境中(测试的唯一区别是不同的 Linux 操作系统;其他版本,如 Tomcat、webapp、geckodriver 等都是相同的),此测试执行时没有任何问题。这是日志片段:

1503556181640   geckodriver     INFO    geckodriver 0.18.0
1503556181643   geckodriver     INFO    Listening on 127.0.0.1:18039
1503556181805   geckodriver::marionette INFO    Starting browser /usr/lib64/firefox/firefox with args ["-marionette"]
1503556182440   Marionette      INFO    Listening on port 44917
08:29:43.198 [Forwarding newSession on session null to remote] INFO  org.openqa.selenium.remote.ProtocolHandshake - Detected dialect: W3C
... and now the test is executed ...

我想它与远程服务器的配置有关,与 Selenium 无关,但我不知道下一步该做什么......

最佳答案

使用 Xvfb 是关键...

所以我删除了 LXDE,因为它不再需要(而且我想我需要一个用于 selenium 的桌面)并使用 xvfb-run 启动 tomcat。 在我启动 xvfb(使用端口 99)并导出显示变量之后。

示例(使用在 systemd 启动脚本中使用的完整路径):

export DISPLAY=:99

/usr/bin/xvfb-run /opt/tomcat-latest/bin/startup.sh

/usr/bin/Xvfb :99 &

关于linux - 在远程服务器上的 Tomcat 中运行 Selenium 测试给出 "Error: GDK_BACKEND does not match available displays",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855117/

相关文章:

tomcat - 部署为 .war 时的 Spring-Boot Logging 配置

selenium - 如何使用 Selenium IDE 测试工具?

reactjs - 使用 Selenium 识别 ReactJS 网页上的元素,这是更好的方法吗?

linux - 使用 Ubuntu 内核进行更改

使用 cron 作业时 exec bash shell 出现 PHP 错误?

python - 在 Python 中使用 Selenium 在 Web 表单中逐个输入多个文件

eclipse - 在服务器上部署时 Maven API 不响应 (Tomcat 7)

java - 使用 Tomcat + Intellij 从部署中删除应用程序

linux - execl找不到命令怎么办?

python - TypeError : 'NoneType' object is unsubscriptable in cv2. 归一化