Skip to content

Commit f01b968

Browse files
committed
[java] Ensure DevTools is augmented when using DriverService
Fixes #10413
1 parent 1f4cf23 commit f01b968

4 files changed

Lines changed: 102 additions & 12 deletions

File tree

java/src/org/openqa/selenium/devtools/CdpEndpointFinder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ public static Optional<URI> getCdpEndPoint(HttpClient.Factory clientFactory, URI
7676
}
7777
}
7878

79+
public static Optional<URI> getReportedUri(Capabilities caps) {
80+
String key;
81+
switch (caps.getBrowserName()) {
82+
case "chrome":
83+
key = "goog:chromeOptions";
84+
break;
85+
case "msedge":
86+
key = "ms:edgeOptions";
87+
break;
88+
case "firefox":
89+
key = "moz:debuggerAddress";
90+
break;
91+
default:
92+
return Optional.empty();
93+
}
94+
return getReportedUri(key, caps);
95+
}
96+
7997
public static Optional<URI> getReportedUri(String capabilityKey, Capabilities caps) {
8098
Object raw = caps.getCapability(capabilityKey);
8199

java/src/org/openqa/selenium/devtools/DevToolsProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openqa.selenium.remote.AugmenterProvider;
2424
import org.openqa.selenium.remote.ExecuteMethod;
2525

26+
import java.net.URI;
2627
import java.util.Optional;
2728
import java.util.function.Predicate;
2829

@@ -52,10 +53,12 @@ public HasDevTools getImplementation(Capabilities caps, ExecuteMethod executeMet
5253

5354
private String getCdpUrl(Capabilities caps) {
5455
Object cdp = caps.getCapability("se:cdp");
55-
if (!(cdp instanceof String)) {
56-
return null;
56+
if (cdp instanceof String) {
57+
return (String) cdp;
5758
}
5859

59-
return (String) cdp;
60+
Optional<URI> reportedUri = CdpEndpointFinder.getReportedUri(caps);
61+
62+
return reportedUri.map(URI::toString).orElse(null);
6063
}
6164
}

java/src/org/openqa/selenium/devtools/SeleniumCdpConnection.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,25 @@ public static Optional<Connection> create(HttpClient.Factory clientFactory, Capa
5151
Require.nonNull("HTTP client factory", clientFactory);
5252
Require.nonNull("Capabilities", capabilities);
5353

54-
return getCdpUri(capabilities).map(uri -> new SeleniumCdpConnection(
54+
return getCdpUri(clientFactory, capabilities).map(uri -> new SeleniumCdpConnection(
5555
clientFactory.createClient(ClientConfig.defaultConfig().baseUri(uri)),
5656
uri.toString()));
5757
}
5858

59-
public static Optional<URI> getCdpUri(Capabilities capabilities) {
59+
public static Optional<URI> getCdpUri(HttpClient.Factory clientFactory,
60+
Capabilities capabilities) {
6061
Object cdp = capabilities.getCapability("se:cdp");
6162

62-
if (!(cdp instanceof String)) {
63-
return Optional.empty();
63+
if (cdp instanceof String) {
64+
try {
65+
return Optional.of(new URI((String) cdp));
66+
} catch (URISyntaxException e) {
67+
return Optional.empty();
68+
}
6469
}
6570

66-
try {
67-
return Optional.of(new URI((String) cdp));
68-
} catch (URISyntaxException e) {
69-
return Optional.empty();
70-
}
71+
Optional<URI> reportedUri = CdpEndpointFinder.getReportedUri(capabilities);
72+
73+
return reportedUri.flatMap(uri -> CdpEndpointFinder.getCdpEndPoint(clientFactory, uri));
7174
}
7275
}

java/test/org/openqa/selenium/remote/RemoteWebDriverBuilderTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.openqa.selenium.SessionNotCreatedException;
2626
import org.openqa.selenium.WebDriver;
2727
import org.openqa.selenium.chrome.ChromeOptions;
28+
import org.openqa.selenium.devtools.DevTools;
29+
import org.openqa.selenium.devtools.HasDevTools;
2830
import org.openqa.selenium.firefox.FirefoxOptions;
2931
import org.openqa.selenium.ie.InternetExplorerOptions;
3032
import org.openqa.selenium.json.Json;
@@ -383,6 +385,70 @@ public void shouldAugmentDriverIfPossible() {
383385
assertThat(number).isEqualTo(1);
384386
}
385387

388+
@Test
389+
public void shouldAugmentDriverWhenUsingDriverService() throws IOException {
390+
URI uri = URI.create("http://localhost:9898");
391+
URL url = uri.toURL();
392+
393+
DriverService service = new FakeDriverService() {
394+
@Override
395+
public URL getUrl() {
396+
return url;
397+
}
398+
};
399+
400+
HttpResponse response = new HttpResponse()
401+
.setContent(Contents.asJson(ImmutableMap.of(
402+
"value", ImmutableMap.of(
403+
"sessionId", SESSION_ID,
404+
"capabilities", new ImmutableCapabilities("firefox", "caps")))));
405+
406+
Augmenter augmenter = new Augmenter().addDriverAugmentation("firefox",
407+
AugmenterTest.HasMagicNumbers.class,
408+
(c, exe) -> () -> 1);
409+
WebDriver driver = RemoteWebDriver.builder()
410+
.oneOf(new FirefoxOptions())
411+
.withDriverService(service)
412+
.augmentUsing(augmenter)
413+
.connectingWith(config -> req -> response)
414+
.build();
415+
416+
int number = ((AugmenterTest.HasMagicNumbers) driver).getMagicNumber();
417+
418+
assertThat(driver).isInstanceOf(AugmenterTest.HasMagicNumbers.class);
419+
assertThat(number).isEqualTo(1);
420+
}
421+
422+
@Test
423+
public void shouldAugmentWithDevToolsWhenUsingDriverService() throws IOException {
424+
URI uri = URI.create("http://localhost:9898");
425+
URL url = uri.toURL();
426+
427+
DriverService service = new FakeDriverService() {
428+
@Override
429+
public URL getUrl() {
430+
return url;
431+
}
432+
};
433+
434+
HttpResponse response = new HttpResponse()
435+
.setContent(Contents.asJson(ImmutableMap.of(
436+
"value", ImmutableMap.of(
437+
"sessionId", SESSION_ID,
438+
"capabilities", new ImmutableCapabilities("firefox", "caps",
439+
"browserName", "firefox",
440+
"moz:debuggerAddress", uri.toString())))));
441+
442+
WebDriver driver = RemoteWebDriver.builder()
443+
.oneOf(new FirefoxOptions())
444+
.withDriverService(service)
445+
.augmentUsing(new Augmenter())
446+
.connectingWith(config -> req -> response)
447+
.build();
448+
449+
assertThat(driver).isInstanceOf(HasDevTools.class);
450+
}
451+
386452
@SuppressWarnings("unchecked")
387453
private List<Capabilities> listCapabilities(HttpRequest request) {
388454
Map<String, Object> converted = new Json().toType(Contents.string(request), MAP_TYPE);

0 commit comments

Comments
 (0)