Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.browsingcontext;

import org.openqa.selenium.json.JsonInput;

public class DownloadCanceled extends NavigationInfo {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use word "canceled" or "cancelled"?
Both are correct, while "canceled" American English, and "cancelled" is British English.

I've checked that in Selenium Java code,

  • "cancelled" is used 17 times, including enum value Status.CANCELLED
  • "canceled" is used 14 times

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually had this same discussion about this word a few times in my career :)

Unlike "color/colour", "canceled" is pretty commonly used in both American/British English, but more common in American.

Across our entire codebase, the American spelling is the winner:

$ grep -r --ignore-case --exclude-dir=.git 'canceled\|canceling' | wc -l
169
$ grep -r --ignore-case --exclude-dir=.git 'cancelled\|cancelling' | wc -l
164

However, if we exclude 3rd party code we have vendored, British spelling has a slight edge:

$ grep -r --ignore-case --exclude-dir=.git --exclude-dir=common/devtools --exclude-dir=third_party 'canceled\|canceling' | wc -l
77
$ grep -r --ignore-case --exclude-dir=.git --exclude-dir=common/devtools --exclude-dir=third_party 'cancelled\|cancelling' | wc -l
88

So, I'd call it a draw.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec uses canceled, which is a hardcore value for checking downloads.


private final String status;

DownloadCanceled(
String browsingContextId, String navigationId, long timestamp, String url, String status) {
super(browsingContextId, navigationId, timestamp, url);
this.status = status != null ? status : "canceled";
}

public static DownloadCanceled fromJson(JsonInput input) {
String browsingContextId = null;
String navigationId = null;
long timestamp = 0;
String url = null;
String status = "canceled";

input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "context":
browsingContextId = input.read(String.class);
break;

case "navigation":
navigationId = input.read(String.class);
break;

case "timestamp":
timestamp = input.read(Long.class);
break;

case "url":
url = input.read(String.class);
break;

case "status":
status = input.read(String.class);
if (!"canceled".equals(status)) {
throw new IllegalArgumentException("Expected status 'canceled', but got: " + status);
}
break;

default:
input.skipValue();
break;
}
}

input.endObject();

return new DownloadCanceled(browsingContextId, navigationId, timestamp, url, status);
}

public String getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.browsingcontext;

import org.openqa.selenium.json.JsonInput;

public class DownloadCompleted extends NavigationInfo {

private final String status;
private final String filepath;

DownloadCompleted(
String browsingContextId,
String navigationId,
long timestamp,
String url,
String status,
String filepath) {
super(browsingContextId, navigationId, timestamp, url);
this.status = status != null ? status : "complete";
this.filepath = filepath;
}

public static DownloadCompleted fromJson(JsonInput input) {
String browsingContextId = null;
String navigationId = null;
long timestamp = 0;
String url = null;
String status = "complete";
String filepath = null;

input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "context":
browsingContextId = input.read(String.class);
break;

case "navigation":
navigationId = input.read(String.class);
break;

case "timestamp":
timestamp = input.read(Long.class);
break;

case "url":
url = input.read(String.class);
break;

case "status":
status = input.read(String.class);
if (!"complete".equals(status)) {
throw new IllegalArgumentException("Expected status 'complete', but got: " + status);
}
break;

case "filepath":
filepath = input.read(String.class);
break;

default:
input.skipValue();
break;
}
}

input.endObject();

return new DownloadCompleted(browsingContextId, navigationId, timestamp, url, status, filepath);
}

public String getStatus() {
return status;
}

public String getFilepath() {
return filepath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.browsingcontext;

import org.openqa.selenium.json.JsonInput;

public class DownloadEnded {

private final NavigationInfo downloadParams;

public DownloadEnded(NavigationInfo downloadParams) {
this.downloadParams = downloadParams;
}

public static DownloadEnded fromJson(JsonInput input) {
String browsingContextId = null;
String navigationId = null;
long timestamp = 0;
String url = null;
String status = null;
String filepath = null;

input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "context":
browsingContextId = input.read(String.class);
break;
case "navigation":
navigationId = input.read(String.class);
break;
case "timestamp":
timestamp = input.read(Long.class);
break;
case "url":
url = input.read(String.class);
break;
case "status":
status = input.read(String.class);
break;
case "filepath":
filepath = input.read(String.class);
break;
default:
input.skipValue();
break;
}
}
input.endObject();

// Create the appropriate object based on status
if ("canceled".equals(status)) {
DownloadCanceled canceled =
new DownloadCanceled(browsingContextId, navigationId, timestamp, url, status);
return new DownloadEnded(canceled);
} else if ("complete".equals(status)) {
DownloadCompleted completed =
new DownloadCompleted(browsingContextId, navigationId, timestamp, url, status, filepath);
return new DownloadEnded(completed);
} else {
throw new IllegalArgumentException(
"status must be either 'canceled' or 'complete', but got: " + status);
}
}

public NavigationInfo getDownloadParams() {
return downloadParams;
}

public boolean isCanceled() {
return downloadParams instanceof DownloadCanceled;
}

public boolean isCompleted() {
return downloadParams instanceof DownloadCompleted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Event;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
import org.openqa.selenium.bidi.browsingcontext.DownloadInfo;
import org.openqa.selenium.bidi.browsingcontext.HistoryUpdated;
import org.openqa.selenium.bidi.browsingcontext.NavigationInfo;
import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed;
import org.openqa.selenium.bidi.browsingcontext.UserPromptOpened;
import org.openqa.selenium.bidi.browsingcontext.*;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
Expand Down Expand Up @@ -70,6 +65,14 @@ public class BrowsingContextInspector implements AutoCloseable {
}
};

private final Function<Map<String, Object>, DownloadEnded> downloadWillEndMapper =
params -> {
try (StringReader reader = new StringReader(JSON.toJson(params));
JsonInput input = JSON.newInput(reader)) {
return input.read(DownloadEnded.class);
}
};

private final Event<BrowsingContextInfo> browsingContextCreated =
new Event<>("browsingContext.contextCreated", browsingContextInfoMapper);

Expand All @@ -91,6 +94,9 @@ public class BrowsingContextInspector implements AutoCloseable {
private final Event<DownloadInfo> downloadWillBeginEvent =
new Event<>("browsingContext.downloadWillBegin", downloadWillBeginMapper);

private final Event<DownloadEnded> downloadWillEndEvent =
new Event<>("browsingContext.downloadEnd", downloadWillEndMapper);

private final Event<UserPromptOpened> userPromptOpened =
new Event<>(
"browsingContext.userPromptOpened",
Expand Down Expand Up @@ -171,6 +177,14 @@ public void onDownloadWillBegin(Consumer<DownloadInfo> consumer) {
}
}

public void onDownloadEnd(Consumer<DownloadEnded> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(downloadWillEndEvent, consumer);
} else {
this.bidi.addListener(browsingContextIds, downloadWillEndEvent, consumer);
}
}

public void onNavigationAborted(Consumer<NavigationInfo> consumer) {
addNavigationEventListener("browsingContext.navigationAborted", consumer);
}
Expand Down Expand Up @@ -227,6 +241,7 @@ public void close() {
this.bidi.clearListener(userPromptClosed);
this.bidi.clearListener(historyUpdated);
this.bidi.clearListener(downloadWillBeginEvent);
this.bidi.clearListener(downloadWillEndEvent);

navigationEventSet.forEach(this.bidi::clearListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,22 @@ void canListenToNavigationCommittedEvent()
@Test
@NeedsFreshDriver
@NotYetImplemented(FIREFOX)
void canListenToDownloadWillBeginEvent()
throws ExecutionException, InterruptedException, TimeoutException {
void canListenToDownloadEnd() throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<DownloadInfo> future = new CompletableFuture<>();
CompletableFuture<DownloadEnded> future = new CompletableFuture<>();

inspector.onDownloadWillBegin(future::complete);
inspector.onDownloadEnd(future::complete);

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate(appServer.whereIs("/downloads/download.html"), ReadinessState.COMPLETE);

driver.findElement(By.id("file-1")).click();

DownloadInfo downloadInfo = future.get(5, TimeUnit.SECONDS);
assertThat(downloadInfo.getBrowsingContextId()).isEqualTo(context.getId());
assertThat(downloadInfo.getUrl()).contains("/downloads/file_1.txt");
assertThat(downloadInfo.getSuggestedFilename()).isEqualTo("file_1.txt");
DownloadEnded downloadEnded = future.get(5, TimeUnit.SECONDS);
assertThat(downloadEnded.getDownloadParams().getBrowsingContextId())
.isEqualTo(context.getId());
assertThat(downloadEnded.isCompleted()).isTrue();
assertThat(downloadEnded.getDownloadParams().getUrl()).contains("/downloads/file_1.txt");
}
}

Expand Down
Loading