Selenium 4不再加载Firefox驱动程序



将基于Selenium的Java应用程序更新到版本4后,我发现打包为UberJar的代码失败了,错误如下:

Driver info: driver.version: FirefoxDriver
at java.base/java.util.Optional.orElseThrow(Optional.java:403)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:230)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
at com.octopus.decorators.FirefoxDecorator.init(FirefoxDecorator.java:72)
有趣的是,所有的测试都通过了。我该如何解决这个问题?

问题是因为我的应用程序被打包为带有Shade插件的UberJAR。Selenium 4必须使用不同的服务来基于服务查找定位依赖项,这需要META-INF/service文件。

我构建UberJAR的配置没有正确捕获这些服务文件。解决方案是将ServicesResourceTransformer添加到我的Shade插件配置中:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${shade.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</plugin>

最新更新