Skip to content

[java]: Example create RemoteWebDriver with embedded authentication in URL #2131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ public void remoteWebDriverIgnoreSSL() throws Exception {
driver.quit();
}

@Test
public void remoteWebDriverWithEmbedAuthUrl() throws Exception {
ClientConfig clientConfig = ClientConfig.defaultConfig()
.withRetries()
.sslContext(createSSLContextWithCA(Path.of("src/test/resources/tls.crt").toAbsolutePath().toString()))
.connectionTimeout(Duration.ofSeconds(300))
.readTimeout(Duration.ofSeconds(3600))
.version(HTTP_1_1.toString());
ChromeOptions options = new ChromeOptions();
options.setEnableDownloads(true);
driver = RemoteWebDriver.builder()
.oneOf(options)
.address(embedAuthToUrl(gridUrl, "admin", "myStrongPassword"))
.config(clientConfig)
.build();
driver.quit();
}

private URL embedAuthToUrl(URL url, String username, String password) throws Exception {
String userInfo = username + ":" + password;
String urlWithAuth = url.getProtocol() + "://" + userInfo + "@" + url.getHost() + ":" + url.getPort() + url.getPath();
return new URL(urlWithAuth);
}

public static SSLContext createSSLContextWithCA(String caCertPath) throws Exception {
FileInputStream fis = new FileInputStream(caCertPath);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Expand Down
Loading