|
2 | 2 |
|
3 | 3 | import dev.selenium.BaseTest; |
4 | 4 |
|
| 5 | +import org.openqa.selenium.remote.http.ClientConfig; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 9 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 10 | + |
| 11 | +import javax.net.ssl.SSLContext; |
| 12 | +import javax.net.ssl.TrustManager; |
| 13 | +import javax.net.ssl.TrustManagerFactory; |
| 14 | +import javax.net.ssl.X509TrustManager; |
| 15 | +import java.io.FileInputStream; |
| 16 | +import java.net.URL; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.security.KeyStore; |
| 19 | +import java.security.cert.CertificateFactory; |
| 20 | +import java.security.cert.X509Certificate; |
| 21 | +import java.time.Duration; |
| 22 | + |
| 23 | +import org.openqa.selenium.UsernameAndPassword; |
| 24 | + |
| 25 | +import static java.net.http.HttpClient.Version.HTTP_1_1; |
| 26 | + |
5 | 27 | public class HttpClientTest extends BaseTest { |
| 28 | + URL gridUrl; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + public void startGrid() { |
| 32 | + gridUrl = startStandaloneGridAdvanced(); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void remoteWebDriverWithClientConfig() throws Exception { |
| 37 | + ClientConfig clientConfig = ClientConfig.defaultConfig() |
| 38 | + .withRetries() |
| 39 | + .sslContext(createSSLContextWithCA(Path.of("src/test/resources/tls.crt").toAbsolutePath().toString())) |
| 40 | + .connectionTimeout(Duration.ofSeconds(300)) |
| 41 | + .readTimeout(Duration.ofSeconds(3600)) |
| 42 | + .authenticateAs(new UsernameAndPassword("admin", "myStrongPassword")) |
| 43 | + .version(HTTP_1_1.toString()); |
| 44 | + ChromeOptions options = new ChromeOptions(); |
| 45 | + options.setEnableDownloads(true); |
| 46 | + driver = RemoteWebDriver.builder() |
| 47 | + .oneOf(options) |
| 48 | + .address(gridUrl) |
| 49 | + .config(clientConfig) |
| 50 | + .build(); |
| 51 | + driver.quit(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void remoteWebDriverIgnoreSSL() throws Exception { |
| 56 | + ClientConfig clientConfig = ClientConfig.defaultConfig() |
| 57 | + .withRetries() |
| 58 | + .sslContext(createIgnoreSSLContext()) |
| 59 | + .connectionTimeout(Duration.ofSeconds(300)) |
| 60 | + .readTimeout(Duration.ofSeconds(3600)) |
| 61 | + .authenticateAs(new UsernameAndPassword("admin", "myStrongPassword")) |
| 62 | + .version(HTTP_1_1.toString()); |
| 63 | + ChromeOptions options = new ChromeOptions(); |
| 64 | + options.setEnableDownloads(true); |
| 65 | + driver = RemoteWebDriver.builder() |
| 66 | + .oneOf(options) |
| 67 | + .address(gridUrl) |
| 68 | + .config(clientConfig) |
| 69 | + .build(); |
| 70 | + driver.quit(); |
| 71 | + } |
| 72 | + |
| 73 | + public static SSLContext createSSLContextWithCA(String caCertPath) throws Exception { |
| 74 | + FileInputStream fis = new FileInputStream(caCertPath); |
| 75 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 76 | + X509Certificate caCert = (X509Certificate) cf.generateCertificate(fis); |
| 77 | + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 78 | + keyStore.load(null, null); |
| 79 | + keyStore.setCertificateEntry("caCert", caCert); |
| 80 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 81 | + tmf.init(keyStore); |
| 82 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 83 | + sslContext.init(null, tmf.getTrustManagers(), null); |
| 84 | + return sslContext; |
| 85 | + } |
| 86 | + |
| 87 | + public static SSLContext createIgnoreSSLContext() throws Exception { |
| 88 | + TrustManager[] trustAllCerts = new TrustManager[]{ |
| 89 | + new X509TrustManager() { |
| 90 | + public X509Certificate[] getAcceptedIssuers() { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + public void checkClientTrusted(X509Certificate[] certs, String authType) { |
| 95 | + } |
6 | 96 |
|
| 97 | + public void checkServerTrusted(X509Certificate[] certs, String authType) { |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 102 | + sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); |
| 103 | + return sslContext; |
| 104 | + } |
7 | 105 | } |
0 commit comments