SpringBoot+Selenium自动化测试中浏览器意外关闭及Connection reset问题求助
问题场景
基于Java+SpringBoot+Selenium搭建的自动化测试项目,启动初期运行正常,但测试执行中途浏览器会自行关闭,控制台抛出java.net.SocketException: Connection reset错误,JUnit定位错误发生在fillForm方法中。
控制台错误日志
2023-06-14T15:27:44.720-03:00 WARN 10544 --- [cHttpClient-1-4] o.a.netty.handler.WebSocketHandler : onError
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394) ~[na:na]
at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426) ~[na:na]
at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:256) ~[netty-buffer-4.1.92.Final.jar:4.1.92.Final]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) ~[netty-buffer-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]2023-06-14T15:27:44.736-03:00 WARN 10544 --- [cHttpClient-1-4] o.openqa.selenium.remote.http.WebSocket : Connection reset
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394) ~[na:na]
at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426) ~[na:na]
at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:256) ~[netty-buffer-4.1.92.Final.jar:4.1.92.Final]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) ~[netty-buffer-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.92.Final.jar:4.1.92.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.92.Final.jar:4.1.92.Final]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
已尝试的无效操作
- 修改网络适配器IPv4配置指定首选DNS
- 执行命令:
ipconfig /flushdns、ipconfig /release、ipconfig /renew、netsh winsock reset
出错代码片段
public void fillForm(String nombre, String email, String asunto, String mensaje) throws InterruptedException { WebElement nombreInput = driver.findElement(By.name("your-name")); nombreInput.sendKeys(nombre); WebElement emailInput = driver.findElement(By.name("your-email")); emailInput.sendKeys(email); WebElement asuntoInput = driver.findElement(By.name("your-subject")); asuntoInput.sendKeys(asunto); WebElement mensajeInput = driver.findElement(By.name("your-message")); mensajeInput.sendKeys(mensaje); }
排查与解决方案
1. 校验Selenium与浏览器驱动版本兼容性
Socket重置问题常因驱动与浏览器版本不匹配导致通信中断,需确认:
- ChromeDriver版本与本地Chrome浏览器大版本完全一致,小版本尽量匹配
- GeckoDriver(Firefox)或EdgeDriver同理,需与对应浏览器版本严格兼容
2. 替换为显式等待策略
当前代码直接调用findElement,若页面未完全加载就执行操作,易引发驱动与浏览器通信异常。修改为显式等待:
public void fillForm(String nombre, String email, String asunto, String mensaje) { WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement nombreInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("your-name"))); nombreInput.sendKeys(nombre); WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("your-email"))); emailInput.sendKeys(email); WebElement asuntoInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("your-subject"))); asuntoInput.sendKeys(asunto); WebElement mensajeInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("your-message"))); mensajeInput.sendKeys(mensaje); }
3. 禁用WebSocket通信(临时 workaround)
部分环境下Selenium的WebSocket通信易出现连接问题,可强制使用HTTP协议:
ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); // 禁用WebSocket,改用HTTP通信 options.setCapability("se:options", Map.of("webSocketUrl", false)); WebDriver driver = new ChromeDriver(options);
4. 规范驱动进程生命周期管理
测试过程中驱动进程意外终止会导致浏览器关闭,需优化代码:
- 每个测试用例执行完毕后,调用
driver.quit()而非driver.close(),确保彻底清理驱动进程 - 在SpringBoot环境中,将WebDriver配置为线程安全的单例或使用线程池管理,避免资源泄漏
5. 排查系统级网络限制
- 检查防火墙、杀毒软件是否拦截了Selenium驱动与浏览器的通信端口
- 尝试在无代理环境下运行测试,排除代理服务器中断连接的可能
内容的提问来源于stack exchange,提问作者Lucas Gümil

