From fb094aff69be4bfdcd9983c38e0b6e887fad1f5a Mon Sep 17 00:00:00 2001 From: Alex Popov Date: Wed, 9 Jul 2025 00:36:26 +0700 Subject: [PATCH 1/2] Refactor multiline strings for improved readability in Java files --- .../selenium/devtools/CdpClientGenerator.java | 11 ++++---- java/src/org/openqa/selenium/grid/Main.java | 9 ++++-- .../selenium/grid/node/config/NodeFlags.java | 15 +++++----- .../selenium/grid/node/relay/RelayFlags.java | 9 +++--- .../jdbc/JdbcSessionMapOptions.java | 8 ++++-- .../org/openqa/selenium/remote/Augmenter.java | 19 +++++++------ .../remote/codec/w3c/W3CHttpCommandCodec.java | 27 ++++++++++-------- .../codec/w3c/W3CHttpResponseCodecTest.java | 28 ++++++++++--------- 8 files changed, 71 insertions(+), 55 deletions(-) diff --git a/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java b/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java index ad43ee1d5a91c..a6a7a685210da 100644 --- a/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java +++ b/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java @@ -1021,11 +1021,12 @@ public TypeDeclaration toTypeDeclaration() { .get() .addStatement( String.format( - "return java.util.Arrays.stream(%s.values())\n" - + ".filter(rs -> rs.value.equalsIgnoreCase(s))\n" - + ".findFirst()\n" - + ".orElseThrow(() -> new org.openqa.selenium.devtools.DevToolsException(\n" - + "\"Given value \" + s + \" is not found within %s \"));", + """ + return java.util.Arrays.stream(%s.values()) + .filter(rs -> rs.value.equalsIgnoreCase(s)) + .findFirst() + .orElseThrow(() -> new org.openqa.selenium.devtools.DevToolsException( + "Given value " + s + " is not found within %s "));""", name, name)); enumDecl diff --git a/java/src/org/openqa/selenium/grid/Main.java b/java/src/org/openqa/selenium/grid/Main.java index 8920af7b5bad0..b0d96fdb2e02a 100644 --- a/java/src/org/openqa/selenium/grid/Main.java +++ b/java/src/org/openqa/selenium/grid/Main.java @@ -143,9 +143,12 @@ public Executable configure(PrintStream out, PrintStream err, String... args) { outWriter.write("\nFor each command, run with `--help` for command-specific help\n"); outWriter.write( - "\nUse the `--ext` flag before the command name to specify an additional " - + "classpath to use with the server (for example, to provide additional " - + "commands, or to provide additional driver implementations). For example:\n"); + """ + + Use the `--ext` flag before the command name to specify an additional \ + classpath to use with the server (for example, to provide additional \ + commands, or to provide additional driver implementations). For example: + """); outWriter.write( String.format( "%n java -jar selenium.jar --ext example.jar%sdir standalone --port 1234", diff --git a/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java b/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java index dd90f080d8f6c..df33bf982e09f 100644 --- a/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java +++ b/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java @@ -173,13 +173,14 @@ public class NodeFlags implements HasRoles { name = "driver-configuration", prefixed = true, example = - "\n" - + "display-name = \"Firefox Nightly\"\n" - + "webdriver-executable = \"/usr/local/bin/geckodriver\"\n" - + "max-sessions = 2\n" - + "stereotype = \"{\\\"browserName\\\": \\\"firefox\\\", \\\"browserVersion\\\":" - + " \\\"86\\\", \\\"moz:firefoxOptions\\\": {\\\"binary\\\":\\\"/Applications/Firefox" - + " Nightly.app/Contents/MacOS/firefox\\\"}}\"") + """ + + display-name = "Firefox Nightly" + webdriver-executable = "/usr/local/bin/geckodriver" + max-sessions = 2 + stereotype = "{\\"browserName\\": \\"firefox\\", \\"browserVersion\\":\ + \\"86\\", \\"moz:firefoxOptions\\": {\\"binary\\":\\"/Applications/Firefox\ + Nightly.app/Contents/MacOS/firefox\\"}}\"""") public List driverConfiguration; @Parameter( diff --git a/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java b/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java index 70450c777e1b3..42becdcef62c5 100644 --- a/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java +++ b/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java @@ -51,10 +51,11 @@ public class RelayFlags implements HasRoles { name = "configs", prefixed = true, example = - "\n" - + "max-sessions = 2\n" - + "stereotype = \"{\\\"browserName\\\": \\\"safari\\\", \\\"platformName\\\":" - + " \\\"iOS\\\", \\\"appium:platformVersion\\\": \\\"14.5\\\" }}\"") + """ + + max-sessions = 2 + stereotype = "{\\"browserName\\": \\"safari\\", \\"platformName\\":\ + \\"iOS\\", \\"appium:platformVersion\\": \\"14.5\\" }}\"""") public List driverConfiguration; @Parameter( diff --git a/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java b/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java index 84252ddf9e090..aa43b13542fc7 100644 --- a/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java +++ b/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java @@ -46,8 +46,12 @@ public JdbcSessionMapOptions(Config config) { } } catch (NoSuchElementException e) { throw new JdbcException( - "Missing session options. Check and add all the following options \n " - + "--jdbc-url \n --jdbc-user \n --jdbc-password "); + """ + Missing session options. Check and add all the following options\s + \ + --jdbc-url \s + --jdbc-user \s + --jdbc-password """); } } diff --git a/java/src/org/openqa/selenium/remote/Augmenter.java b/java/src/org/openqa/selenium/remote/Augmenter.java index f24daf1caa32b..b9d1f6bcc471e 100644 --- a/java/src/org/openqa/selenium/remote/Augmenter.java +++ b/java/src/org/openqa/selenium/remote/Augmenter.java @@ -147,15 +147,16 @@ public WebDriver augment(WebDriver driver) { if (driver instanceof Decorated) { LOG.warning( - "Warning: In future versions, passing a decorated driver will no longer be allowed.\n" - + " Instead, augment the driver first and then use it to created a decorated" - + " driver.\n" - + " Explanation: Decorated drivers are not aware of the augmentations applied to" - + " them. It can lead to expected behavior.\n" - + " For example, augmenting HasDevTools interface to a decorated driver. \n" - + " The decorated driver is not aware that after augmentation it is an instance of" - + " HasDevTools. So it does not invoke the close() method of the underlying" - + " websocket, potentially causing a memory leak. "); + """ + Warning: In future versions, passing a decorated driver will no longer be allowed. + Instead, augment the driver first and then use it to created a decorated\ + driver. + Explanation: Decorated drivers are not aware of the augmentations applied to\ + them. It can lead to expected behavior. + For example, augmenting HasDevTools interface to a decorated driver.\s + The decorated driver is not aware that after augmentation it is an instance of\ + HasDevTools. So it does not invoke the close() method of the underlying\ + websocket, potentially causing a memory leak.\s"""); } Capabilities caps = ImmutableCapabilities.copyOf(((HasCapabilities) driver).getCapabilities()); diff --git a/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java b/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java index 970cd2035c6e4..5fb62c8e62ffb 100644 --- a/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java +++ b/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java @@ -214,9 +214,10 @@ public W3CHttpCommandCodec() { case GET_PAGE_SOURCE: return toScript( - "var source = document.documentElement.outerHTML; \n" - + "if (!source) { source = new XMLSerializer().serializeToString(document); }\n" - + "return source;"); + """ + var source = document.documentElement.outerHTML;\s + if (!source) { source = new XMLSerializer().serializeToString(document); } + return source;"""); case CLEAR_LOCAL_STORAGE: return toScript("localStorage.clear()"); @@ -327,15 +328,17 @@ public W3CHttpCommandCodec() { } case SUBMIT_ELEMENT: return toScript( - "/* submitForm */var form = arguments[0];\n" - + "while (form.nodeName != \"FORM\" && form.parentNode) {\n" - + " form = form.parentNode;\n" - + "}\n" - + "if (!form) { throw Error('Unable to find containing form element'); }\n" - + "if (!form.ownerDocument) { throw Error('Unable to find owning document'); }\n" - + "var e = form.ownerDocument.createEvent('Event');\n" - + "e.initEvent('submit', true, true);\n" - + "if (form.dispatchEvent(e)) { HTMLFormElement.prototype.submit.call(form) }\n", + """ + /* submitForm */var form = arguments[0]; + while (form.nodeName != "FORM" && form.parentNode) { + form = form.parentNode; + } + if (!form) { throw Error('Unable to find containing form element'); } + if (!form.ownerDocument) { throw Error('Unable to find owning document'); } + var e = form.ownerDocument.createEvent('Event'); + e.initEvent('submit', true, true); + if (form.dispatchEvent(e)) { HTMLFormElement.prototype.submit.call(form) } + """, asElement(parameters.get("id"))); default: diff --git a/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java b/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java index b7d1e846b1674..941a9f42f4620 100644 --- a/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java +++ b/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java @@ -60,12 +60,13 @@ void noErrorNoCry() { @Test void shouldBeAbleToHandleGatewayTimeoutError() { String responseString = - "\r\n" - + "\r\n" - + "

504 Gateway Time-out

\r\n" - + "The server didn't respond in time.\r\n" - + "\r\n" - + ""; + """ + \r + \r +

504 Gateway Time-out

\r + The server didn't respond in time.\r + \r + """; byte[] contents = responseString.getBytes(UTF_8); @@ -85,13 +86,14 @@ void shouldBeAbleToHandleGatewayTimeoutError() { @Test void shouldBeAbleToHandleBadGatewayError() { String responseString = - "\r\n" - + "502 Bad Gateway\r\n" - + "\r\n" - + "

502 Bad Gateway

\r\n" - + "
nginx
\r\n" - + "\r\n" - + ""; + """ + \r + 502 Bad Gateway\r + \r +

502 Bad Gateway

\r +
nginx
\r + \r + """; byte[] contents = responseString.getBytes(UTF_8); From b91fa906b5b4ecbd328c1afa2105a51ce39784a7 Mon Sep 17 00:00:00 2001 From: Alex Popov Date: Wed, 9 Jul 2025 00:40:31 +0700 Subject: [PATCH 2/2] Refactor multiline string formatting for consistency in Java files --- .../openqa/selenium/devtools/CdpClientGenerator.java | 5 +++-- java/src/org/openqa/selenium/grid/Main.java | 2 +- .../openqa/selenium/grid/node/config/NodeFlags.java | 5 +++-- .../openqa/selenium/grid/node/relay/RelayFlags.java | 5 +++-- .../grid/sessionmap/jdbc/JdbcSessionMapOptions.java | 5 +++-- java/src/org/openqa/selenium/remote/Augmenter.java | 5 +++-- .../selenium/remote/codec/w3c/W3CHttpCommandCodec.java | 7 ++++--- .../remote/codec/w3c/W3CHttpResponseCodecTest.java | 10 ++++++---- 8 files changed, 26 insertions(+), 18 deletions(-) diff --git a/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java b/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java index a6a7a685210da..836c6fcc1405d 100644 --- a/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java +++ b/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java @@ -1021,12 +1021,13 @@ public TypeDeclaration toTypeDeclaration() { .get() .addStatement( String.format( - """ + """ return java.util.Arrays.stream(%s.values()) .filter(rs -> rs.value.equalsIgnoreCase(s)) .findFirst() .orElseThrow(() -> new org.openqa.selenium.devtools.DevToolsException( - "Given value " + s + " is not found within %s "));""", + "Given value " + s + " is not found within %s "));\ + """, name, name)); enumDecl diff --git a/java/src/org/openqa/selenium/grid/Main.java b/java/src/org/openqa/selenium/grid/Main.java index b0d96fdb2e02a..248e6b3f63982 100644 --- a/java/src/org/openqa/selenium/grid/Main.java +++ b/java/src/org/openqa/selenium/grid/Main.java @@ -143,7 +143,7 @@ public Executable configure(PrintStream out, PrintStream err, String... args) { outWriter.write("\nFor each command, run with `--help` for command-specific help\n"); outWriter.write( - """ + """ Use the `--ext` flag before the command name to specify an additional \ classpath to use with the server (for example, to provide additional \ diff --git a/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java b/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java index df33bf982e09f..130fdc1ccb6b2 100644 --- a/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java +++ b/java/src/org/openqa/selenium/grid/node/config/NodeFlags.java @@ -173,14 +173,15 @@ public class NodeFlags implements HasRoles { name = "driver-configuration", prefixed = true, example = - """ + """ display-name = "Firefox Nightly" webdriver-executable = "/usr/local/bin/geckodriver" max-sessions = 2 stereotype = "{\\"browserName\\": \\"firefox\\", \\"browserVersion\\":\ \\"86\\", \\"moz:firefoxOptions\\": {\\"binary\\":\\"/Applications/Firefox\ - Nightly.app/Contents/MacOS/firefox\\"}}\"""") + Nightly.app/Contents/MacOS/firefox\\"}}\"\ + """) public List driverConfiguration; @Parameter( diff --git a/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java b/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java index 42becdcef62c5..526252a5f2e73 100644 --- a/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java +++ b/java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java @@ -51,11 +51,12 @@ public class RelayFlags implements HasRoles { name = "configs", prefixed = true, example = - """ + """ max-sessions = 2 stereotype = "{\\"browserName\\": \\"safari\\", \\"platformName\\":\ - \\"iOS\\", \\"appium:platformVersion\\": \\"14.5\\" }}\"""") + \\"iOS\\", \\"appium:platformVersion\\": \\"14.5\\" }}\"\ + """) public List driverConfiguration; @Parameter( diff --git a/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java b/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java index aa43b13542fc7..4d381523a93db 100644 --- a/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java +++ b/java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcSessionMapOptions.java @@ -46,12 +46,13 @@ public JdbcSessionMapOptions(Config config) { } } catch (NoSuchElementException e) { throw new JdbcException( - """ + """ Missing session options. Check and add all the following options\s \ --jdbc-url \s --jdbc-user \s - --jdbc-password """); + --jdbc-password \ + """); } } diff --git a/java/src/org/openqa/selenium/remote/Augmenter.java b/java/src/org/openqa/selenium/remote/Augmenter.java index b9d1f6bcc471e..8b3ad719e8163 100644 --- a/java/src/org/openqa/selenium/remote/Augmenter.java +++ b/java/src/org/openqa/selenium/remote/Augmenter.java @@ -147,7 +147,7 @@ public WebDriver augment(WebDriver driver) { if (driver instanceof Decorated) { LOG.warning( - """ + """ Warning: In future versions, passing a decorated driver will no longer be allowed. Instead, augment the driver first and then use it to created a decorated\ driver. @@ -156,7 +156,8 @@ public WebDriver augment(WebDriver driver) { For example, augmenting HasDevTools interface to a decorated driver.\s The decorated driver is not aware that after augmentation it is an instance of\ HasDevTools. So it does not invoke the close() method of the underlying\ - websocket, potentially causing a memory leak.\s"""); + websocket, potentially causing a memory leak.\s\ + """); } Capabilities caps = ImmutableCapabilities.copyOf(((HasCapabilities) driver).getCapabilities()); diff --git a/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java b/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java index 5fb62c8e62ffb..ff1ee2e4ad2fe 100644 --- a/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java +++ b/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java @@ -214,10 +214,11 @@ public W3CHttpCommandCodec() { case GET_PAGE_SOURCE: return toScript( - """ + """ var source = document.documentElement.outerHTML;\s if (!source) { source = new XMLSerializer().serializeToString(document); } - return source;"""); + return source;\ + """); case CLEAR_LOCAL_STORAGE: return toScript("localStorage.clear()"); @@ -328,7 +329,7 @@ public W3CHttpCommandCodec() { } case SUBMIT_ELEMENT: return toScript( - """ + """ /* submitForm */var form = arguments[0]; while (form.nodeName != "FORM" && form.parentNode) { form = form.parentNode; diff --git a/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java b/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java index 941a9f42f4620..ca8f660eba7e4 100644 --- a/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java +++ b/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java @@ -60,13 +60,14 @@ void noErrorNoCry() { @Test void shouldBeAbleToHandleGatewayTimeoutError() { String responseString = - """ + """ \r \r

504 Gateway Time-out

\r The server didn't respond in time.\r \r - """; + \ + """; byte[] contents = responseString.getBytes(UTF_8); @@ -86,14 +87,15 @@ void shouldBeAbleToHandleGatewayTimeoutError() { @Test void shouldBeAbleToHandleBadGatewayError() { String responseString = - """ + """ \r 502 Bad Gateway\r \r

502 Bad Gateway

\r
nginx
\r \r - """; + \ + """; byte[] contents = responseString.getBytes(UTF_8);