Skip to content

Commit 29e67b7

Browse files
committed
[java]: Example create RemoteWebDriver with embedded authentication in URL
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent f021281 commit 29e67b7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

examples/java/src/test/java/dev/selenium/drivers/HttpClientTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ public void remoteWebDriverIgnoreSSL() throws Exception {
7070
driver.quit();
7171
}
7272

73+
@Test
74+
public void remoteWebDriverWithEmbedAuthUrl() throws Exception {
75+
ClientConfig clientConfig = ClientConfig.defaultConfig()
76+
.withRetries()
77+
.sslContext(createSSLContextWithCA(Path.of("src/test/resources/tls.crt").toAbsolutePath().toString()))
78+
.connectionTimeout(Duration.ofSeconds(300))
79+
.readTimeout(Duration.ofSeconds(3600))
80+
.version(HTTP_1_1.toString());
81+
ChromeOptions options = new ChromeOptions();
82+
options.setEnableDownloads(true);
83+
driver = RemoteWebDriver.builder()
84+
.oneOf(options)
85+
.address(embedAuthToUrl(gridUrl, "admin", "myStrongPassword"))
86+
.config(clientConfig)
87+
.build();
88+
driver.quit();
89+
}
90+
91+
private URL embedAuthToUrl(URL url, String username, String password) throws Exception {
92+
String userInfo = username + ":" + password;
93+
String urlWithAuth = url.getProtocol() + "://" + userInfo + "@" + url.getHost() + ":" + url.getPort() + url.getPath();
94+
return new URL(urlWithAuth);
95+
}
96+
7397
public static SSLContext createSSLContextWithCA(String caCertPath) throws Exception {
7498
FileInputStream fis = new FileInputStream(caCertPath);
7599
CertificateFactory cf = CertificateFactory.getInstance("X.509");

0 commit comments

Comments
 (0)