Skip to content

Commit 3ce6661

Browse files
authored
[java] Allow downloading files from old Grid server (#16839)
allow downloading files from old Grid server Endpoint "/se/files/:name" was added in Selenium 4.39.0. If user has upgraded only Selenium client version (but not the server!) then method `remoteDriver.downloadFile()` fails with "unsupported command" exception. Now the download method will work again, falling back to the old endpoint `POST /se/files` (which is slow for large files). See issue #16612 and PR #16627.
1 parent 309fadd commit 3ce6661

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

java/src/org/openqa/selenium/remote/RemoteWebDriver.java

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

2020
import static java.util.Collections.singleton;
21+
import static java.util.Objects.requireNonNull;
2122
import static java.util.concurrent.TimeUnit.SECONDS;
2223
import static java.util.logging.Level.SEVERE;
23-
import static org.openqa.selenium.HasDownloads.DownloadedFile;
2424
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
2525

2626
import java.io.BufferedInputStream;
@@ -69,6 +69,7 @@
6969
import org.openqa.selenium.SearchContext;
7070
import org.openqa.selenium.SessionNotCreatedException;
7171
import org.openqa.selenium.TakesScreenshot;
72+
import org.openqa.selenium.UnsupportedCommandException;
7273
import org.openqa.selenium.WebDriver;
7374
import org.openqa.selenium.WebDriverException;
7475
import org.openqa.selenium.WebElement;
@@ -83,6 +84,7 @@
8384
import org.openqa.selenium.interactions.Sequence;
8485
import org.openqa.selenium.internal.Debug;
8586
import org.openqa.selenium.internal.Require;
87+
import org.openqa.selenium.io.Zip;
8688
import org.openqa.selenium.logging.LocalLogs;
8789
import org.openqa.selenium.logging.LoggingHandler;
8890
import org.openqa.selenium.logging.Logs;
@@ -730,12 +732,24 @@ public List<DownloadedFile> getDownloadedFiles() {
730732
public void downloadFile(String fileName, Path targetLocation) throws IOException {
731733
requireDownloadsEnabled(capabilities);
732734

733-
Response response = execute(DriverCommand.GET_DOWNLOADED_FILE, Map.of("name", fileName));
735+
try {
736+
Response response = execute(DriverCommand.GET_DOWNLOADED_FILE, Map.of("name", fileName));
737+
738+
Contents.Supplier content = (Contents.Supplier) response.getValue();
739+
try (InputStream fileContent = content.get()) {
740+
Files.createDirectories(targetLocation);
741+
Files.copy(new BufferedInputStream(fileContent), targetLocation.resolve(fileName));
742+
}
743+
} catch (UnsupportedCommandException e) {
744+
String error = requireNonNull(e.getMessage(), e.toString()).split("\n", 2)[0];
745+
LOG.log(
746+
Level.WARNING,
747+
"You have too old Selenium Grid version, please upgrade it. Caused by: {0}",
748+
error);
734749

735-
Contents.Supplier content = (Contents.Supplier) response.getValue();
736-
try (InputStream fileContent = content.get()) {
737-
Files.createDirectories(targetLocation);
738-
Files.copy(new BufferedInputStream(fileContent), targetLocation.resolve(fileName));
750+
Response response = execute(DriverCommand.DOWNLOAD_FILE, Map.of("name", fileName));
751+
String contents = ((Map<String, String>) response.getValue()).get("contents");
752+
Zip.unzip(contents, targetLocation.toFile());
739753
}
740754
}
741755

0 commit comments

Comments
 (0)