Chrome 在 Ubuntu 14.04 终端上使用 maven 命令时 运行 TestNG 测试套件文件无法打开浏览器

Chrome browser does not open when running TestNG test suite file using maven command on Ubuntu 14.04 Terminal

机器:Ubuntu Linux 14.04 LTS

Maven: 3.0.5

当我从 Eclipse 执行我的 TestNG 测试套件文件时,它会打开 Chrome 浏览器并正确执行测试。但是当我在终端上执行以下命令时,

$ sudo mvn clean install -DsuiteXmlFile=testng.xml

Since there is already some access issue with my machine, I have to use sudo with the command.

Chrome 浏览器打不开,maven log 不停地打印下面的日志,

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Starting webdriver on OS: Linux
Starting ChromeDriver 2.37.543619 (a237acd3116cac3b3f0da42a000502ce3fafcb23) on port 14536
Only local connections are allowed.
Starting webdriver on OS: Linux
Starting ChromeDriver 2.37.543619 (a237acd3116cac3b3f0da42a000502ce3fafcb23) on port 6694
Only local connections are allowed.
Starting webdriver on OS: Linux
Starting ChromeDriver 2.37.543619 (a237acd3116cac3b3f0da42a000502ce3fafcb23) on port 4991
Only local connections are allowed.
Starting webdriver on OS: Linux
Starting ChromeDriver 2.37.543619 (a237acd3116cac3b3f0da42a000502ce3fafcb23) on port 19892
Only local connections are allowed.
Starting webdriver on OS: Linux
Starting ChromeDriver 2.37.543619 (a237acd3116cac3b3f0da42a000502ce3fafcb23) on port 21613
Only local connections are allowed.

However, this is working perfectly fine on Windows 10.

有没有人对此有解决方案,因为我找不到任何解决方案。

如下所示设置您的系统显示值并再次尝试运行测试。

You can write at the end of your .bashrc file:

export DISPLAY=:0.0

我之前在 运行 上通过 Maven 命令在 Jenkins 上遇到过这个问题,我在 Ubuntu 上安装了 xvfb 然后完美地工作了。

在 Ubuntu 上安装 xvfb:

sudo apt-get update
sudo apt-get install xvfb

那么你需要添加这些配置才能很好地启动它:

Xvfb :1 -screen 0 800x600x16
DISPLAY=:1.0

并且您可以将其安装为 Jenkins 插件: 关注 this

通过对我的测试进行 docker 化,终于解决了我的问题。

我使用 ENV 从 Docker 文件中设置 DISPLAY,例如

ENV DISPLAY :99

然后我使用 ENTRYPOINT 从 Docker 容器执行我的 run.sh 文件以启动 Xvfb 显示:

Xvfb :99 -ac &

并在同一 run.sh 文件中的上述行之后执行我的测试:

mvn clean install -DsuiteXmlFile=testng.xml

运行 的输出是:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 13506
Only local connections are allowed.
May 31, 2018 5:44:12 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
31/05/2018 05:44:16 copy_tiles: allocating first_line at size 41

[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 17.688 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

编辑:

可以找到 Docker 图片 here