Skip to content

Commit 2ce6752

Browse files
authored
Add examples for Drivers HTTP Client in Java and Python (#2041)
* Add examples for http_client java and python * Updated for other languages * Fix flaky test in examples python (#2042) [deploy site] --------- Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent e2defad commit 2ce6752

File tree

28 files changed

+476
-97
lines changed

28 files changed

+476
-97
lines changed

.github/workflows/java-examples.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,50 @@ jobs:
4545
if: matrix.os == 'ubuntu'
4646
run: Xvfb :99 &
4747
- name: Set up Java
48+
id: java
4849
uses: actions/setup-java@v4
4950
with:
5051
distribution: 'temurin'
5152
java-version: 17
53+
- name: Import test cert non-Windows
54+
if: matrix.os != 'windows'
55+
run: sudo keytool -import -noprompt -trustcacerts -alias SeleniumHQ -file examples/java/src/test/resources/tls.crt -keystore ${{ steps.java.outputs.path }}/lib/security/cacerts -storepass changeit
56+
- name: Import test cert Windows
57+
if: matrix.os == 'windows'
58+
run: keytool -import -noprompt -trustcacerts -alias SeleniumHQ -file examples/java/src/test/resources/tls.crt -keystore ${{ steps.java.outputs.path }}/lib/security/cacerts -storepass changeit
5259
- name: Run Tests Stable
5360
if: matrix.release == 'stable'
5461
uses: nick-invision/[email protected]
5562
with:
56-
timeout_minutes: 20
63+
timeout_minutes: 40
5764
max_attempts: 3
5865
command: |
5966
cd examples/java
60-
mvn -B test
67+
mvn -B test -D"jdk.internal.httpclient.disableHostnameVerification=true"
6168
- name: Run Tests Nightly Linux/macOS
6269
if: matrix.release == 'nightly' && matrix.os != 'windows'
6370
uses: nick-invision/[email protected]
6471
with:
65-
timeout_minutes: 20
72+
timeout_minutes: 40
6673
max_attempts: 3
6774
command: |
6875
pip install yq
6976
xml_content=$(curl -sf https://oss.sonatype.org/service/local/repositories/snapshots/content/org/seleniumhq/selenium/selenium-java/)
7077
latest_snapshot=$(echo "$xml_content" | xq '.content.data."content-item"' | jq -r 'sort_by(.lastModified) | last | .text')
7178
echo "Latest Selenium Snapshot: $latest_snapshot"
7279
cd examples/java
73-
mvn -B -U test -Dselenium.version="$latest_snapshot"
80+
mvn -B -U test -D"jdk.internal.httpclient.disableHostnameVerification=true"
7481
7582
- name: Run Tests Nightly Windows
7683
if: matrix.release == 'nightly' && matrix.os == 'windows'
7784
uses: nick-invision/[email protected]
7885
with:
79-
timeout_minutes: 20
86+
timeout_minutes: 40
8087
max_attempts: 3
8188
command: |
8289
pip install yq
8390
$xml_content = Invoke-WebRequest -Uri "https://oss.sonatype.org/service/local/repositories/snapshots/content/org/seleniumhq/selenium/selenium-java/"
8491
$latest_snapshot = $xml_content.Content | xq '.content.data.\"content-item\"' | jq -r 'sort_by(.lastModified) | last | .text'
8592
Write-Output "Latest Selenium Snapshot: $latest_snapshot"
8693
cd examples/java
87-
mvn -B -U test "-Dselenium.version=$latest_snapshot"
94+
mvn -B -U test -D"jdk.internal.httpclient.disableHostnameVerification=true"

.github/workflows/python-examples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
- name: Run tests
8181
uses: nick-invision/[email protected]
8282
with:
83-
timeout_minutes: 20
83+
timeout_minutes: 40
8484
max_attempts: 3
8585
command: |
8686
cd examples/python

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ website_and_docs/resources
1111
.settings
1212
.gitignore
1313
.pydevproject
14+
**/*.iml
15+
**/.gradle

examples/java/src/test/java/dev/selenium/BaseTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.net.URL;
66
import java.nio.file.Files;
7+
import java.nio.file.Path;
78
import java.time.Duration;
89
import java.util.Arrays;
910
import java.util.logging.Level;
@@ -26,6 +27,9 @@ public class BaseTest {
2627
protected WebDriverWait wait;
2728
protected File driverPath;
2829
protected File browserPath;
30+
protected String username = "admin";
31+
protected String password = "myStrongPassword";
32+
protected String trustStorePassword = "seleniumkeystore";
2933

3034
public WebElement getLocatedElement(WebDriver driver, By by) {
3135
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
@@ -98,6 +102,38 @@ protected URL startStandaloneGrid() {
98102
}
99103
}
100104

105+
protected URL startStandaloneGridAdvanced() {
106+
int port = PortProber.findFreePort();
107+
try {
108+
System.setProperty("javax.net.ssl.trustStore", Path.of("src/test/resources/server.jks").toAbsolutePath().toString());
109+
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
110+
System.setProperty("jdk.internal.httpclient.disableHostnameVerification", "true");
111+
Main.main(
112+
new String[] {
113+
"standalone",
114+
"--port",
115+
String.valueOf(port),
116+
"--selenium-manager",
117+
"true",
118+
"--enable-managed-downloads",
119+
"true",
120+
"--log-level",
121+
"WARNING",
122+
"--username",
123+
username,
124+
"--password",
125+
password,
126+
"--https-certificate",
127+
Path.of("src/test/resources/tls.crt").toAbsolutePath().toString(),
128+
"--https-private-key",
129+
Path.of("src/test/resources/tls.key").toAbsolutePath().toString()
130+
});
131+
return new URL("https://localhost:" + port);
132+
} catch (Exception e) {
133+
throw new RuntimeException(e);
134+
}
135+
}
136+
101137
protected void enableLogging() {
102138
Logger logger = Logger.getLogger("");
103139
logger.setLevel(Level.FINE);

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,104 @@
22

33
import dev.selenium.BaseTest;
44

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+
527
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+
}
696

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+
}
7105
}
2.83 KB
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIEAzCCAuugAwIBAgIIPgWI/2ppJPowDQYJKoZIhvcNAQELBQAwgYcxEDAOBgNV
3+
BAYTB1Vua25vd24xEDAOBgNVBAgTB1Vua25vd24xEDAOBgNVBAcTB1Vua25vd24x
4+
EzARBgNVBAoTClNlbGVuaXVtSFExJTAjBgNVBAsTHFNvZnR3YXJlIEZyZWVkb20g
5+
Q29uc2VydmFuY3kxEzARBgNVBAMTClNlbGVuaXVtSFEwHhcNMjQxMTAzMDkwMDUz
6+
WhcNMzQxMTAxMDkwMDUzWjCBhzEQMA4GA1UEBhMHVW5rbm93bjEQMA4GA1UECBMH
7+
VW5rbm93bjEQMA4GA1UEBxMHVW5rbm93bjETMBEGA1UEChMKU2VsZW5pdW1IUTEl
8+
MCMGA1UECxMcU29mdHdhcmUgRnJlZWRvbSBDb25zZXJ2YW5jeTETMBEGA1UEAxMK
9+
U2VsZW5pdW1IUTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKVTx0e5
10+
6/75QgE5E6rTYPlTkIxDjZylOMT2YBNuB8vIFZkSaCtLEqto0XTVV6dQc8Ge41QV
11+
rkt7DID1oN40rvWZdla9/2bVhCsWsRiXlvrKDbjoUi5kiLcfKJW+erUWs28xnLOw
12+
bvGNLLAjEUitKKGpR1vsSMOuvMN9VnsSkn9smAHLT2y41CjKpvdkq+OCUdnqfYju
13+
vV6OthRPXFMsDb1fYqZfE7fZhLc806Rg31qLssNVPwxt6VeNYi1/e5cWYeKIJQoj
14+
sFkqIdvu7xHtR7Qu1tNdeQoiDhMS7VLdZDsnAAtQLHvyAVEBicBX95VrGnOTlKdk
15+
+UDwyOP6djCISzUCAwEAAaNxMG8wHQYDVR0OBBYEFNrLCgZ7d2vfurWaJ4wa8O/T
16+
PfXPME4GA1UdEQEB/wREMEKCCWxvY2FsaG9zdIITc2VsZW5pdW0tZ3JpZC5sb2Nh
17+
bIISc2VsZW5pdW0tZ3JpZC5wcm9kggxzZWxlbml1bS5kZXYwDQYJKoZIhvcNAQEL
18+
BQADggEBABtxoPrVrPO5ELzUuSXbvYKHQG9YEuoAisXsiOWmldXRRvT/yTr3nzJn
19+
bC4dJywMW5unPdq1NoMxza0AF0KBFp1GzLDW5/KcA26R4IQi2xfQKVyRzb4vu0CY
20+
BDbnzF7Bisj50sSI4WThtF4xLEHVmzJ2GWNp6SgAScIrdGeB320aTqUIDO8YHH+y
21+
oeSu6qQfEcDiBWh3KD85vCIx0+L4AM3WKkP5nDq2FL6nwCdxqV7bo5/BZJEODMiW
22+
xv/hw0r1OBn2T2Z6o3pRI92zu9sjj6PzPP80DUPl7+fqAaRlLFglXd8b+Qxojm9o
23+
B0QN+gEM717L6WqmJGr1VC6HWQCvcCc=
24+
-----END CERTIFICATE-----
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDi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/lYlLkw6qM9i8vIKCfTpfi4vh4qfjQUczdy1w2zFbxriC2+uxhEqDN2tud3/P
18+
0CYrO0SGQKYCROrYUh0Pl1MswBeu8yT5AdrIBK3t92wfYqTWK7VUZQaUQ7YJWfXS
19+
usbz5qIzCQKBgH8ICHt+/gxUOtqjWYu0pPFyATWp2n1EWO13PyHrnHU0BDaFXQE9
20+
JuSdoOG3V6R8Y7Lul14n49etllCc2Hgd7ozmxn/AKVm5+M+oUYSXjI+qQANEJLHe
21+
410Y60EtcDnGen1gBWtog57KpzJkeIf3fGvaUkGkYoMFa6/yL3N7u2YNAoGADH29
22+
WKAKpasDvRVYrenf9D9ixKSTn+pXKesB/WZXZMzqwA7cf+90P8yplXn5HjXfmTot
23+
yV9uWY41F/TDGuX13DRvrzVTyvsDGFs7j8WrP1pGL5GQ/XvgnZnE8vyMzXbJqVEA
24+
ic0cDIHuyd9cPPrcLt7d3ZbE5ris7APtV/5d/hkCgYAMFCYoKcCh+9/2HOgwQ1b6
25+
16CS71TvDBCx7+D1V3WXrIOWkNzW2SIZtnhQwglU9L7PFw6ViJAY4sB2p9hDDtcZ
26+
e7Lotmnbrb75QQpWUyaoZMsw8l23MOGPzHKPqNiT57uOorjcFrePi9EOdERSG9+4
27+
lRKqCFhaNBUwQ4idzO0rWA==
28+
-----END PRIVATE KEY-----

examples/python/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pytest
33
trio
44
pytest-trio
55
flake8
6+
requests

0 commit comments

Comments
 (0)