Skip to content

Commit

Permalink
[Grid] Add delete files method (SeleniumHQ#12501)
Browse files Browse the repository at this point in the history
implement delete files method

method removes all files in the downloads directory

Fixes SeleniumHQ#11986

Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
aldobrynin and diemol committed Aug 8, 2023
1 parent d556045 commit 967ac63
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
3 changes: 3 additions & 0 deletions java/src/org/openqa/selenium/grid/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ protected Node(Tracer tracer, NodeId id, URI uri, Secret registrationSecret) {
post("/session/{sessionId}/se/files")
.to(params -> new DownloadFile(this, sessionIdFrom(params)))
.with(spanDecorator("node.download_file")),
delete("/session/{sessionId}/se/files")
.to(params -> new DownloadFile(this, sessionIdFrom(params)))
.with(spanDecorator("node.download_file")),
get("/se/grid/node/owner/{sessionId}")
.to(params -> new IsSessionOwner(this, sessionIdFrom(params)))
.with(spanDecorator("node.is_session_owner").andThen(requiresSecret)),
Expand Down
8 changes: 8 additions & 0 deletions java/src/org/openqa/selenium/grid/node/local/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.openqa.selenium.internal.Debug;
import org.openqa.selenium.internal.Either;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.io.Zip;
import org.openqa.selenium.json.Json;
Expand Down Expand Up @@ -666,6 +667,13 @@ public HttpResponse downloadFile(HttpRequest req, SessionId id) {
ImmutableMap<String, Map<String, Object>> result = ImmutableMap.of("value", data);
return new HttpResponse().setContent(asJson(result));
}
if (req.getMethod().equals(HttpMethod.DELETE)) {
File[] files = Optional.ofNullable(downloadsDirectory.listFiles()).orElse(new File[] {});
for (File file : files) {
FileHandler.delete(file);
}
return new HttpResponse();
}
String raw = string(req);
if (raw.isEmpty()) {
throw new WebDriverException(
Expand Down
36 changes: 36 additions & 0 deletions java/test/org/openqa/selenium/grid/node/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import static org.openqa.selenium.remote.http.HttpMethod.POST;

Expand Down Expand Up @@ -639,6 +640,29 @@ void canListFilesToDownload() throws IOException {
}
}

@Test
@DisplayName("DownloadsTestCase")
void canDeleteFileDownloads() throws IOException {

Either<WebDriverException, CreateSessionResponse> response =
node.newSession(createSessionRequest(caps));
assertThatEither(response).isRight();
Session session = response.right().getSession();
String zip = simulateFileDownload(session.getId(), "Hello, world!");

try {
assertThat(listFileDownloads(session.getId())).contains(zip);

HttpRequest deleteRequest = new HttpRequest(DELETE, String.format("/session/%s/se/files", session.getId()));
HttpResponse deleteResponse = node.execute(deleteRequest);
assertThat(deleteResponse.isSuccessful()).isTrue();

assertThat(listFileDownloads(session.getId()).isEmpty()).isTrue();
} finally {
node.stop(session.getId());
}
}

@Test
@DisplayName("DownloadsTestCase")
void ensureImmunityToSessionTimeOutsForFileDownloads() throws InterruptedException {
Expand Down Expand Up @@ -905,6 +929,18 @@ private String simulateFileDownload(SessionId id, String text) throws IOExceptio
return zip.getName();
}

private List<String> listFileDownloads(SessionId sessionId) {
HttpRequest req = new HttpRequest(GET, String.format("/session/%s/se/files", sessionId));
HttpResponse rsp = node.execute(req);
Map<String, Object> raw = new Json().toType(string(rsp), Json.MAP_TYPE);
assertThat(raw).isNotNull();
Map<String, Object> map =
Optional.ofNullable(raw.get("value"))
.map(data -> (Map<String, Object>) data)
.orElseThrow(() -> new IllegalStateException("Could not find value attribute"));
return (List<String>) map.get("names");
}

private static class MyClock extends Clock {

private final AtomicReference<Instant> now;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.remote.http.Contents.asJson;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import static org.openqa.selenium.remote.http.HttpMethod.POST;
import static org.openqa.selenium.testing.drivers.Browser.IE;
Expand Down Expand Up @@ -115,14 +116,8 @@ void testCanListDownloadedFiles() throws InterruptedException {
// Waiting for the file to be remotely downloaded
TimeUnit.SECONDS.sleep(3);

HttpRequest request = new HttpRequest(GET, String.format("/session/%s/se/files", sessionId));
try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
HttpResponse response = client.execute(request);
Map<String, Object> jsonResponse = new Json().toType(string(response), Json.MAP_TYPE);
@SuppressWarnings("unchecked")
Map<String, Object> value = (Map<String, Object>) jsonResponse.get("value");
@SuppressWarnings("unchecked")
List<String> names = (List<String>) value.get("names");
List<String> names = getDownloadedFilesList(client, sessionId);
assertThat(names).contains("file_1.txt", "file_2.jpg");
} finally {
driver.quit();
Expand Down Expand Up @@ -158,4 +153,45 @@ void testCanDownloadFiles() throws InterruptedException, IOException {
driver.quit();
}
}

@Test
@Ignore(IE)
@Ignore(SAFARI)
void testCanDeleteFiles() throws InterruptedException {
URL gridUrl = server.getUrl();
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, capabilities);
driver.get(appServer.whereIs("downloads/download.html"));
driver.findElement(By.id("file-1")).click();
SessionId sessionId = driver.getSessionId();

// Waiting for the file to be remotely downloaded
TimeUnit.SECONDS.sleep(3);

try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
List<String> names = getDownloadedFilesList(client, sessionId);
assertThat(names).contains("file_1.txt");

HttpRequest
deleteRequest =
new HttpRequest(DELETE, String.format("/session/%s/se/files", sessionId));
HttpResponse deleteResponse = client.execute(deleteRequest);
assertThat(deleteResponse.isSuccessful()).isTrue();

List<String> afterDeleteNames = getDownloadedFilesList(client, sessionId);
assertThat(afterDeleteNames.isEmpty()).isTrue();
} finally {
driver.quit();
}
}

private static List<String> getDownloadedFilesList(HttpClient client, SessionId sessionId) {
HttpRequest request = new HttpRequest(GET, String.format("/session/%s/se/files", sessionId));
HttpResponse response = client.execute(request);
Map<String, Object> jsonResponse = new Json().toType(string(response), Json.MAP_TYPE);
@SuppressWarnings("unchecked")
Map<String, Object> value = (Map<String, Object>) jsonResponse.get("value");
@SuppressWarnings("unchecked")
List<String> names = (List<String>) value.get("names");
return names;
}
}

0 comments on commit 967ac63

Please sign in to comment.