在Scala Play框架中使用Selenium时测试编译冲突问题求助
解决Play框架中Selenium版本冲突导致测试编译失败的问题
问题背景
基于Play框架构建测试API,主程序和测试环节都需要调用Selenium Grid。当前build.sbt引入了Selenium 4.8.1相关依赖,主程序编译运行正常,但测试编译失败,报错:
Class org.openqa.selenium.internal.FindsById not found - continuing with a stub.
尝试给Selenium依赖添加% Compile标记无效,移除Selenium依赖后测试可正常运行,推测是Play测试依赖自带的Selenium版本与项目指定版本冲突。
解决方案
核心是排除Play测试依赖中的Selenium传递依赖,强制使用项目指定的4.8.1版本,同时优化Selenium依赖的引入方式(避免重复导入):
修改后的build.sbt配置
import scala.language.postfixOps ThisBuild / scalaVersion := "2.13.12" ThisBuild / version := "1" lazy val root = (project in file(".")) .enablePlugins(PlayScala) .settings( name := """MarauderV2""", libraryDependencies ++= Seq( guice, // 仅需引入selenium-java,它已包含remote-driver、chrome-driver等核心依赖 "org.seleniumhq.selenium" % "selenium-java" % "4.8.1", // 排除scalatestplus-play中的所有Selenium传递依赖 "org.scalatestplus.play" %% "scalatestplus-play" % "5.1.0" % Test excludeAll( ExclusionRule(organization = "org.seleniumhq.selenium") ), // 排除specs2中的所有Selenium传递依赖 specs2 % Test excludeAll( ExclusionRule(organization = "org.seleniumhq.selenium") ) ) )
补充说明
- Selenium 4.x已移除
org.openqa.selenium.internal.FindsById这类旧定位接口,改用driver.findElement(By.id("xxx"))即可实现相同功能,无需依赖旧接口。 - 排除测试依赖中的Selenium后,测试代码会使用项目指定的4.8.1版本,彻底解决版本冲突问题。
内容的提问来源于stack exchange,提问作者hkN
相关产品推荐
相关产品推荐

