You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Selenium实现Github登录报错:UriTemplateHandler类缺失

问题:Spring Boot集成Selenium实现Github登录时加载ApplicationContext失败

运行测试方法时抛出如下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@3d3c886f testClass = com.github.selenium.process.GithubProcess, locations = [], classes = [com.github.selenium.SeleniumApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true", "server.port=0"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4310d43, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@27f981c6, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@771a660, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@9da1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@1ffaf86, org.springframework.boot.test.context.SpringBootTestAnnotation@169dd363], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Caused by: java.lang.ClassNotFoundException: org.springframework.web.util.UriTemplateHandler

测试代码

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class GithubProcess {

    private static WebDriver driver;

    String baseUrl = "https://github.com/login";

    @BeforeAll
    static void beforeAll() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    @Order(1)
    public void login() throws InterruptedException {
        // github login through selenium with username and password
        driver.get(baseUrl);

        WebElement usernameOrEmail = driver.findElement(By.id("login_field"));
        WebElement password = driver.findElement(By.id("password"));
        WebElement signInButton = driver.findElement(By.cssSelector("input[type='submit']"));

        String usernameOrEmailField = "my-username-or-email";
        String passwordField = "my-password";

        usernameOrEmail.sendKeys(usernameOrEmailField);
        password.sendKeys(passwordField);
        signInButton.click();

        Thread.sleep(1000);

    }
}

pom.xml依赖配置

<properties>
    <java.version>17</java.version>
    <selenium.version>4.6.0</selenium.version>
    <webdrivermanager.version>5.3.1</webdrivermanager.version>
    <junit.jupiter.version>5.9.1</junit.jupiter.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--Selenium-->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>${selenium.version}</version>
    </dependency>

    <!--Webdriver Manager-->
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>${webdrivermanager.version}</version>
    </dependency>

    <!--JUnit5-->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>

</dependencies>

解决方案

错误原因

org.springframework.web.util.UriTemplateHandler类属于Spring Web模块,当前项目仅引入了spring-boot-starter(核心启动依赖,不含Web模块),但测试类使用了@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT),该配置会触发Spring Boot启动Web容器,导致缺少Web模块依赖而抛出类找不到的异常。

解决办法

方法1:禁用Web环境(推荐,符合当前测试场景)

当前Selenium测试是直接操作浏览器访问Github,不需要启动Spring的Web容器,因此修改@SpringBootTest的webEnvironment参数为NONE即可:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class GithubProcess {
    // 原有代码保持不变
}

方法2:添加Web模块依赖(仅当需要Web环境时使用)

如果确实需要在测试中启动Web容器,在pom.xml中添加spring-boot-starter-web依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

额外优化

selenium-java依赖已经包含了selenium-chrome-driver,可以移除pom.xml中重复的selenium-chrome-driver依赖,减少冗余。


内容的提问来源于stack exchange,提问作者Sercan Noyan Germiyanoğlu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 16:10:43