Skip to content

Commit 2cdc830

Browse files
MustafaAgamypujagani
authored andcommitted
[Java] Add Locale.ROOT to avoid port formatting issues for all drivers (SeleniumHQ#15121)
Co-authored-by: Puja Jagani <[email protected]>
1 parent 85b5e27 commit 2cdc830

File tree

10 files changed

+80
-105
lines changed

10 files changed

+80
-105
lines changed

java/src/org/openqa/selenium/chrome/ChromeDriverService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ protected void loadSystemProperties() {
284284
@Override
285285
protected List<String> createArgs() {
286286
List<String> args = new ArrayList<>();
287-
args.add(String.format("--port=%d", getPort()));
287+
args.add(String.format(Locale.ROOT, "--port=%d", getPort()));
288288

289289
// Readable timestamp and append logs only work if log path is specified in args
290290
// Cannot use logOutput because goog:loggingPrefs requires --log-path get sent

java/src/org/openqa/selenium/edge/EdgeDriverService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected void loadSystemProperties() {
278278
@Override
279279
protected List<String> createArgs() {
280280
List<String> args = new ArrayList<>();
281-
args.add(String.format("--port=%d", getPort()));
281+
args.add(String.format(Locale.ROOT, "--port=%d", getPort()));
282282

283283
// Readable timestamp and append logs only work if log path is specified in args
284284
// Cannot use logOutput because goog:loggingPrefs requires --log-path get sent

java/src/org/openqa/selenium/firefox/GeckoDriverService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131
import java.util.HashMap;
3232
import java.util.List;
33+
import java.util.Locale;
3334
import java.util.Map;
3435
import org.openqa.selenium.Capabilities;
3536
import org.openqa.selenium.WebDriverException;
@@ -219,7 +220,7 @@ protected void loadSystemProperties() {
219220
@Override
220221
protected List<String> createArgs() {
221222
List<String> args = new ArrayList<>();
222-
args.add(String.format("--port=%d", getPort()));
223+
args.add(String.format(Locale.ROOT, "--port=%d", getPort()));
223224

224225
int wsPort = PortProber.findFreePort();
225226
args.add(String.format("--websocket-port=%d", wsPort));

java/src/org/openqa/selenium/ie/InternetExplorerDriverService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.ArrayList;
2929
import java.util.HashMap;
3030
import java.util.List;
31+
import java.util.Locale;
3132
import java.util.Map;
3233
import org.openqa.selenium.Capabilities;
3334
import org.openqa.selenium.WebDriverException;
@@ -217,7 +218,7 @@ protected void loadSystemProperties() {
217218
@Override
218219
protected List<String> createArgs() {
219220
List<String> args = new ArrayList<>();
220-
args.add(String.format("--port=%d", getPort()));
221+
args.add(String.format(Locale.ROOT, "--port=%d", getPort()));
221222

222223
if (logLevel != null) {
223224
args.add(String.format("--log-level=%s", logLevel));

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected Map<String, String> getEnvironment() {
138138
}
139139

140140
protected URL getUrl(int port) throws IOException {
141-
return new URL(String.format("http://localhost:%d", port));
141+
return new URL(String.format(Locale.ROOT, "http://localhost:%d", port));
142142
}
143143

144144
protected Capabilities getDefaultDriverOptions() {
@@ -505,17 +505,6 @@ public DS build() {
505505
port = PortProber.findFreePort();
506506
}
507507

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-
+ String.format("--port=%d", port)
513-
+ "\", please make sure to add the required arguments \"-Duser.language=en"
514-
+ " -Duser.region=US\" to your JVM, for more info please visit :\n"
515-
+ " https://www.selenium.dev/documentation/webdriver/browsers/",
516-
getPort()));
517-
}
518-
519508
if (timeout == null) {
520509
timeout = getDefaultTimeout();
521510
}

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

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

205205
@Test
206206
@NoDriverBeforeTest
207-
void shouldThrowNumberFormatException() {
208-
Locale arabicLocale = new Locale("ar", "EG");
209-
Locale.setDefault(arabicLocale);
210-
211-
int port = PortProber.findFreePort();
212-
ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
213-
builder.usingPort(port);
214-
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);
207+
void shouldLaunchSuccessfullyWithArabicDate() {
208+
try {
209+
Locale arabicLocale = new Locale("ar", "EG");
210+
Locale.setDefault(arabicLocale);
211+
212+
int port = PortProber.findFreePort();
213+
ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
214+
builder.usingPort(port);
215+
builder.build();
216+
217+
} catch (Exception e) {
218+
throw e;
219+
} finally {
220+
Locale.setDefault(Locale.US);
221+
}
225222
}
226223
}

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,20 @@ void canExecuteCdpCommands() {
198198

199199
@Test
200200
@NoDriverBeforeTest
201-
void shouldThrowNumberFormatException() {
202-
Locale arabicLocale = new Locale("ar", "EG");
203-
Locale.setDefault(arabicLocale);
204-
205-
int port = PortProber.findFreePort();
206-
EdgeDriverService.Builder builder = new EdgeDriverService.Builder();
207-
builder.usingPort(port);
208-
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/");
217-
218-
Locale.setDefault(Locale.US);
201+
void shouldLaunchSuccessfullyWithArabicDate() {
202+
try {
203+
Locale arabicLocale = new Locale("ar", "EG");
204+
Locale.setDefault(arabicLocale);
205+
206+
int port = PortProber.findFreePort();
207+
EdgeDriverService.Builder builder = new EdgeDriverService.Builder();
208+
builder.usingPort(port);
209+
builder.build();
210+
211+
} catch (Exception e) {
212+
throw e;
213+
} finally {
214+
Locale.setDefault(Locale.US);
215+
}
219216
}
220217
}

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
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;
2221
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2322
import static org.junit.jupiter.api.Assertions.fail;
2423
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -273,24 +272,21 @@ void canSetContext() {
273272

274273
@Test
275274
@NoDriverBeforeTest
276-
void shouldThrowNumberFormatException() {
277-
Locale arabicLocale = new Locale("ar", "EG");
278-
Locale.setDefault(arabicLocale);
279-
280-
int port = PortProber.findFreePort();
281-
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
282-
builder.usingPort(port);
283-
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);
275+
void shouldLaunchSuccessfullyWithArabicDate() {
276+
try {
277+
Locale arabicLocale = new Locale("ar", "EG");
278+
Locale.setDefault(arabicLocale);
279+
280+
int port = PortProber.findFreePort();
281+
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
282+
builder.usingPort(port);
283+
builder.build();
284+
285+
} catch (Exception e) {
286+
throw e;
287+
} finally {
288+
Locale.setDefault(Locale.US);
289+
}
294290
}
295291

296292
private static class CustomFirefoxProfile extends FirefoxProfile {}

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,21 @@ void testPersistentHoverCanBeTurnedOff() throws Exception {
147147

148148
@Test
149149
@NoDriverBeforeTest
150-
void shouldThrowNumberFormatException() {
151-
Locale arabicLocale = new Locale("ar", "EG");
152-
Locale.setDefault(arabicLocale);
150+
void shouldLaunchSuccessfullyWithArabicDate() {
151+
try {
152+
Locale arabicLocale = new Locale("ar", "EG");
153+
Locale.setDefault(arabicLocale);
153154

154-
int port = PortProber.findFreePort();
155-
InternetExplorerDriverService.Builder builder = new InternetExplorerDriverService.Builder();
156-
builder.usingPort(port);
155+
int port = PortProber.findFreePort();
156+
InternetExplorerDriverService.Builder builder = new InternetExplorerDriverService.Builder();
157+
builder.usingPort(port);
158+
builder.build();
157159

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);
160+
} catch (Exception e) {
161+
throw e;
162+
} finally {
163+
Locale.setDefault(Locale.US);
164+
}
168165
}
169166

170167
private WebDriver newIeDriver() {

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,20 @@ public void canAttachDebugger() {
137137

138138
@Test
139139
@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);
140+
void shouldLaunchSuccessfullyWithArabicDate() {
141+
try {
142+
Locale arabicLocale = new Locale("ar", "EG");
143+
Locale.setDefault(arabicLocale);
144+
145+
int port = PortProber.findFreePort();
146+
SafariDriverService.Builder builder = new SafariDriverService.Builder();
147+
builder.usingPort(port);
148+
builder.build();
149+
150+
} catch (Exception e) {
151+
throw e;
152+
} finally {
153+
Locale.setDefault(Locale.US);
154+
}
158155
}
159156
}

0 commit comments

Comments
 (0)