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

Firefox/Safari下Serenity Selenium测试启动失败:DriverConfigurationError问题

Serenity Selenium Firefox启动失败问题排查与解决

问题描述

我尝试在Firefox和Safari上运行Serenity Selenium自动化测试,Chrome上测试正常,但Firefox启动时出现以下错误:

net.thucydides.core.webdriver.DriverConfigurationError: WebDriver was unable to create a new instance of type class org.openqa.selenium.firefox.FirefoxDriver
WebDriver reported the following message: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
See below for more details.

at serenityswag.authentication.actions.LoginActions.as(LoginActions.java:10)
at serenityswag.inventory.WhenViewingHighlightedProducts.shouldDisplayHighlightedProductsOnTheWelcomePage(WhenViewingHighlightedProducts.java:32)

测试代码示例

@Test
public void shouldDisplayHighlightedProductsOnTheWelcomePage() {
    login.as(User.STANDARD_USER);

    List<String> productsOnDisplay = productList.titles();

    assertThat(productsOnDisplay).hasSize(6)
            .contains("Sauce Labs Backpack");
}

serenity.conf配置

serenity.test.root=serenityswag
webdriver.driver=firefox
serenity.take.screenshots=BEFORE_AND_AFTER_EACH_STEP
serenity.browser.maximized = true

LoginActions.java代码

public class LoginActions extends UIInteractionSteps {
    @Step("Log in as {0}")
    public void as(User user) {
        openUrl("https://www.saucedemo.com/");

        // Login as a standard user
        $("[data-test='username']").sendKeys(user.getUsername());
        $("[data-test='password']").sendKeys(user.getPassword());
        $("[data-test='login-button']").click();
    }
}

解决方法

  • 版本兼容性检查
    Firefox与GeckoDriver版本必须严格匹配,版本不匹配是最常见的启动失败原因。查看Mozilla官方的版本对应表,确保项目中使用的GeckoDriver版本兼容当前安装的Firefox版本。如果使用Maven/Gradle依赖管理,可在构建文件中指定正确的GeckoDriver版本,比如Maven中:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>对应版本号</version>
    </dependency>
    
  • 指定Firefox安装路径
    如果Firefox未安装在系统默认路径,需在serenity.conf中添加webdriver.firefox.bin配置,示例:

    # macOS路径示例
    webdriver.firefox.bin=/Applications/Firefox.app/Contents/MacOS/firefox-bin
    # Windows路径示例
    webdriver.firefox.bin=C:\\Program Files\\Mozilla Firefox\\firefox.exe
    
  • 配置GeckoDriver路径
    若系统PATH中未包含GeckoDriver,需在serenity.conf中指定驱动路径:

    # macOS/Linux路径示例
    webdriver.gecko.driver=/usr/local/bin/geckodriver
    # Windows路径示例
    webdriver.gecko.driver=C:\\drivers\\geckodriver.exe
    
  • 更新Serenity与Selenium版本
    旧版Serenity可能不兼容新版本的Firefox或Selenium。查看Serenity官方文档的版本兼容说明,更新构建文件中的Serenity依赖版本,确保与Selenium版本匹配。

  • 检查Firefox状态
    关闭所有Firefox进程后重新运行测试,避免因已有浏览器实例占用资源导致启动失败。同时检查Firefox是否有阻止自动化的插件或设置(如强制私有模式),必要时重置浏览器设置。

内容的提问来源于stack exchange,提问作者JavaMan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 17:15:07