在Jenkins中使用Xvfb运行无头Firefox以运行Selenium测试

5

在FreeBSD服务器上运行Play框架测试时,我遇到了“Error: no display specified”错误。因此每次都会遇到超时。

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox

Jenkins已经:

1)安装了Xvfb插件

2)安装了Play框架

使用selenide库和play框架的selenide模块编写了测试脚本。

Xvfb已在作业配置中进行配置并启用。

作业控制台输出为:

Checking out Revision 3f485bd2e3dbcfa058fc19f89ab18020e36707d8 (origin/trunk)
...
Xvfb starting$ /usr/local/bin//Xvfb :1 -screen 0  -fbdir /usr/local/jenkins/xvfb-9-786185694297443042.fbdir
...
Command detected: clean
Command detected: deps --sync
Command detected: precompile
Command detected: auto-test
[YalsTests] $ /srv/java/play/play clean
...
~ using java version "1.8.0_72"
[YalsTests] $ /srv/java/play/play auto-test
~ 14 tests to run:
~
~ selenium/front/CorrectInput...         org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: no display specified

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:113)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)

工作配置:

[X] Start Xvfb before the build, and shut it down after.
Xvfb specific display name  1
Xvfb display name offset 0

Invoke Play Framework       
Command set     Play 1.x 
Goals   
    Clean project [clean]
    Custom parameter
         Custom command deps --sync
    Precompile all Java sources and templates [precompile]
    Automatically run all application tests [auto-test]
3个回答

1
Selenium任务需要知道要连接的DISPLAY。您可以将其设置为环境变量(如果在.profile中设置,请不要忘记导出)。
export DISPLAY=:10

这是针对bash的,其他shell可能需要进行两步操作:

DISPLAY=:10
export DISPLAY

你也可以在命令行之前指定变量:
DISPLAY=:10 java -jar mySelenium.jar

DISPLAY=:10 java -jar mySelenium.jar我的问题是关于这个命令。这是用于GRID执行吗?如果我需要通过Jenkins在远程机器上执行测试WebDriver,该如何操作? - Afsal
@Afsal 或许这个能帮到你?https://dev59.com/0KHia4cB1Zd3GeqPaN0i - lilalinux
1
所以想法是在Selenium代码中设置显示变量,并为Xvfb使用相同的显示编号。对吧?我使用Chrome。我会检查Chrome中是否有类似的功能。 - Afsal

0

您可以使用Selenoid项目来避免所有这些问题,它会在Docker容器中并行启动无头浏览器。容器镜像是通过考虑webdriver和浏览器的兼容版本来创建的。它们还包括字体、flashplayer等。只需选择一个已经存在的镜像并运行测试即可。无需安装Java即可运行Selenium测试。


这并没有真正回答用户的问题。 - Michael Dally

0

如果我通过Jenkins运行我的测试,我倾向于提供这个:

xvfb-run --server-args="-screen 0, 1920x1080x16" mvn clean install...

过去让我困扰的一件事是,虽然xvfb-run会创建一个虚拟显示器,但如果它的大小不正确,它可能会严重影响您的屏幕截图和网络交互,因此通常建议提供一个适当的分辨率大小来显示您的浏览器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接