Skip to content

Commit 00b0b39

Browse files
committed
[grid] Enhancing CdpEndpointFinder to get debuggerAddress URI
This is part of the work needed for #9327
1 parent be913bb commit 00b0b39

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openqa.selenium.devtools;
1919

20+
import org.openqa.selenium.Capabilities;
2021
import org.openqa.selenium.internal.Require;
2122
import org.openqa.selenium.json.Json;
2223
import org.openqa.selenium.remote.http.ClientConfig;
@@ -68,4 +69,32 @@ public static Optional<URI> getCdpEndPoint(HttpClient.Factory clientFactory, URI
6869
}
6970
}
7071

72+
public static Optional<URI> getReportedUri(String capabilityKey, Capabilities caps) {
73+
Object raw = caps.getCapability(capabilityKey);
74+
75+
if ((raw instanceof Map)) {
76+
raw = ((Map<?, ?>) raw).get("debuggerAddress");
77+
}
78+
79+
if (!(raw instanceof String)) {
80+
LOG.fine("No debugger address");
81+
return Optional.empty();
82+
}
83+
84+
int index = ((String) raw).lastIndexOf(':');
85+
if (index == -1 || index == ((String) raw).length() - 1) {
86+
LOG.fine("No index in " + raw);
87+
return Optional.empty();
88+
}
89+
90+
try {
91+
URI uri = new URI(String.format("http://%s", raw));
92+
LOG.fine("URI found: " + uri);
93+
return Optional.of(uri);
94+
} catch (URISyntaxException e) {
95+
LOG.warning("Unable to create URI from: " + raw);
96+
return Optional.empty();
97+
}
98+
}
99+
71100
}

java/client/test/org/openqa/selenium/chromium/ChromiumDevToolsLocatorTest.java renamed to java/client/test/org/openqa/selenium/devtools/CdpEndpointFinderTest.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.chromium;
18+
package org.openqa.selenium.devtools;
1919

2020
import org.junit.Test;
2121
import org.junit.experimental.categories.Category;
@@ -30,23 +30,36 @@
3030
import static org.assertj.core.api.Assertions.assertThat;
3131

3232
@Category(UnitTests.class)
33-
public class ChromiumDevToolsLocatorTest {
33+
public class CdpEndpointFinderTest {
3434

3535
@Test
3636
public void shouldReturnEmptyIfNoDebuggerAddressIsGiven() {
37-
Optional<URI> uri = ChromiumDevToolsLocator.getReportedUri("foo:options", new ImmutableCapabilities());
37+
Optional<URI> uri = CdpEndpointFinder
38+
.getReportedUri("foo:options", new ImmutableCapabilities());
3839

3940
assertThat(uri).isEmpty();
4041
}
4142

4243
@Test
4344
public void shouldReturnUriIfPresent() {
44-
Capabilities caps = new Json().toType("{\"ms:edgeOptions\": { \"debuggerAddress\": \"localhost:55498\" }}",
45-
Capabilities.class);
45+
Capabilities caps = new Json()
46+
.toType(
47+
"{\"ms:edgeOptions\": { \"debuggerAddress\": \"localhost:55498\" }}",
48+
Capabilities.class);
4649

47-
Optional<URI> uri = ChromiumDevToolsLocator.getReportedUri("ms:edgeOptions", caps);
50+
Optional<URI> uri = CdpEndpointFinder.getReportedUri("ms:edgeOptions", caps);
4851

4952
assertThat(uri.get()).isEqualTo(URI.create("http://localhost:55498"));
5053
}
5154

55+
@Test
56+
public void shouldReturnUriIfPresentAndIsAtTopLevel() {
57+
Capabilities caps = new Json().toType(
58+
"{\"moz:debuggerAddress\": \"localhost:93487\" }",
59+
Capabilities.class);
60+
61+
Optional<URI> uri = CdpEndpointFinder.getReportedUri("moz:debuggerAddress", caps);
62+
63+
assertThat(uri.get()).isEqualTo(URI.create("http://localhost:93487"));
64+
}
5265
}

0 commit comments

Comments
 (0)