Skip to content

Commit 967ac63

Browse files
aldobrynindiemol
andauthored
[Grid] Add delete files method (#12501)
implement delete files method method removes all files in the downloads directory Fixes #11986 Co-authored-by: Diego Molina <[email protected]>
1 parent d556045 commit 967ac63

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

java/src/org/openqa/selenium/grid/node/Node.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ protected Node(Tracer tracer, NodeId id, URI uri, Secret registrationSecret) {
165165
post("/session/{sessionId}/se/files")
166166
.to(params -> new DownloadFile(this, sessionIdFrom(params)))
167167
.with(spanDecorator("node.download_file")),
168+
delete("/session/{sessionId}/se/files")
169+
.to(params -> new DownloadFile(this, sessionIdFrom(params)))
170+
.with(spanDecorator("node.download_file")),
168171
get("/se/grid/node/owner/{sessionId}")
169172
.to(params -> new IsSessionOwner(this, sessionIdFrom(params)))
170173
.with(spanDecorator("node.is_session_owner").andThen(requiresSecret)),

java/src/org/openqa/selenium/grid/node/local/LocalNode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import org.openqa.selenium.internal.Debug;
9696
import org.openqa.selenium.internal.Either;
9797
import org.openqa.selenium.internal.Require;
98+
import org.openqa.selenium.io.FileHandler;
9899
import org.openqa.selenium.io.TemporaryFilesystem;
99100
import org.openqa.selenium.io.Zip;
100101
import org.openqa.selenium.json.Json;
@@ -666,6 +667,13 @@ public HttpResponse downloadFile(HttpRequest req, SessionId id) {
666667
ImmutableMap<String, Map<String, Object>> result = ImmutableMap.of("value", data);
667668
return new HttpResponse().setContent(asJson(result));
668669
}
670+
if (req.getMethod().equals(HttpMethod.DELETE)) {
671+
File[] files = Optional.ofNullable(downloadsDirectory.listFiles()).orElse(new File[] {});
672+
for (File file : files) {
673+
FileHandler.delete(file);
674+
}
675+
return new HttpResponse();
676+
}
669677
String raw = string(req);
670678
if (raw.isEmpty()) {
671679
throw new WebDriverException(

java/test/org/openqa/selenium/grid/node/NodeTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
2626
import static org.openqa.selenium.json.Json.MAP_TYPE;
2727
import static org.openqa.selenium.remote.http.Contents.string;
28+
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
2829
import static org.openqa.selenium.remote.http.HttpMethod.GET;
2930
import static org.openqa.selenium.remote.http.HttpMethod.POST;
3031

@@ -639,6 +640,29 @@ void canListFilesToDownload() throws IOException {
639640
}
640641
}
641642

643+
@Test
644+
@DisplayName("DownloadsTestCase")
645+
void canDeleteFileDownloads() throws IOException {
646+
647+
Either<WebDriverException, CreateSessionResponse> response =
648+
node.newSession(createSessionRequest(caps));
649+
assertThatEither(response).isRight();
650+
Session session = response.right().getSession();
651+
String zip = simulateFileDownload(session.getId(), "Hello, world!");
652+
653+
try {
654+
assertThat(listFileDownloads(session.getId())).contains(zip);
655+
656+
HttpRequest deleteRequest = new HttpRequest(DELETE, String.format("/session/%s/se/files", session.getId()));
657+
HttpResponse deleteResponse = node.execute(deleteRequest);
658+
assertThat(deleteResponse.isSuccessful()).isTrue();
659+
660+
assertThat(listFileDownloads(session.getId()).isEmpty()).isTrue();
661+
} finally {
662+
node.stop(session.getId());
663+
}
664+
}
665+
642666
@Test
643667
@DisplayName("DownloadsTestCase")
644668
void ensureImmunityToSessionTimeOutsForFileDownloads() throws InterruptedException {
@@ -905,6 +929,18 @@ private String simulateFileDownload(SessionId id, String text) throws IOExceptio
905929
return zip.getName();
906930
}
907931

932+
private List<String> listFileDownloads(SessionId sessionId) {
933+
HttpRequest req = new HttpRequest(GET, String.format("/session/%s/se/files", sessionId));
934+
HttpResponse rsp = node.execute(req);
935+
Map<String, Object> raw = new Json().toType(string(rsp), Json.MAP_TYPE);
936+
assertThat(raw).isNotNull();
937+
Map<String, Object> map =
938+
Optional.ofNullable(raw.get("value"))
939+
.map(data -> (Map<String, Object>) data)
940+
.orElseThrow(() -> new IllegalStateException("Could not find value attribute"));
941+
return (List<String>) map.get("names");
942+
}
943+
908944
private static class MyClock extends Clock {
909945

910946
private final AtomicReference<Instant> now;

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.assertj.core.api.Assertions.assertThat;
2121
import static org.openqa.selenium.remote.http.Contents.asJson;
2222
import static org.openqa.selenium.remote.http.Contents.string;
23+
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
2324
import static org.openqa.selenium.remote.http.HttpMethod.GET;
2425
import static org.openqa.selenium.remote.http.HttpMethod.POST;
2526
import static org.openqa.selenium.testing.drivers.Browser.IE;
@@ -115,14 +116,8 @@ void testCanListDownloadedFiles() throws InterruptedException {
115116
// Waiting for the file to be remotely downloaded
116117
TimeUnit.SECONDS.sleep(3);
117118

118-
HttpRequest request = new HttpRequest(GET, String.format("/session/%s/se/files", sessionId));
119119
try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
120-
HttpResponse response = client.execute(request);
121-
Map<String, Object> jsonResponse = new Json().toType(string(response), Json.MAP_TYPE);
122-
@SuppressWarnings("unchecked")
123-
Map<String, Object> value = (Map<String, Object>) jsonResponse.get("value");
124-
@SuppressWarnings("unchecked")
125-
List<String> names = (List<String>) value.get("names");
120+
List<String> names = getDownloadedFilesList(client, sessionId);
126121
assertThat(names).contains("file_1.txt", "file_2.jpg");
127122
} finally {
128123
driver.quit();
@@ -158,4 +153,45 @@ void testCanDownloadFiles() throws InterruptedException, IOException {
158153
driver.quit();
159154
}
160155
}
156+
157+
@Test
158+
@Ignore(IE)
159+
@Ignore(SAFARI)
160+
void testCanDeleteFiles() throws InterruptedException {
161+
URL gridUrl = server.getUrl();
162+
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, capabilities);
163+
driver.get(appServer.whereIs("downloads/download.html"));
164+
driver.findElement(By.id("file-1")).click();
165+
SessionId sessionId = driver.getSessionId();
166+
167+
// Waiting for the file to be remotely downloaded
168+
TimeUnit.SECONDS.sleep(3);
169+
170+
try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
171+
List<String> names = getDownloadedFilesList(client, sessionId);
172+
assertThat(names).contains("file_1.txt");
173+
174+
HttpRequest
175+
deleteRequest =
176+
new HttpRequest(DELETE, String.format("/session/%s/se/files", sessionId));
177+
HttpResponse deleteResponse = client.execute(deleteRequest);
178+
assertThat(deleteResponse.isSuccessful()).isTrue();
179+
180+
List<String> afterDeleteNames = getDownloadedFilesList(client, sessionId);
181+
assertThat(afterDeleteNames.isEmpty()).isTrue();
182+
} finally {
183+
driver.quit();
184+
}
185+
}
186+
187+
private static List<String> getDownloadedFilesList(HttpClient client, SessionId sessionId) {
188+
HttpRequest request = new HttpRequest(GET, String.format("/session/%s/se/files", sessionId));
189+
HttpResponse response = client.execute(request);
190+
Map<String, Object> jsonResponse = new Json().toType(string(response), Json.MAP_TYPE);
191+
@SuppressWarnings("unchecked")
192+
Map<String, Object> value = (Map<String, Object>) jsonResponse.get("value");
193+
@SuppressWarnings("unchecked")
194+
List<String> names = (List<String>) value.get("names");
195+
return names;
196+
}
161197
}

0 commit comments

Comments
 (0)