Alpine Docker 图像中带有 Firefox-ESR 的 Selenium

Selenium with Firefox-ESR in Alpine Docker image

我正在使用 Selenium 从 Java 代码进行测试,它使用 Maven 自动启动,当我 运行 mvn verify 它使用 gecko-driver 启动新的 firefox 硒实例:

final FirefoxOptions opts = new FirefoxOptions();
if (HEADLESS_MODE) {
  opts.addArguments(ARG_HEADLESS);
}
return new FirefoxDriver(opts);

在 "headless" 和正常两种模式下,它在我的机器(以及其他开发人员的机器)上都运行良好。现在我正在尝试 运行 Selenium 测试 CI 在 Alpine Linux Docker 容器中构建。当我按原样尝试 运行 时,出现错误:

mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "--headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile.pW1sbkcv98QN" Error: GDK_BACKEND does not match available displays.

然后我从 this post 安装了所有建议的工具(dbusxvfb)并在 运行 测试之前启动它:

apk add xvfb dbus
dbus-uuidgen > /etc/machine-id
export DISPLAY=:99
Xvfb $DISPLAY -ac &
mvn verify

但现在 Selenium 测试等待了大约 10 秒并因错误而失败

1565433736375 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "--headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile.1ZMsJ0Hy95V0" 1565433738424 Marionette INFO Listening on port 2828

!!! [Child][MessageChannel] Error: (msgtype=0x3E0003,name=PCompositable::Msg_Destroy) Channel error: cannot send/recv

[Child 2191] ###!!! ABORT: Aborting on channel error.: file /home/buildozer/aports/community/firefox-esr/src/firefox-52.8.1esr/ipc/glue/MessageChannel.cpp, line 2152 [Child 2191] ###!!! ABORT: Aborting on channel error.: file /home/buildozer/aports/community/firefox-esr/src/firefox-52.8.1esr/ipc/glue/MessageChannel.cpp, line 2152

!!! [Child][MessageChannel] Error: (msgtype=0x3E0003,name=PCompositable::Msg_Destroy) Channel error: cannot send/recv

看来我在这里做错了什么,在 Alpine Linux Docker 容器中 运行 headless Selenium 测试的正确方法是什么?我宁愿配置现有的 Docker 图像而不是使用 Selenium 图像,因为迁移会很昂贵。


更新:

Selenium 版本是 3.141.59
Gecko 驱动程序版本为 v0.24.0-linux64
Firefox-ESR 版本为:52.8.1-r0


更新2:

xvfbdbus 和导出的 DISPLAY 实际上并不需要 运行 无头模式下的 Firefox - 只需 运行 firefox --headless 和就这样了。

根据 Mozilla -- Firefox 52 不支持无头标志-esr.Headless 标志在版本 55(针对 Linux)和 56(Mac/Windows ) 。请尝试最新的 Firefox ESR 版本。