Ubuntu Jenkins服务器上WebdriverIO测试用例运行失败:浏览器崩溃
问题与解决方案
问题背景
- 用WebdriverIO + Cucumber编写的测试用例,本地运行完全正常
- Jenkins已完成与GitHub的配置,部署在Ubuntu服务器中
- 服务器已安装Chrome和Firefox浏览器,但运行测试时两个浏览器均出现类似报错,核心错误信息:
Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: Process unexpectedly closed with status 1
解决步骤
1. 匹配浏览器与驱动版本
版本不兼容是最常见的触发原因,需确保Ubuntu上的Chrome/Firefox与对应ChromeDriver/GeckoDriver版本完全匹配:
- 先查看浏览器版本:
google-chrome --version firefox --version - 下载对应版本的驱动,要么将驱动路径加入系统
PATH环境变量,要么在wdio配置文件中直接指定路径:
示例(wdio.conf.js):exports.config = { // 其他配置项... capabilities: [{ browserName: 'firefox', 'moz:firefoxOptions': { args: ['--headless'] }, 'wdio:geckodriverOptions': { binary: '/usr/local/bin/geckodriver' } }, { browserName: 'chrome', 'goog:chromeOptions': { args: ['--headless=new'] }, 'wdio:chromedriverOptions': { binary: '/usr/local/bin/chromedriver' } }] }
2. 启用无头模式运行
Ubuntu服务器通常无图形界面,必须开启浏览器无头模式才能正常启动:
- Chrome添加
--headless=new参数,Firefox添加--headless参数,同时建议搭配--no-sandbox和--disable-dev-shm-usage参数规避权限与内存问题:capabilities: [{ browserName: 'chrome', 'goog:chromeOptions': { args: ['--headless=new', '--no-sandbox', '--disable-dev-shm-usage'] } }, { browserName: 'firefox', 'moz:firefoxOptions': { args: ['--headless', '--no-sandbox'] } }]
3. 调整权限配置
Jenkins运行用户可能缺少启动浏览器或驱动的权限:
- 给驱动文件添加执行权限:
sudo chmod +x /usr/local/bin/chromedriver sudo chmod +x /usr/local/bin/geckodriver - 将浏览器安装目录权限赋予Jenkins用户:
sudo chown -R jenkins:jenkins /usr/bin/google-chrome sudo chown -R jenkins:jenkins /usr/bin/firefox
4. 补全系统依赖库
无头浏览器运行需要依赖部分系统库,缺失会导致进程直接崩溃:
- 安装Chrome依赖:
sudo apt-get install -y libnss3 libgconf-2-4 libxss1 libappindicator1 libindicator7 - 安装Firefox依赖:
sudo apt-get install -y libdbus-glib-1-2 libgtk-3-0 libxt6
5. 切换至DevTools协议(可选)
若使用独立Selenium Server仍无法解决问题,可直接使用WebdriverIO的DevTools协议,无需依赖Selenium服务:
在wdio.conf.js中修改配置:
exports.config = { // 其他配置项... automationProtocol: 'devtools' }
6. 查看详细启动日志
若以上步骤均无效,添加日志参数获取更详细的错误信息,定位具体问题:
- Chrome添加日志参数:
--enable-logging --v=1 --log-path=/var/log/chrome-test.log - Firefox添加日志参数:
-log /var/log/firefox-test.log
内容的提问来源于stack exchange,提问作者Jakay
相关产品推荐
相关产品推荐

