Winium集成Selenium测试Windows桌面应用时W3C Capabilities报错求助
问题描述
在现有Selenium项目中集成Winium实现Windows桌面应用测试,未修改原有代码仅添加Winium依赖,触发测试时抛出会话创建失败错误。
测试代码
WiniumDriver driver = null; String appPath = "C:/windows/system32/calc.exe"; String address = "http://localhost:9999"; DesktopOptions option = new DesktopOptions(); option.setApplicationPath(appPath); option.setDebugConnectToRunningApp(false); option.setLaunchDelay(2); driver = new WiniumDriver(new URL(address), option);
依赖配置
<dependency> <groupId>com.github.2gis.winium</groupId> <artifactId>winium-elements-desktop</artifactId> <version>0.2.0-1</version> </dependency> <dependency> <groupId>com.github.2gis.winium</groupId> <artifactId>winium-webdriver</artifactId> <version>0.1.0-1</version> </dependency>
核心错误信息
WARNING: Support for Legacy Capabilities is deprecated; You are sending the following invalid capabilities: [app, debugConnectToRunningApp, launchDelay]; Please update to W3C Syntax
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session.
Caused by: java.lang.IllegalArgumentException: Illegal key values seen in w3c capabilities: [app, debugConnectToRunningApp, launchDelay]
问题原因
你使用的Winium官方版本(0.1.0-1/0.2.0-1)已停止维护,仅支持Selenium 3.x及更早版本的旧版Capabilities格式;而当前项目依赖的Selenium 4.9.0强制遵循W3C规范,会对非标准的Capabilities参数进行校验,导致app、debugConnectToRunningApp、launchDelay被判定为非法值,进而无法创建会话。
解决方案
方案1:降级Selenium版本到3.x
将项目中的Selenium依赖版本降至3.x系列(推荐3.141.59),该版本仍兼容旧版Capabilities格式,无需修改现有Winium代码即可正常运行。
方案2:替换为微软官方WinAppDriver
WinAppDriver是微软维护的Windows桌面应用自动化工具,完全支持W3C规范,与Selenium 4.x原生兼容:
- 下载安装WinAppDriver
- 替换依赖为WinAppDriver的Java客户端:
<dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.5.1</version> </dependency> - 调整代码适配WinAppDriver:
WindowsDriver driver = null; String appPath = "C:/Windows/System32/calc.exe"; DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("app", appPath); driver = new WindowsDriver(new URL("http://localhost:4723"), capabilities);
方案3:使用社区维护的Winium分支
若坚持使用Winium,可尝试社区维护的更新分支(如winium-desktop),部分分支已适配W3C规范,但需注意版本匹配问题。
内容的提问来源于stack exchange,提问作者SeleniumUser

