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

Gradle+Selenium测试在Github Actions失败但本地正常求助

问题:Github Actions中Selenium+Gradle执行SSO登录测试失败

本地执行./gradlew test可正常运行测试,但Github Actions中执行时,Selenium停留在"Access Manager"页面,无法跳转到目标站点,抛出超时错误:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for title to be "Continuous Improvement - Event Name Overview". Current title: "Access Manager" (tried for 45 second(s) with 500 milliseconds interval)

解决方案

1. 优化Chrome无头模式参数

旧的无头模式参数可能无法适配SSO跳转场景,补充参数让浏览器行为更接近本地环境:

@Before
public void setup() {
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless=new", "--disable-gpu", "--window-size=1920,1200",
                        "--ignore-certificate-errors", "--disable-extensions", 
                        "--no-sandbox", "--disable-dev-shm-usage",
                        "--disable-web-security", "--allow-running-insecure-content",
                        "--disable-features=VizDisplayCompositor");
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

说明:--headless=new是Chrome 112+的新无头模式,比旧版更贴近正常浏览器行为,WebDriverManager会自动匹配对应版本的ChromeDriver。

2. 强化登录步骤的等待逻辑

直接输入账号密码可能因元素未就绪导致操作无效,改为等待元素可交互后再执行操作,并增加多条件等待:

@Test
public void homePageTitle() throws InterruptedException{
    WebDriverWait wait = new WebDriverWait (driver, 45);
    driver.get("https://notarealurl.com");
    
    // 等待用户名输入框可交互并输入
    WebElement usernameInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("Ecom_User_ID")));
    usernameInput.sendKeys("notarealusername");
    
    // 等待密码输入框可交互并输入
    WebElement passwordInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("Ecom_Password")));
    passwordInput.sendKeys("notarealpassword");
    
    // 等待登录按钮可点击并点击
    WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginButton2")));
    loginBtn.click();
    
    // 同时等待标题和URL变化,提升容错性
    wait.until(ExpectedConditions.or(
        ExpectedConditions.titleIs("Continuous Improvement - Event Name Overview"),
        ExpectedConditions.urlContains("target-page-identifier")
    ));
}

3. 验证Github Actions的网络可达性

在gradle.yml中添加步骤,确认运行环境能正常访问目标站点和SSO页面:

- name: Verify site accessibility
  run: |
    curl -I https://notarealurl.com
    curl -I https://your-sso-access-manager-url.com

如果SSO有IP白名单限制,需要将Github Actions的IP段加入白名单,或改用固定IP的自托管runner。

4. 更新依赖版本

当前使用的Selenium和WebDriverManager版本较旧,更新到最新稳定版修复兼容性问题,同时替换已停止维护的jcenter仓库:
修改build.gradle:

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
    implementation 'org.seleniumhq.selenium:selenium-java:4.18.1'
    testImplementation("io.github.bonigarcia:webdrivermanager:5.6.3")
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 23:50:05