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

Docker部署Selenide Java测试:E2E测试执行异常排查求助

Selenide E2E测试在Docker环境中显示0测试运行问题

我为Selenide Java测试构建了Docker镜像,API测试可正常运行,但E2E测试在Docker环境中无法执行,单独运行E2E测试时显示0测试运行,且无任何报错信息。怀疑问题出在Dockerfile中Chrome与ChromeDriver的安装配置,尝试多种安装方式均未解决。相关配置及日志如下:

Dockerfile配置

FROM openjdk:21-ea-21-jdk-slim AS BUILD

# Set the working directory in the container
WORKDIR /app

# Copy the Maven configuration file (pom.xml) to the container
COPY pom.xml .

# Copy the source code into the container
COPY . .

# Build your application with Maven
RUN apt-get update && \
     apt-get install -y maven && \
     apt-get clean;

RUN apt-get update -y && apt-get install -y wget xvfb unzip jq
RUN apt-get update && apt-get install -y curl

RUN apt-get update && apt-get install -y chromium

# Install Google Chrome dependencies
RUN apt-get install -y libxss1 libappindicator1 libgconf-2-4 \
    fonts-liberation libasound2 libnspr4 libnss3 libx11-xcb1 libxtst6 lsb-release xdg-utils \
    libgbm1 libnss3 libatk-bridge2.0-0 libgtk-3-0 libx11-xcb1 libxcb-dri3-0

# Fetch the latest version numbers and URLs for Chrome and ChromeDriver
RUN curl -s https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json > /tmp/versions.json

RUN CHROME_URL=$(jq -r '.channels.Stable.downloads.chrome[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chrome-linux64.zip $CHROME_URL && \
    unzip /tmp/chrome-linux64.zip -d /opt/chrome

RUN chmod +x /opt/chrome/chrome-linux64/chrome

RUN CHROMEDRIVER_URL=$(jq -r '.channels.Stable.downloads.chromedriver[] | select(.platform=="linux64") | .url' /tmp/versions.json) && \
    wget -q --continue -O /tmp/chromedriver-linux64.zip $CHROMEDRIVER_URL && \
    unzip /tmp/chromedriver-linux64.zip -d /opt/chromedriver && \
    chmod +x /opt/chromedriver/chromedriver-linux64/chromedriver

# Set up Chromedriver Environment variables
ENV CHROMEDRIVER_DIR /opt/chromedriver
ENV PATH $CHROMEDRIVER_DIR:$PATH

ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

# Run the application when the container starts
CMD ["mvn", "test"]

测试执行日志

[INFO] 
2024-03-17T19:56:38.573205002Z [INFO] -------------------------------------------------------
2024-03-17T19:56:38.573276927Z [INFO]  T E S T S
2024-03-17T19:56:38.573561943Z [INFO] -------------------------------------------------------
2024-03-17T19:56:39.635558976Z [INFO] Running TestSuite
2024-03-17T19:56:59.738518367Z [TestNG-tests-1] INFO framework.cleanup.CleanupThread - Thread started
2024-03-17T19:57:02.483653897Z [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 22.82 s -- in TestSuite
2024-03-17T19:57:02.820011928Z [INFO] 
2024-03-17T19:57:02.820063444Z [INFO] Results:
2024-03-17T19:57:02.820072141Z [INFO] 
2024-03-17T19:57:02.821374723Z [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
2024-03-17T19:57:02.821432691Z [INFO] 
2024-03-17T19:57:02.825335558Z [INFO] ------------------------------------------------------------------------
2024-03-17T19:57:02.825405780Z [INFO] BUILD SUCCESS
2024-03-17T19:57:02.826307320Z [INFO] ------------------------------------------------------------------------
2024-03-17T19:57:02.827949419Z [INFO] Total time:  02:25 min
2024-03-17T19:57:02.828526872Z [INFO] Finished at: 2024-03-17T19:57:02Z
2024-03-17T19:57:02.829058800Z [INFO] ------------------------------------------------------------------------

Maven Surefire插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-testng</artifactId>
            <version>3.1.2</version>
        </dependency>
    </dependencies>
    <configuration>
        <!-- Create report even if there are test failures -->
        <testFailureIgnore>true</testFailureIgnore>
        <argLine>
            --enable-preview -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
        </argLine>
        <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/SuiteCollection.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

排查与解决建议

1. 确认测试类的扫描与执行规则

  • 检查SuiteCollection.xml是否正确包含E2E测试类,有无过滤规则排除了E2E测试
  • 本地运行E2E测试验证是否正常,排除测试类注解问题(比如@TestNG的@Test注解是否正确添加)
  • 在Docker容器中手动执行mvn test -Dtest=*E2ETest*(替换成你的E2E测试类前缀/后缀),确认是否能识别到测试类

2. 修复Chrome与ChromeDriver的路径配置

  • 当前PATH仅添加了/opt/chromedriver,但ChromeDriver实际位于/opt/chromedriver/chromedriver-linux64/,Chrome位于/opt/chrome/chrome-linux64/,需修正PATH或创建软链接:
    # 创建软链接到系统可执行路径
    RUN ln -s /opt/chrome/chrome-linux64/chrome /usr/bin/chrome
    RUN ln -s /opt/chromedriver/chromedriver-linux64/chromedriver /usr/bin/chromedriver
    
  • 移除冲突的Chromium安装:RUN apt-get remove -y chromium,避免与官方Chrome冲突
  • 在容器内手动验证Chrome可用性:chrome --headless=new --no-sandbox --disable-dev-shm-usage,查看是否能正常启动

3. 配置Selenide无头运行参数

在测试代码或配置文件中添加无头模式配置,确保Docker环境中Chrome以无头模式运行:

import com.codeborne.selenide.Configuration;
import java.util.List;
import java.util.Map;

public class TestConfig {
    static {
        Configuration.browser = "chrome";
        Configuration.headless = true;
        Configuration.browserSize = "1920x1080";
        Configuration.browserCapabilities.setCapability("goog:chromeOptions", Map.of(
            "args", List.of(
                "--no-sandbox",
                "--disable-dev-shm-usage",
                "--disable-gpu",
                "--window-size=1920,1080"
            )
        ));
    }
}

4. 检查Maven测试执行细节

  • 执行mvn test -X开启Debug日志,查看Surefire插件扫描测试类的过程,确认是否找到E2E测试类
  • 验证SuiteCollection.xml在容器中的路径是否正确,执行ls src/test/resources/确认文件存在
  • 检查Surefire插件是否有includes/excludes配置无意中排除了E2E测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 22:47:33