在TeamCity中无法在Docker容器内运行Selenium测试
在TeamCity Docker容器中运行Selenium无头测试失败
问题说明
本地执行包含OpenJDK、Maven、Selenium、无头Chrome的测试项目完全正常,但通过TeamCity使用自定义Dockerfile运行时,出现元素定位失败的错误。
本地可正常执行的命令
cd to/my/src/code mvn clean mvn test
使用的Dockerfile
# escape=` FROM mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2016 SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] # Install Chocolatey RUN write-host "*********** Install Chocolatey" ARG chocolateyVersion='0.10.11' RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) # Install googlechrome RUN write-host "*********** Install googlechrome" RUN choco install googlechrome -y # Install openjdk RUN write-host "*********** Install openjdk" RUN choco install openjdk --version=18.0.1.1 -y # Install maven RUN write-host "*********** Install maven" RUN choco install maven --version=3.8.6 -y # Verify installation RUN java --version RUN mvn −version
TeamCity报错信息
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of Proxy element for: DefaultElementLocator 'By.id: LoginName' (tried for 40 second(s) with 500 milliseconds interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) at com.mycompany.pages.PublishAccountPage.loginFunctionForJCR(PublishAccountPage.java:44) at com.mycompany.tests.SampleTest.Sample(SampleTest.java:25) Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#LoginName"} (Session info: headless chrome=103.0.5060.134)
TeamCity构建步骤

排查与解决方案
1. 匹配ChromeDriver与Chrome版本
报错显示Chrome版本为103.0.5060.134,需确保测试代码中使用的ChromeDriver版本与之完全对应。可在Dockerfile中安装指定版本的Chrome,或在测试代码中指定对应版本的ChromeDriver,避免版本不兼容导致页面加载异常。
2. 优化无头Chrome启动参数
无头模式默认窗口尺寸较小,可能导致页面布局异常,需添加必要启动参数:
ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); // 启用新版无头模式,更接近正常Chrome行为 options.addArguments("--window-size=1920,1080"); // 设置标准窗口尺寸 options.addArguments("--no-sandbox"); // 关闭沙箱,适配容器运行环境 options.addArguments("--disable-dev-shm-usage"); // 解决容器内/dev/shm内存不足问题 WebDriver driver = new ChromeDriver(options);
3. 调整页面等待逻辑
- 检查
LoginName元素是否嵌套在iframe中,若是需先切换到对应iframe再定位元素。 - 增加页面加载完成的等待逻辑,确保页面资源加载完毕再定位元素:
new WebDriverWait(driver, Duration.ofSeconds(40)).until( webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete") );
4. 配置Docker环境变量
Docker中安装的JDK和Maven需确保环境变量正确配置,避免测试时路径异常。可在Dockerfile末尾添加:
ENV JAVA_HOME="C:\Program Files\OpenJDK\jdk-18.0.1.1" ENV PATH="$PATH;$JAVA_HOME\bin;C:\Program Files\Apache\maven\bin"
(路径可根据Chocolatey实际安装位置调整)
5. 检查容器网络连通性
在Dockerfile中添加curl或Invoke-WebRequest命令,确认容器内可正常访问测试目标网站,排除网络不通导致的页面加载失败。
内容的提问来源于stack exchange,提问作者Alvin
相关产品推荐
相关产品推荐

