整合Healenium与Selenium时出现java.net.ConnectException错误求助
解决Selenium整合Healenium时的java.net.ConnectException错误
错误原因
这个java.net.ConnectException的核心问题是:Healenium客户端无法连接到其后端服务。Healenium 3.x版本并非纯客户端库,它依赖独立运行的Healenium Backend(包含数据存储和自愈逻辑服务),仅添加Maven依赖不足以让它正常工作。
解决方案
1. 部署Healenium后端服务
推荐用Docker Compose快速部署,步骤如下:
- 创建
docker-compose.yml文件,内容如下:
version: '3.8' services: postgres: image: postgres:13-alpine container_name: healenium-postgres environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: healenium ports: - "5432:5432" volumes: - healenium-postgres:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5 healenium: image: epam/healenium-backend:3.5.2 container_name: healenium ports: - "7878:7878" environment: - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/healenium - SPRING_DATASOURCE_USERNAME=postgres - SPRING_DATASOURCE_PASSWORD=postgres - HEALENIUM_DB_UPDATE=true - HEALENIUM_HEALING_ENABLED=true depends_on: postgres: condition: service_healthy selector-imitator: image: epam/selector-imitator:0.2.1 container_name: selector-imitator ports: - "8085:8085" environment: - IMITATOR_SERVER_URL=http://0.0.0.0:8085 volumes: healenium-postgres:
- 打开终端,进入
docker-compose.yml所在目录,执行命令启动服务:
docker-compose up -d
- 用
docker ps命令检查三个容器(postgres、healenium、selector-imitator)是否都处于Up状态。
2. 配置测试代码连接后端
在测试代码中添加Healenium后端地址配置,修改后的完整代码如下:
package selhel; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import com.epam.healenium.SelfHealingDriver; public class SearchTest { @Test public void firstTest() { // 配置ChromeDriver路径(Selenium 4.6+可省略,会自动管理驱动) System.setProperty("webdriver.chrome.driver","C://Program Files/ChromeDriver/chromedriver.exe"); // 配置Healenium后端地址(默认就是localhost:7878,可省略;若后端部署在其他机器需修改) System.setProperty("healenium.url", "http://localhost:7878"); // 启用Healenium自愈功能 System.setProperty("healenium.enabled", "true"); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); WebDriver delegate = new ChromeDriver(options); SelfHealingDriver driver = SelfHealingDriver.create(delegate); driver.manage().window().maximize(); driver.get("https://www.google.com"); driver.findElement(By.xpath("//a[text()='Gmail']")).click(); // 测试结束后关闭驱动 driver.quit(); } }
3. 额外验证项
- 确保ChromeDriver版本与本地Chrome浏览器版本完全匹配(比如Chrome 124对应ChromeDriver 124.x)
- 检查7878、5432、8085端口是否被其他程序占用,若占用需修改docker-compose.yml中的端口映射
- 国内网络下若镜像拉取缓慢,可配置Docker国内镜像源加速
内容的提问来源于stack exchange,提问作者KK12
相关产品推荐
相关产品推荐

