From 29e67b7b0f5d19485bb399e81b2243975f641963 Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Wed, 15 Jan 2025 18:35:15 +0700 Subject: [PATCH] [java]: Example create RemoteWebDriver with embedded authentication in URL Signed-off-by: Viet Nguyen Duc --- .../dev/selenium/drivers/HttpClientTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/examples/java/src/test/java/dev/selenium/drivers/HttpClientTest.java b/examples/java/src/test/java/dev/selenium/drivers/HttpClientTest.java index 8304dba81dd6..0ae167cb711a 100644 --- a/examples/java/src/test/java/dev/selenium/drivers/HttpClientTest.java +++ b/examples/java/src/test/java/dev/selenium/drivers/HttpClientTest.java @@ -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");