Skip to content

Commit fad6fbe

Browse files
authored
[java] Add NumberFormatException when the port isn't correctly formatted for Firefox, Edge and Chrome Drivers (#14946)
1 parent 9cd397c commit fad6fbe

File tree

6 files changed

+92
-18
lines changed

6 files changed

+92
-18
lines changed

java/src/org/openqa/selenium/remote/service/DriverService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URL;
3232
import java.time.Duration;
3333
import java.util.List;
34+
import java.util.Locale;
3435
import java.util.Map;
3536
import java.util.concurrent.CompletableFuture;
3637
import java.util.concurrent.ExecutionException;
@@ -504,6 +505,17 @@ public DS build() {
504505
port = PortProber.findFreePort();
505506
}
506507

508+
if (Locale.getDefault(Locale.Category.FORMAT).getLanguage().equals("ar")) {
509+
throw new NumberFormatException(
510+
String.format(
511+
"Couldn't format the port numbers because the System Language is arabic:"
512+
+ " \"--port=%d\", please make sure to add the required arguments"
513+
+ " \"-Duser.language=en -Duser.region=US\" to your JVM, for more info please"
514+
+ " visit :\n"
515+
+ " https://www.selenium.dev/documentation/webdriver/browsers/",
516+
getPort()));
517+
}
518+
507519
if (timeout == null) {
508520
timeout = getDefaultTimeout();
509521
}

java/test/org/openqa/selenium/chrome/ChromeDriverFunctionalTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,23 @@ void canExecuteCdpCommands() {
204204

205205
@Test
206206
@NoDriverBeforeTest
207-
void shouldLaunchSuccessfullyWithArabicDate() {
207+
void shouldThrowNumberFormatException() {
208208
Locale arabicLocale = new Locale("ar", "EG");
209209
Locale.setDefault(arabicLocale);
210-
Locale.setDefault(Locale.US);
211210

212211
int port = PortProber.findFreePort();
213212
ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
214213
builder.usingPort(port);
215-
ChromeDriverService service = builder.build();
216214

217-
driver = new ChromeDriver(service, (ChromeOptions) CHROME.getCapabilities());
218-
driver.get(pages.simpleTestPage);
219-
assertThat(driver.getTitle()).isEqualTo("Hello WebDriver");
215+
assertThatExceptionOfType(NumberFormatException.class)
216+
.isThrownBy(builder::build)
217+
.withMessage(
218+
"Couldn't format the port numbers because the System Language is arabic: \""
219+
+ String.format("--port=%d", port)
220+
+ "\", please make sure to add the required arguments \"-Duser.language=en"
221+
+ " -Duser.region=US\" to your JVM, for more info please visit :\n"
222+
+ " https://www.selenium.dev/documentation/webdriver/browsers/");
223+
224+
Locale.setDefault(Locale.US);
220225
}
221226
}

java/test/org/openqa/selenium/edge/EdgeDriverFunctionalTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,23 @@ void canExecuteCdpCommands() {
198198

199199
@Test
200200
@NoDriverBeforeTest
201-
void shouldLaunchSuccessfullyWithArabicDate() {
201+
void shouldThrowNumberFormatException() {
202202
Locale arabicLocale = new Locale("ar", "EG");
203203
Locale.setDefault(arabicLocale);
204-
Locale.setDefault(Locale.US);
205204

206205
int port = PortProber.findFreePort();
207206
EdgeDriverService.Builder builder = new EdgeDriverService.Builder();
208207
builder.usingPort(port);
209-
EdgeDriverService service = builder.build();
210208

211-
driver = new EdgeDriver(service, (EdgeOptions) EDGE.getCapabilities());
209+
assertThatExceptionOfType(NumberFormatException.class)
210+
.isThrownBy(builder::build)
211+
.withMessage(
212+
"Couldn't format the port numbers because the System Language is arabic: \""
213+
+ String.format("--port=%d", port)
214+
+ "\", please make sure to add the required arguments \"-Duser.language=en"
215+
+ " -Duser.region=US\" to your JVM, for more info please visit :\n"
216+
+ " https://www.selenium.dev/documentation/webdriver/browsers/");
212217

213-
driver.get(pages.simpleTestPage);
214-
assertThat(driver.getTitle()).isEqualTo("Hello WebDriver");
218+
Locale.setDefault(Locale.US);
215219
}
216220
}

java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.firefox;
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
2122
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2223
import static org.junit.jupiter.api.Assertions.fail;
2324
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -272,19 +273,24 @@ void canSetContext() {
272273

273274
@Test
274275
@NoDriverBeforeTest
275-
void shouldLaunchSuccessfullyWithArabicDate() {
276+
void shouldThrowNumberFormatException() {
276277
Locale arabicLocale = new Locale("ar", "EG");
277278
Locale.setDefault(arabicLocale);
278-
Locale.setDefault(Locale.US);
279279

280280
int port = PortProber.findFreePort();
281281
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
282282
builder.usingPort(port);
283-
GeckoDriverService service = builder.build();
284283

285-
driver = new FirefoxDriver(service, (FirefoxOptions) FIREFOX.getCapabilities());
286-
driver.get(pages.simpleTestPage);
287-
assertThat(driver.getTitle()).isEqualTo("Hello WebDriver");
284+
assertThatExceptionOfType(NumberFormatException.class)
285+
.isThrownBy(builder::build)
286+
.withMessage(
287+
"Couldn't format the port numbers because the System Language is arabic: \""
288+
+ String.format("--port=%d", port)
289+
+ "\", please make sure to add the required arguments \"-Duser.language=en"
290+
+ " -Duser.region=US\" to your JVM, for more info please visit :\n"
291+
+ " https://www.selenium.dev/documentation/webdriver/browsers/");
292+
293+
Locale.setDefault(Locale.US);
288294
}
289295

290296
private static class CustomFirefoxProfile extends FirefoxProfile {}

java/test/org/openqa/selenium/ie/InternetExplorerDriverTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.awt.*;
2626
import java.time.Duration;
27+
import java.util.Locale;
2728
import org.junit.jupiter.api.Test;
2829
import org.openqa.selenium.By;
2930
import org.openqa.selenium.Capabilities;
@@ -33,6 +34,7 @@
3334
import org.openqa.selenium.WebDriver;
3435
import org.openqa.selenium.WebElement;
3536
import org.openqa.selenium.interactions.Actions;
37+
import org.openqa.selenium.net.PortProber;
3638
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
3739
import org.openqa.selenium.remote.http.ClientConfig;
3840
import org.openqa.selenium.testing.JupiterTestBase;
@@ -143,6 +145,28 @@ void testPersistentHoverCanBeTurnedOff() throws Exception {
143145
assertThat(item.getText()).isEmpty();
144146
}
145147

148+
@Test
149+
@NoDriverBeforeTest
150+
void shouldThrowNumberFormatException() {
151+
Locale arabicLocale = new Locale("ar", "EG");
152+
Locale.setDefault(arabicLocale);
153+
154+
int port = PortProber.findFreePort();
155+
InternetExplorerDriverService.Builder builder = new InternetExplorerDriverService.Builder();
156+
builder.usingPort(port);
157+
158+
assertThatExceptionOfType(NumberFormatException.class)
159+
.isThrownBy(builder::build)
160+
.withMessage(
161+
"Couldn't format the port numbers because the System Language is arabic: \""
162+
+ String.format("--port=%d", port)
163+
+ "\", please make sure to add the required arguments \"-Duser.language=en"
164+
+ " -Duser.region=US\" to your JVM, for more info please visit :\n"
165+
+ " https://www.selenium.dev/documentation/webdriver/browsers/");
166+
167+
Locale.setDefault(Locale.US);
168+
}
169+
146170
private WebDriver newIeDriver() {
147171
return new WebDriverBuilder().get();
148172
}

java/test/org/openqa/selenium/safari/SafariDriverTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.file.Path;
2828
import java.nio.file.Paths;
2929
import java.time.Duration;
30+
import java.util.Locale;
3031
import org.junit.jupiter.api.Test;
3132
import org.openqa.selenium.Capabilities;
3233
import org.openqa.selenium.SessionNotCreatedException;
@@ -133,4 +134,26 @@ public void canAttachDebugger() {
133134
localDriver = new WebDriverBuilder().get(new SafariOptions());
134135
((HasDebugger) localDriver).attachDebugger();
135136
}
137+
138+
@Test
139+
@NoDriverBeforeTest
140+
void shouldThrowNumberFormatException() {
141+
Locale arabicLocale = new Locale("ar", "EG");
142+
Locale.setDefault(arabicLocale);
143+
144+
int port = PortProber.findFreePort();
145+
SafariDriverService.Builder builder = new SafariDriverService.Builder();
146+
builder.usingPort(port);
147+
148+
assertThatExceptionOfType(NumberFormatException.class)
149+
.isThrownBy(builder::build)
150+
.withMessage(
151+
"Couldn't format the port numbers because the System Language is arabic: \""
152+
+ String.format("--port=%d", port)
153+
+ "\", please make sure to add the required arguments \"-Duser.language=en"
154+
+ " -Duser.region=US\" to your JVM, for more info please visit :\n"
155+
+ " https://www.selenium.dev/documentation/webdriver/browsers/");
156+
157+
Locale.setDefault(Locale.US);
158+
}
136159
}

0 commit comments

Comments
 (0)