【解决+附加相关资源链接】selenium.SessionNotCreatedException: session not created unable to connect to renderer

记一次特别让人无语的排错经验,焦头烂额+抓耳挠腮+捶胸顿足,都不足以形容出这个问题排查过程的难受。
问题的环境:在Linux通过Jenkins运行Selenium脚本。
问题报错(全):

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 29129
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
java.lang.NullPointerException
	at com.xxx.xxxx.xxx.screenshot.Screenshot.captureScreenShot(Screenshot.java:39)
	at com.xxx.xxxx.xxx.screenshot.ScreenshotListener.onTestFailure(ScreenshotListener.java:26)
	at org.testng.internal.Invoker.runTestListeners(Invoker.java:1895)
	at org.testng.internal.Invoker.runTestListeners(Invoker.java:1879)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:778)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
	at org.testng.TestRunner.privateRun(TestRunner.java:767)
	at org.testng.TestRunner.run(TestRunner.java:617)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
	at org.testng.TestNG.run(TestNG.java:1031)
	at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:283)
	at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
	at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:120)
	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
200♀ok
[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 6.72 s <<< FAILURE! - in TestSuite
[ERROR] deliver(com.xxx.xxxx.xxx.xx.ceshidenglujietu)  Time elapsed: 1.171 s  <<< FAILURE!
org.openqa.selenium.SessionNotCreatedException: 
session not created
from disconnected: unable to connect to renderer
  (Session info: headless chrome=78.0.3904.70)
  (Driver info: chromedriver=78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}),platform=Linux 3.10.0-327.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 873 milliseconds
Build info: version: '2.33.0', revision: '4ecaf82108b2a6cc6f006aae81961236eba93358', time: '2013-05-22 12:00:17'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-327.el7.x86_64', java.version: '1.8.0_221'
Driver info: org.openqa.selenium.chrome.ChromeDriver
	at com.xxx.xxx.xxxx.xx.ceshidenglujietu.deliver(ceshidenglujietu.java:70)

[ERROR] quit(com.xxx.xxx.xxx.xxx.ceshidenglujietu)  Time elapsed: 6.399 s  <<< FAILURE!
java.lang.NullPointerException
	at com.xxx.xx.xxx.xxx.ceshidenglujietu.quit(ceshidenglujietu.java:126)

现在截取关键部分:

org.openqa.selenium.SessionNotCreatedException: 
session not created
from disconnected: unable to connect to renderer
  (Session info: headless chrome=78.0.3904.70)
  (Driver info: chromedriver=78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}),platform=Linux 3.10.0-327.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)

先放一波解决方案:
网上太多说是需要driver和chrom版本一致的问题 (放个相关资源)
1、对照表
2、chrome下载地址
3、部分chrome for Linux下载地址
不过,这里还真不是,而是–>:
找到你的etc/hosts 文件(根据系统自行百度添加),增加以下映射:

127.0.0.1    localhost

下面详细说说这个让人欲哭无泪的排错过程:
1、最开始看到上面报错,NullPointerException:
空指针异常很明显,就开始看代码打断点,看了半天就定位到,捕捉截图的时候,会传送一个driver,初步设想就是这个没有new出来。
Screenshot.captureScreenShot(result, driver,true,"", "");
2、怀疑可能chromedriver版本不一致吧
最开始报错session info和driver info的版本号只是差了小版本的,比如

 (Session info: headless chrome=78.0.3904.108)
  (Driver info: chromedriver=78.0.3904105) 

虽然看起来就差了小版本,但是就怕万一,于是开始导出找资源,高版本想小版本一致,真的太难了,最后还是找到了,锁定版本“78.0.3904.70”,于是一番卸载、安装、解压,成了,但是报错依然存在。(Liunx下安装卸载软件命令详解)
3、于是,开始苦思冥想,看看pom.xml文件的依赖是不是有问题?看看jar包是否都不缺?看看jenkins的全局变量设置(昨天jenkins那台服务器崩了,最后修改了一些文件,具体哪些不清楚,是另外一个同事在解决)?
最后的最后,就是上边的解决方案了,哎,有人动了host文件,缺少了localhost映射,导致的chromedriver获取出现异常。

你可能感兴趣的:(自动化测试框架开发中遇到的问题,selenium,Chrome)