Skip to content

Commit 69a9ae4

Browse files
committed
[java] download file to disk without string return
1 parent f87014d commit 69a9ae4

File tree

3 files changed

+17
-63
lines changed

3 files changed

+17
-63
lines changed

java/src/org/openqa/selenium/HasDownloads.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,13 @@ default void requireDownloadsEnabled(Capabilities capabilities) {
4848
List<String> getDownloadableFiles();
4949

5050
/**
51-
* Downloads a file with the given file name.
51+
* Downloads a file to a given location.
5252
*
5353
* @param fileName the name of the file to be downloaded
54-
* @return a String representing a base64 encoded zip file
54+
* @param targetLocation the location where the file will be downloaded to
55+
* @throws IOException if an I/O error occurs while downloading the file
5556
*/
56-
String downloadFile(String fileName);
57-
58-
/**
59-
* Downloads a file to a given location.
60-
*
61-
* @param fileName the name of the file to be downloaded
62-
* @param downloadLocation the location where the file will be downloaded to
63-
* @throws IOException if an I/O error occurs while downloading the file
64-
*/void downloadFile(String fileName, Path downloadLocation) throws IOException;
57+
void downloadFile(String fileName, Path targetLocation) throws IOException;
6558

6659
/** Deletes the downloadable files. */
6760
void deleteDownloadableFiles();

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

+10-18
Original file line numberDiff line numberDiff line change
@@ -722,36 +722,27 @@ public void removeVirtualAuthenticator(VirtualAuthenticator authenticator) {
722722
@SuppressWarnings("unchecked")
723723
public List<String> getDownloadableFiles() {
724724
requireDownloadsEnabled(capabilities);
725+
725726
Response response = execute(DriverCommand.GET_DOWNLOADABLE_FILES);
726727
Map<String, List<String>> value = (Map<String, List<String>>) response.getValue();
727728
return value.get("names");
728729
}
729730

730-
/**
731-
* Downloads a file with the specified file name.
732-
*
733-
* @param fileName the name of the file to be downloaded
734-
* @return string representation of the Base64 zipped content
735-
* @throws WebDriverException if capability to enable downloads is not set
736-
*/
737-
@Override
738-
@SuppressWarnings("unchecked")
739-
public String downloadFile(String fileName) {
740-
requireDownloadsEnabled(capabilities);
741-
Response response = execute(DriverCommand.DOWNLOAD_FILE, Map.of("name", fileName));
742-
return ((Map<String, String>) response.getValue()).get("contents");
743-
}
744-
745731
/**
746732
* Downloads a file from the specified location.
747733
*
748734
* @param fileName the name of the file to download
749-
* @param downloadLocation the location where the file should be downloaded
735+
* @param targetLocation the location where the file should be downloaded
750736
* @throws IOException if an I/O error occurs during the file download
751737
*/
738+
@SuppressWarnings("unchecked")
752739
@Override
753-
public void downloadFile(String fileName, Path downloadLocation) throws IOException {
754-
Zip.unzip(downloadFile(fileName), downloadLocation.toFile());
740+
public void downloadFile(String fileName, Path targetLocation) throws IOException {
741+
requireDownloadsEnabled(capabilities);
742+
743+
Response response = execute(DriverCommand.DOWNLOAD_FILE, Map.of("name", fileName));
744+
String contents = ((Map<String, String>) response.getValue()).get("contents");
745+
Zip.unzip(contents, targetLocation.toFile());
755746
}
756747

757748
/**
@@ -762,6 +753,7 @@ public void downloadFile(String fileName, Path downloadLocation) throws IOExcept
762753
@Override
763754
public void deleteDownloadableFiles() {
764755
requireDownloadsEnabled(capabilities);
756+
765757
execute(DriverCommand.DELETE_DOWNLOADABLE_FILES);
766758
}
767759

java/test/org/openqa/selenium/grid/router/RemoteWebDriverDownloadTest.java

+3-34
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,17 @@
2121
import static org.openqa.selenium.testing.drivers.Browser.IE;
2222
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2323

24-
import java.io.BufferedReader;
25-
import java.io.ByteArrayInputStream;
2624
import java.io.IOException;
27-
import java.io.InputStreamReader;
2825
import java.io.StringReader;
2926
import java.net.URL;
30-
import java.nio.charset.StandardCharsets;
3127
import java.nio.file.Files;
3228
import java.nio.file.Path;
3329
import java.time.Duration;
34-
import java.util.Base64;
3530
import java.util.LinkedList;
3631
import java.util.List;
3732
import java.util.Objects;
3833
import java.util.concurrent.ExecutorService;
3934
import java.util.concurrent.Executors;
40-
import java.util.zip.ZipInputStream;
4135
import org.junit.jupiter.api.AfterEach;
4236
import org.junit.jupiter.api.Assertions;
4337
import org.junit.jupiter.api.BeforeEach;
@@ -138,40 +132,15 @@ void canDownloadFiles() throws IOException {
138132

139133
String fileName = ((HasDownloads) driver).getDownloadableFiles().get(0);
140134

141-
Path downloadLocation = Files.createTempDirectory("download");
142-
((HasDownloads) driver).downloadFile(fileName, downloadLocation);
135+
Path targetLocation = Files.createTempDirectory("download");
136+
((HasDownloads) driver).downloadFile(fileName, targetLocation);
143137

144-
String fileContent = String.join("", Files.readAllLines(downloadLocation.resolve(fileName)));
138+
String fileContent = String.join("", Files.readAllLines(targetLocation.resolve(fileName)));
145139
assertThat(fileContent).isEqualTo("Hello, World!");
146140

147141
driver.quit();
148142
}
149143

150-
@Test
151-
void canUseDownloadedContent() throws IOException {
152-
URL gridUrl = server.getUrl();
153-
WebDriver driver = new RemoteWebDriver(gridUrl, capabilities);
154-
driver = new Augmenter().augment(driver);
155-
156-
driver.get(appServer.whereIs("downloads/download.html"));
157-
driver.findElement(By.id("file-1")).click();
158-
159-
new WebDriverWait(driver, Duration.ofSeconds(5))
160-
.until(d -> !((HasDownloads) d).getDownloadableFiles().isEmpty());
161-
162-
String fileName = ((HasDownloads) driver).getDownloadableFiles().get(0);
163-
164-
String base64Zip = ((HasDownloads) driver).downloadFile(fileName);
165-
byte[] decodedBytes = Base64.getDecoder().decode(base64Zip);
166-
ByteArrayInputStream bais = new ByteArrayInputStream(decodedBytes);
167-
try (ZipInputStream zis = new ZipInputStream(bais)) {
168-
zis.getNextEntry();
169-
BufferedReader reader =
170-
new BufferedReader(new InputStreamReader(zis, StandardCharsets.UTF_8));
171-
assertThat(reader.readLine()).isEqualTo("Hello, World!");
172-
}
173-
}
174-
175144
@Test
176145
@Ignore(IE)
177146
@Ignore(SAFARI)

0 commit comments

Comments
 (0)