-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Description
Feature and motivation
I understand that files get automatically deleted after a session but let's say I have a test case (which I really do) that does 10 file downloads and those files don't have unique names. Now with the existing implementation when a POST request is made to "/session/{sessionId}/se/files" it will fetch all the files again and there will be a discrepancy between what file I need and what file is being given to me by the endpoint. To avoid this, if a DELETE request is supported, after fetching each file we could delete it and not worry about what file is being provided to us because other files just got deleted.
Code example below -
Usage example
protected String getDownloadedFileByGrid( String expectedFile ) throws Exception {
String home = System.getProperty("user.home");
SessionId sessionId = ( ( RemoteWebDriver ) driver ).getSessionId();
File dirToCopy = new File(home + File.separator + "Downloads" + File.separator + "gridnodes" + File.separator + sessionId);
String uri = String.format("/session/%s/se/files", sessionId );
String fileToDownload = null;
try ( HttpClient client = HttpClient.Factory.createDefault().createClient(new URL( "http://localhost:4444/" ) )) {
HttpRequest request = new HttpRequest( org.openqa.selenium.remote.http.HttpMethod.GET, uri);
HttpResponse response = client.execute(request);
String text = string(response);
Type responseType = new TypeToken<Map<String, Map<String, List<String>>>>() {
}.getType();
Map<String, Map<String, List<String>>> map = new Json().toType(text, responseType);
Map<String, List<String>> parsedResponse = map.get("value");
for (String eachFile : parsedResponse.get("names")) {
if( eachFile.contains( expectedFile ) ) {
fileToDownload = eachFile;
break;
}
}
}
try (HttpClient client = HttpClient.Factory.createDefault().createClient(new URL( "http://localhost:4444/" ) )) {
HttpRequest request = new HttpRequest( org.openqa.selenium.remote.http.HttpMethod.POST, uri);
request.setContent( Contents.asJson(singletonMap("name", fileToDownload)));
HttpResponse response = client.execute(request);
String text = string(response);
Type responseType = new TypeToken<Map<String, Map<String, String>>>() {
}.getType();
Map<String, Map<String, String>> map = new Json().toType(text, responseType);
Map<String, String> parsedResponse = map.get("value");
String encodedContents = parsedResponse.get("contents");
Zip.unzip(encodedContents, dirToCopy );
System.out.println("The file which was "
+ "downloaded in the node is now available in the directory: "
+ dirToCopy.getAbsolutePath());
}
return dirToCopy + File.separator + fileToDownload;This is where the problem is -
for (String eachFile : parsedResponse.get("names")) {
if( eachFile.contains( expectedFile ) ) {
fileToDownload = eachFile;
break;
}
}
Let's say I download 10 files and they don't have unique names and I say give me file that contains ".xls" or ".pdf". First file we get is good but when It downloads the second file, the list of files from node is fetched again. It will check ok this first file contains ".xls" or ".pdf and it will return back the same file.