Skip to content

Commit ea3d5f0

Browse files
committed
Add examples for http_client java and python
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent e2defad commit ea3d5f0

File tree

13 files changed

+397
-7
lines changed

13 files changed

+397
-7
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"

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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,108 @@
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+
private SSLContext createSSLContextWithCA(String caCertPath) throws Exception {
74+
// Load the CA certificate
75+
FileInputStream fis = new FileInputStream(caCertPath);
76+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
77+
X509Certificate caCert = (X509Certificate) cf.generateCertificate(fis);
78+
// Create a KeyStore and load the CA certificate
79+
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
80+
keyStore.load(null, null);
81+
keyStore.setCertificateEntry("caCert", caCert);
82+
// Initialize a TrustManagerFactory with the KeyStore
83+
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
84+
tmf.init(keyStore);
85+
// Create and initialize an SSLContext with the TrustManagerFactory
86+
SSLContext sslContext = SSLContext.getInstance("TLS");
87+
sslContext.init(null, tmf.getTrustManagers(), null);
88+
return sslContext;
89+
}
90+
91+
public static SSLContext createIgnoreSSLContext() throws Exception {
92+
TrustManager[] trustAllCerts = new TrustManager[]{
93+
new X509TrustManager() {
94+
public X509Certificate[] getAcceptedIssuers() {
95+
return null;
96+
}
97+
98+
public void checkClientTrusted(X509Certificate[] certs, String authType) {
99+
}
6100

101+
public void checkServerTrusted(X509Certificate[] certs, String authType) {
102+
}
103+
}
104+
};
105+
SSLContext sslContext = SSLContext.getInstance("TLS");
106+
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
107+
return sslContext;
108+
}
7109
}
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 numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQClU8dHuev++UIB
3+
OROq02D5U5CMQ42cpTjE9mATbgfLyBWZEmgrSxKraNF01VenUHPBnuNUFa5LewyA
4+
9aDeNK71mXZWvf9m1YQrFrEYl5b6yg246FIuZIi3HyiVvnq1FrNvMZyzsG7xjSyw
5+
IxFIrSihqUdb7EjDrrzD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JLHe
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

examples/python/tests/conftest.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from selenium.webdriver.common.utils import free_port
88
from datetime import datetime
99
from urllib.request import urlopen
10+
import requests
11+
from requests.auth import HTTPBasicAuth
1012

1113
import pytest
1214
from selenium import webdriver
@@ -236,3 +238,88 @@ def wait_for_server(url, timeout=60):
236238
process.wait(timeout=10)
237239
except subprocess.TimeoutExpired:
238240
process.kill()
241+
242+
243+
def _get_resource_path(file_name: str):
244+
if os.path.abspath("").endswith("tests"):
245+
path = os.path.abspath(f"resources/{file_name}")
246+
else:
247+
path = os.path.join(
248+
os.path.dirname(
249+
os.path.dirname(
250+
os.path.abspath(__file__)
251+
)
252+
),
253+
f"tests/resources/{file_name}",
254+
)
255+
return path
256+
257+
258+
@pytest.fixture(scope="function")
259+
def grid_server():
260+
_host = "localhost"
261+
_port = free_port()
262+
_username = "admin"
263+
_password = "myStrongPassword"
264+
_path_cert = _get_resource_path("tls.crt")
265+
_path_key = _get_resource_path("tls.key")
266+
_path_jks = _get_resource_path("server.jks")
267+
_truststore_pass = "seleniumkeystore"
268+
_path = os.path.join(
269+
os.path.dirname(
270+
os.path.dirname(
271+
os.path.dirname(
272+
os.path.abspath(__file__)
273+
)
274+
)
275+
),
276+
"selenium-server-4.26.0.jar",
277+
)
278+
279+
args = [
280+
"java",
281+
f"-Djavax.net.ssl.trustStore={_path_jks}",
282+
f"-Djavax.net.ssl.trustStorePassword={_truststore_pass}",
283+
"-Djdk.internal.httpclient.disableHostnameVerification=true",
284+
"-jar",
285+
_path,
286+
"standalone",
287+
"--port",
288+
str(_port),
289+
"--selenium-manager",
290+
"true",
291+
"--enable-managed-downloads",
292+
"true",
293+
"--username",
294+
_username,
295+
"--password",
296+
_password,
297+
"--https-certificate",
298+
_path_cert,
299+
"--https-private-key",
300+
_path_key,
301+
]
302+
303+
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
304+
305+
def wait_for_server(url, timeout=60):
306+
start = time.time()
307+
while time.time() - start < timeout:
308+
try:
309+
requests.get(url, verify=_path_cert, auth=HTTPBasicAuth(_username, _password))
310+
return True
311+
except OSError as e:
312+
print(e)
313+
time.sleep(0.2)
314+
return False
315+
316+
if not wait_for_server(f"https://{_host}:{_port}/status"):
317+
raise RuntimeError(f"Selenium server did not start within the allotted time.")
318+
319+
yield f"https://{_host}:{_port}"
320+
321+
process.terminate()
322+
try:
323+
process.wait(timeout=10)
324+
except subprocess.TimeoutExpired:
325+
process.kill()

0 commit comments

Comments
 (0)