在没有GUI的Ubunt服务器16.04上通过Jenkins执行Selenium测试(仅限CLI) - HEADLESS MODE

时间:2017-02-08 13:17:00

标签: java selenium-webdriver

我正在通过Jenkins进行Selenium自动化测试。

我在我的电脑上完成了Jenkins设置,并以Maven项目运行测试。它没有问题。

Jenkins是在没有GUI(仅限CLI)的Ubuntu服务器上安装的。

  • 我在Jenkins中安装了一个额外的插件Xvfb

  • 在Jenkins中设置全局属性DISPLAY:0

  • 同样在Ubunt服务器上设置变量

    root@jenkins:~# echo $DISPLAY :0
    

因此,当我通过Jenkins运行selenium脚本时,我收到以下错误:

  

runBrowser(testCases.ApplicationTableTempl)经过的时间:1.068秒   <<<失败! java.awt.AWTError:无法连接到X11窗口服务器   使用':0'作为DISPLAY变量的值。在   sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)at   sun.awt.X11GraphicsEnvironment.access $ 200(X11GraphicsEnvironment.java:65)   在   sun.awt.X11GraphicsEnvironment $ 1.run(X11GraphicsEnvironment.java:115)   在java.security.AccessController.doPrivileged(Native Method)at   sun.awt.X11GraphicsEnvironment。(X11GraphicsEnvironment.java:74)   at java.lang.Class.forName0(Native Method)at   java.lang.Class.forName(Class.java:264)at   java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)at   java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)   在sun.awt.X11.XToolkit。(XToolkit.java:126)at   java.lang.Class.forName0(Native Method)at   java.lang.Class.forName(Class.java:264)at   java.awt.Toolkit $ 2.run(Toolkit.java:860)at   java.awt.Toolkit $ 2.run(Toolkit.java:855)at   java.security.AccessController.doPrivileged(Native Method)at   java.awt.Toolkit.getDefaultToolkit(Toolkit.java:854)at   utility.Utility.startRecording(Utility.java:183)at   testCases.ApplicationTableTempl.runBrowser(ApplicationTableTempl.java:31)

     

runBrowser(testCases.LoginTest)经过的时间:0.196秒<<<失败!   java.lang.NoClassDefFoundError:无法初始化类   sun.awt.X11.XToolkit at java.lang.Class.forName0(Native Method)at   java.lang.Class.forName(Class.java:264)at   java.awt.Toolkit $ 2.run(Toolkit.java:860)at   java.awt.Toolkit $ 2.run(Toolkit.java:855)at   java.security.AccessController.doPrivileged(Native Method)at   java.awt.Toolkit.getDefaultToolkit(Toolkit.java:854)at   utility.Utility.startRecording(Utility.java:183)at   testCases.LoginTest.runBrowser(LoginTest.java:34)

     

结果:

     

测试失败:ApplicationTableTempl.runBrowser:31»AWT无法连接   到X11窗口服务器u ... LoginTest.runBrowser:34»NoClassDefFound   无法初始化类sun.awt.X ...

有人建议我如何解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

看起来根本原因来自您自己的代码:

  

testCases.LoginTest.runBrowser - > utility.Utility.startRecording - > java.awt.Toolkit.getDefaultToolkit

删除java.awt.Toolkitutility.Utility.startRecording的依赖关系,它不会失败。

答案 1 :(得分:0)

我遇到了类似的问题。我可以通过以root身份登录到服务器并执行以下命令来解决它。

$ xhost +

答案 2 :(得分:0)

我只是在我的代码中注释掉,现在它不会使用功能来屏幕录像(startRecording)。请参阅herehere

我试过了:

root@jenkins:~# xhost +

得到以下回复:

xhost:  unable to open display ":0"

在詹金斯,我收到了以下错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Test case = ApplicationTableTempl
browserName = chrome
Starting ChromeDriver 2.23.409687 (c46e862757edc04c06b1bd88724d15a5807b84d1) on port 10447
Only local connections are allowed.
Feb 08, 2017 4:01:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[0.219][SEVERE]: Error initializing NSS with a persistent database (sql:/var/lib/jenkins/.pki/nssdb): libsoftokn3.so: cannot open shared object file: No such file or directory
[0.219][SEVERE]: Error initializing NSS without a persistent database: NSS error code: -5925
[0.220][SEVERE]: nss_error=-5925, os_error=0
#0 0x0000004d9d2e <unknown>
#1 0x0000004ab14b <unknown>
#2 0x0000006b42b8 <unknown>
#3 0x00000061eedb <unknown>
#4 0x000000530c67 <unknown>
#5 0x000000516e8b <unknown>
#6 0x000000508aa7 <unknown>
#7 0x000000478d2a <unknown>
#8 0x00000060bb39 <unknown>
#9 0x000000609b44 <unknown>
#10 0x0000004da266 <unknown>
#11 0x0000004acdc5 <unknown>
#12 0x0000004ad0b8 <unknown>
#13 0x0000004ad40b <unknown>
#14 0x0000004af079 <unknown>
#15 0x0000004b9b6e <unknown>
#16 0x0000004cd7e2 <unknown>
#17 0x0000004ca793 <unknown>
#18 0x7fa08ab376ba start_thread
#19 0x7fa08822882d clone

Feb 08, 2017 4:02:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
OS = linux, Browser = 
Test case = LoginTest
browserName = firefox
Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 62.272 sec <<< FAILURE! - in TestSuite
runBrowser(testCases.ApplicationTableTempl)  Time elapsed: 61.716 sec  <<< FAILURE!
org.openqa.selenium.NoSuchSessionException: no such session
(Driver info: chromedriver=2.23.409687 (c46e862757edc04c06b1bd88724d15a5807b84d1),platform=Linux 4.4.0-62-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 22 milliseconds
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'jenkins', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-62-generic', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{message=unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.23.409687 (c46e862757edc04c06b1bd88724d15a5807b84d1),platform=Linux 4.4.0-62-generic x86_64), platform=ANY}]
Session ID: d438dd092af3c51f3f72ca37527e0e91
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.maximize(RemoteWebDriver.java:882)
at factory.BrowserFactory.getBrowser(BrowserFactory.java:57)
at testCases.ApplicationTableTempl.runBrowser(ApplicationTableTempl.java:34)

runBrowser(testCases.LoginTest)  Time elapsed: 0.207 sec  <<< FAILURE!
java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
t java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:126)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.awt.Toolkit$2.run(Toolkit.java:860)
at java.awt.Toolkit$2.run(Toolkit.java:855)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:854)
at utility.Utility.startRecording(Utility.java:183)
at testCases.LoginTest.runBrowser(LoginTest.java:34)


Results :

Failed tests: 
ApplicationTableTempl.runBrowser:34 » NoSuchSession no such session
(Driver ...
LoginTest.runBrowser:34 » AWT Can't connect to X11 window server using ':0' as...