-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Description
Description
We have many Selenium tests, that utilize the CDP network interception feature. After updating to Chrome 136 and Selenium DevTools 4.32 we noticed, that the network interception only works half the time. Switching back to Chrome 135 and DevTools 4.31 fixes the issue. I also tried with the newest versions (Chrome 138, DevTools 4.34), but to no avail.
I was able to narrow down the issue to a combination of the following actions:
- Open a new window with the webdriver and switch to it.
- Create a new session with DevTools
If you remove any one of those steps, the issue does not occur.
Our tests run on a chrome selenoid docker image inside Testcontainers (Version 1.21.3). I also tried a standard chrome standalone image, but with the same results. The below code should reproduce the issue (after a couple of tries).
Any ideas, as to what causes the issue? It seems to me like the network interceptor is not hooked up correctly.
Log of the failed test:
2025-07-17 15:57:34,902 INFO [main] org.testcontainers.images.PullPolicy.defaultPolicy():59 - Image pull policy will be performed by: DefaultPullPolicy()
2025-07-17 15:57:34,913 INFO [main] org.testcontainers.utility.ImageNameSubstitutor.instance():55 - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
2025-07-17 15:57:34,966 INFO [main] org.testcontainers.DockerClientFactory.getOrInitializeStrategy():150 - Testcontainers version: 1.21.3
2025-07-17 15:57:35,198 INFO [main] org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$loadConfiguredStrategy$9():380 - Loaded org.testcontainers.dockerclient.NpipeSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
2025-07-17 15:57:35,446 INFO [main] org.testcontainers.dockerclient.DockerClientProviderStrategy.tryOutStrategy():290 - Found Docker environment with local Npipe socket (npipe:////./pipe/docker_engine)
2025-07-17 15:57:35,449 INFO [main] org.testcontainers.DockerClientFactory.client():208 - Docker host IP address is localhost
2025-07-17 15:57:35,473 INFO [main] org.testcontainers.DockerClientFactory.client():241 - Connected to docker:
Server Version: 27.3.1
API Version: 1.47
Operating System: Rancher Desktop WSL Distribution
Total Memory: 15811 MB
2025-07-17 15:57:35,496 INFO [main] org.testcontainers.containers.GenericContainer.tryStart():379 - Creating container for image: testcontainers/ryuk:0.12.0
2025-07-17 15:57:35,790 INFO [main] org.testcontainers.containers.GenericContainer.tryStart():443 - Container testcontainers/ryuk:0.12.0 is starting: 3738f86e7a2a3ad58b2e9c951d2349d8096c2ddfccf5333ea14a4641ba47d8cd
2025-07-17 15:57:36,248 INFO [main] org.testcontainers.containers.GenericContainer.tryStart():533 - Container testcontainers/ryuk:0.12.0 started in PT0.7520495S
2025-07-17 15:57:36,253 INFO [main] org.testcontainers.utility.RyukResourceReaper.init():43 - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
2025-07-17 15:57:36,254 INFO [main] org.testcontainers.DockerClientFactory.client():256 - Checking the system...
2025-07-17 15:57:36,254 INFO [main] org.testcontainers.DockerClientFactory.check():277 - ✔︎ Docker server version should be at least 1.6.0
2025-07-17 15:57:36,267 INFO [main] org.testcontainers.containers.SeleniumUtils.determineClasspathSeleniumVersion():44 - Selenium API version 4.34.0 detected on classpath
2025-07-17 15:57:36,281 INFO [main] org.testcontainers.containers.GenericContainer.tryStart():379 - Creating container for image: twilio/selenoid:chrome_stable_138
2025-07-17 15:57:36,317 INFO [main] org.testcontainers.containers.GenericContainer.tryStart():443 - Container twilio/selenoid:chrome_stable_138 is starting: ba45dbcccffffbd2718c47ff052d53c0a71179ed9a041f399e1296d891ee751b
2025-07-17 15:57:37,020 INFO [main] org.testcontainers.containers.GenericContainer.tryStart():533 - Container twilio/selenoid:chrome_stable_138 started in PT0.739049S
Juli 17, 2025 3:57:37 PM org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer createTracer
INFORMATION: Using OpenTelemetry for tracing
2025-07-17 15:57:37,763 INFO [main] com.enbw.eop.selenium.ReproduceIssueTest.remoteWebDriver():85 - created session with capabilities: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 138.0.7204.100, chrome: {chromedriverVersion: 138.0.7204.94 (545025748884..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:35623}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
java.lang.AssertionError:
Expecting actual:
0
to be greater than:
0
at com.enbw.eop.selenium.ReproduceIssueTest.testInterceptor(ReproduceIssueTest.java:78)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Reproducible Code
package com.enbw.eop.selenium;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.NetworkInterceptor;
import org.openqa.selenium.devtools.v138.network.Network;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.Filter;
import org.openqa.selenium.remote.http.HttpResponse;
import org.slf4j.Logger;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.utility.DockerImageName;
class ReproduceIssueTest {
private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(ReproduceIssueTest.class);
private static final int SELENOID_DEVTOOLS_PROXY_CONTAINER_PORT = 7070;
@Test
void testInterceptor() {
try (BrowserWebDriverContainer<?> container = new BrowserWebDriverContainer<>(
DockerImageName.parse("twilio/selenoid:chrome_stable_138")
.asCompatibleSubstituteFor("selenium/standalone-chrome"))
.withExposedPorts(SELENOID_DEVTOOLS_PROXY_CONTAINER_PORT)
.waitingFor(getWaitStrategy())) {
container.withEnv(Map.of("DRIVER_ARGS", "--url-base=/wd/hub --verbose"));
container.start();
RemoteWebDriver driver = remoteWebDriver(container, new ChromeOptions());
MutableCapabilities mutableCapabilities = (MutableCapabilities) driver.getCapabilities();
mutableCapabilities.setCapability("se:cdp", "http://localhost:" + container.getMappedPort(SELENOID_DEVTOOLS_PROXY_CONTAINER_PORT));
WebDriver webDriver = new Augmenter().augment(driver);
// open a new window and switch to it
webDriver = webDriver.switchTo().newWindow(WindowType.WINDOW);
DevTools devTools = ((HasDevTools) webDriver).getDevTools();
devTools.createSession();
devTools.send(Network.clearBrowserCache());
AtomicInteger interceptedCount = new AtomicInteger();
Filter filter = httpHandler -> req -> {
HttpResponse response = httpHandler.execute(req);
interceptedCount.incrementAndGet();
LOG.info("Captured request: {}", req.getUri());
return response;
};
try (NetworkInterceptor interceptor = new NetworkInterceptor(webDriver, filter)) {
webDriver.navigate().to("https://www.example.com");
// wait for some requests to be made
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
}
} finally {
webDriver.quit();
assertThat(interceptedCount).hasValueGreaterThan(0);
}
}
}
public static RemoteWebDriver remoteWebDriver(BrowserWebDriverContainer<?> container, Capabilities capabilities) {
RemoteWebDriver remoteWebDriver = new RemoteWebDriver(container.getSeleniumAddress(), capabilities);
LOG.info("created session with capabilities: {}", remoteWebDriver.getCapabilities());
return remoteWebDriver;
}
private static WaitStrategy getWaitStrategy() {
final WaitStrategy logWaitStrategy = new LogMessageWaitStrategy()
.withRegEx(
".*ChromeDriver was started successfully.*\n"
)
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS));
return new WaitAllStrategy()
.withStrategy(logWaitStrategy)
.withStrategy(new HostPortWaitStrategy())
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS));
}
}
ℹ️ Last known working version: 4.31