Skip to content

Commit c84a764

Browse files
authored
Merge branch 'trunk' into petesong/add-practice-example
2 parents 3200342 + 88c7b5c commit c84a764

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1385
-1320
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/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public async Task PerformanceMetrics()
111111
var session = ((IDevTools)driver).GetDevToolsSession();
112112
var domains = session.GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V130.DevToolsSessionDomains>();
113113

114-
await domains.Performance.Enable(new OpenQA.Selenium.DevTools.V13.Performance.EnableCommandSettings());
114+
await domains.Performance.Enable(new OpenQA.Selenium.DevTools.V130.Performance.EnableCommandSettings());
115115
var metricsResponse =
116116
await session.SendCommand<GetMetricsCommandSettings, GetMetricsCommandResponse>(
117117
new GetMetricsCommandSettings()
Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,101 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
using System;
119
using Microsoft.VisualStudio.TestTools.UnitTesting;
20+
using OpenQA.Selenium;
21+
using OpenQA.Selenium.Chrome;
22+
23+
namespace SeleniumDocs.Interactions{
24+
25+
[TestClass]
26+
public class CookiesTest{
27+
28+
WebDriver driver = new ChromeDriver();
29+
30+
[TestMethod]
31+
public void addCookie(){
32+
driver.Url="https://www.selenium.dev/selenium/web/blank.html";
33+
// Add cookie into current browser context
34+
driver.Manage().Cookies.AddCookie(new Cookie("key", "value"));
35+
driver.Quit();
36+
}
37+
38+
[TestMethod]
39+
public void getNamedCookie(){
40+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
41+
// Add cookie into current browser context
42+
driver.Manage().Cookies.AddCookie(new Cookie("foo", "bar"));
43+
// Get cookie details with named cookie 'foo'
44+
Cookie cookie = driver.Manage().Cookies.GetCookieNamed("foo");
45+
Assert.AreEqual(cookie.Value, "bar");
46+
driver.Quit();
47+
}
48+
49+
[TestMethod]
50+
public void getAllCookies(){
51+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
52+
// Add cookies into current browser context
53+
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
54+
driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
55+
// Get cookies
56+
var cookies = driver.Manage().Cookies.AllCookies;
57+
foreach (var cookie in cookies){
58+
if (cookie.Name.Equals("test1")){
59+
Assert.AreEqual("cookie1", cookie.Value);
60+
}
61+
if (cookie.Name.Equals("test2")){
62+
Assert.AreEqual("cookie2", cookie.Value);
63+
}
64+
}
65+
driver.Quit();
66+
}
67+
68+
[TestMethod]
69+
public void deleteCookieNamed(){
70+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
71+
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
72+
// delete cookie named
73+
driver.Manage().Cookies.DeleteCookieNamed("test1");
74+
driver.Quit();
75+
}
276

3-
namespace SeleniumDocs.Interactions
4-
{
5-
[TestClass]
6-
public class CookiesTest : BaseTest
7-
{
77+
[TestMethod]
78+
public void deleteCookieObject(){
79+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
80+
Cookie cookie = new Cookie("test2", "cookie2");
81+
driver.Manage().Cookies.AddCookie(cookie);
82+
/*
83+
Selenium CSharp bindings also provides a way to delete
84+
cookie by passing cookie object of current browsing context
85+
*/
86+
driver.Manage().Cookies.DeleteCookie(cookie);
87+
driver.Quit();
88+
}
89+
90+
[TestMethod]
91+
public void deleteAllCookies(){
92+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
93+
// Add cookies into current browser context
94+
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
95+
driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
96+
// Delete All cookies
97+
driver.Manage().Cookies.DeleteAllCookies();
98+
driver.Quit();
99+
}
8100
}
9101
}

examples/dotnet/SeleniumDocs/SeleniumDocs.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.7.1" />
1111
<PackageReference Include="MSTest.TestAdapter" Version="3.6.0" />
1212
<PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
13-
<PackageReference Include="Selenium.Support" Version="4.25.0" />
14-
<PackageReference Include="Selenium.WebDriver" Version="4.25.0" />
13+
<PackageReference Include="Selenium.Support" Version="4.26.1" />
14+
<PackageReference Include="Selenium.WebDriver" Version="4.26.1" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

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
}

0 commit comments

Comments
 (0)