运行WebdriverIO+Sauce Connect时遭遇“Unexpected reserved word”语法错误
解决WebdriverIO Browser Runner + Sauce Connect 出现的"Unexpected reserved word"错误
问题概述
搭建基于WebdriverIO v8和Sauce Connect的最简测试用例时,运行测试触发语法错误,即使测试文件为空也会出现该问题。尝试将项目转为ES模块格式后,问题仍未解决。
错误信息
{ "message": "Uncaught SyntaxError: Unexpected reserved word", "filename": "http://192.168.1.51:61964/@wdio/browser-runner/setup", "error": "SyntaxError: Unexpected reserved word" }
相关配置文件
package.json
{ "name": "test", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "@wdio/browser-runner": "^8.11.2", "@wdio/cli": "^8.11.2", "@wdio/sauce-service": "^8.11.2", "webdriverio": "^8.11.2" } }
wdio.conf.js
module.exports.config = { // ... runner: 'browser', specs: ['./test/index.js'], user: process.env.SAUCE_USERNAME, key: process.env.SAUCE_ACCESS_KEY, region: 'eu', services: [ ['sauce', { sauceConnect: true, sauceConnectOpts: { // ... } }] ], capabilities: [{ browserName: 'chrome', platformName: 'Windows 10', browserVersion: '75' }], logLevel: 'trace', };
解决方案
核心原因
WebdriverIO的browser-runner基于DevTools协议开发,仅兼容现代浏览器(通常Chrome 90及以上版本)。你指定的Chrome 75不支持browser-runner注入的初始化脚本中使用的ES6+语法(如模块导入/导出等保留字),因此触发语法错误。
具体修复步骤
更换Runner类型
将wdio.conf.js中的runner: 'browser'改为runner: 'local',或者直接移除runner配置(WebdriverIO v8默认会根据服务适配):module.exports.config = { // ... runner: 'local', // 替换原有的browser runner // ... }清理不必要的依赖
如果不需要browser-runner的特性,可以移除该依赖:npm uninstall @wdio/browser-runner验证兼容性
确保指定的浏览器版本在Sauce Labs的支持范围内,旧版本浏览器建议使用传统WebDriver协议而非DevTools协议的runner。
内容的提问来源于stack exchange,提问作者Георги Костов
相关产品推荐
相关产品推荐

